bool GalleryUtil::Rename(const QString &currDir, const QString &oldName, const QString &newName) { // make sure there isn't already a file/directory with the same name QFileInfo fi(currDir + '/' + newName); if (fi.exists()) return false; fi.setFile(currDir + '/' + oldName); if (fi.isDir()) return RenameDirectory(currDir, oldName, newName); // rename the file QDir cdir(currDir); if (!cdir.rename(oldName, newName)) return false; // rename the file's thumbnail if it exists if (QFile::exists(currDir + "/.thumbcache/" + oldName)) { QDir d(currDir + "/cache/gallery-thumbcache/"); d.rename(oldName, newName); } int prefixLen = gCoreContext->GetSetting("GalleryDir").length(); QString path = GetConfDir() + "/MythGallery"; path += currDir.right(currDir.length() - prefixLen); path += QString("/.thumbcache/"); if (QFile::exists(path + oldName)) { QDir d(path); d.rename(oldName, newName); } // fix up the metadata in the database MSqlQuery query(MSqlQuery::InitCon()); query.prepare("UPDATE gallerymetadata " "SET image = :IMAGENEW " "WHERE image = :IMAGEOLD"); query.bindValue(":IMAGENEW", QString(currDir + '/' + newName)); query.bindValue(":IMAGEOLD", QString(currDir + '/' + oldName)); if (query.exec()) return true; // try to undo rename on DB failure cdir.rename(newName, oldName); return false; }
void CPhotoPubDoc::OnNametrans() { char szDir[MAX_PATH]; BROWSEINFO bi; ITEMIDLIST *pidl; bi.hwndOwner = NULL; bi.pidlRoot = NULL; bi.pszDisplayName = szDir; bi.lpszTitle = "请选择照片所在的文件夹:"; bi.ulFlags = BIF_RETURNONLYFSDIRS; bi.lpfn = NULL; bi.lParam = 0; bi.iImage = 0; pidl = SHBrowseForFolder(&bi); if(pidl == NULL) return ; if(!SHGetPathFromIDList(pidl, szDir)) return ; CString SrcPath(szDir); SrcPath += '\\'; int n=CountAllFolderFile(SrcPath,"*.jpg"); if (!n) { AfxMessageBox("该文件夹中没有JPG文件!"); return; } CFileDialog dlg(TRUE, "*.jpg","",\ OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST|OFN_HIDEREADONLY,\ "Excel格式(*.xls) |*.xls|All Files (*.*)|*.*||",NULL); char title[]= {"打开包含学号和姓名的Excel文件"}; dlg.m_ofn.lpstrTitle= title; if (dlg.DoModal() == IDOK) { CString excel= dlg.GetPathName(); // contain the selected filename RenameDirectory(SrcPath,excel); } }
nfsstat3 CNFS3Prog::ProcedureRENAME(void) { char pathFrom[MAXPATHLEN], *pathTo; wcc_data fromdir_wcc, todir_wcc; nfsstat3 stat; PrintLog("RENAME"); std::string dirFromName; std::string fileFromName; ReadDirectory(dirFromName, fileFromName); strcpy_s(pathFrom, GetFullPath(dirFromName, fileFromName)); std::string dirToName; std::string fileToName; ReadDirectory(dirToName, fileToName); pathTo = GetFullPath(dirToName, fileToName); stat = CheckFile((char*)dirFromName.c_str(), pathFrom); fromdir_wcc.before.attributes_follow = GetFileAttributesForNFS((char*)dirFromName.c_str(), &fromdir_wcc.before.attributes); todir_wcc.before.attributes_follow = GetFileAttributesForNFS((char*)dirToName.c_str(), &todir_wcc.before.attributes); if (FileExists(pathTo)) { DWORD fileAttr = GetFileAttributes(pathTo); if ((fileAttr & FILE_ATTRIBUTE_DIRECTORY) && (fileAttr & FILE_ATTRIBUTE_REPARSE_POINT)) { if (RemoveFolder(pathTo) == 0) { stat = NFS3ERR_IO; } } else { if (!RemoveFile(pathTo)) { stat = NFS3ERR_IO; } } } if (stat == NFS3_OK) { errno_t errorNumber = RenameDirectory(pathFrom, pathTo); if (errorNumber != 0) { char buffer[BUFFER_SIZE]; strerror_s(buffer, BUFFER_SIZE, errorNumber); PrintLog(buffer); if (errorNumber == 13) { stat = NFS3ERR_ACCES; } else { stat = NFS3ERR_IO; } } } fromdir_wcc.after.attributes_follow = GetFileAttributesForNFS((char*)dirFromName.c_str(), &fromdir_wcc.after.attributes); todir_wcc.after.attributes_follow = GetFileAttributesForNFS((char*)dirToName.c_str(), &todir_wcc.after.attributes); Write(&stat); Write(&fromdir_wcc); Write(&todir_wcc); return stat; }