項目中一小段代碼
//------------------------------------------------- // 複製文件 //------------------------------------------------- void CVideoDemoDlg::Copy_file() { TCHAR path[200]; CString strSrcPath; CString strDesPath; //設置要複製的到文件夾路徑 strDesPath = "E:\\"; //設置當前工做路徑 SetCurrentDirectory("D:\\"); GetCurrentDirectory(200,path); CFileFind finder; BOOL bWorking = finder.FindFile("*.*"); while (bWorking) { bWorking = finder.FindNextFile(); strSrcPath = "D:\\"; CString filename= finder.GetFileName(); //獲得文件或文件夾名 if(filename==savanewfilename || filename == "." || filename == ".." || filename == "Recycled" || filename == "System Volume Information") { //對於文件系統提供的文件不fj } else { strSrcPath = strSrcPath + filename; CopyDirectory(strSrcPath,strDesPath); //複製目錄 } } }
//------------------------------------------------- // 供Copy_file()使用 //------------------------------------------------- void CVideoDemoDlg::CopyDirectory(CString pTo,CString pFrom) { char buf[1024]; char buf1[1024]; SHFILEOPSTRUCT fo; memset(buf,0,sizeof(buf)); memset(buf1,0,sizeof(buf1)); memset(&fo,0,sizeof(fo)); strcpy(buf,pTo); strcpy(buf1,pFrom); fo.wFunc=FO_COPY; //複製是FO_COPY,刪除是FO_DELETE; 移動FO_MOVE fo.pFrom=buf; fo.pTo=buf1; fo.fFlags=FOF_NOERRORUI|FOF_NOCONFIRMMKDIR | FOF_NOCONFIRMATION; SHFileOperation(&fo); } //------------------------------------------------- // 刪除文件 //------------------------------------------------- void CVideoDemoDlg::Delete_file() { TCHAR path[200]; SetCurrentDirectory("D:\\"); //設置當前目錄 GetCurrentDirectory(200,path); //獲得當前目錄路徑 CFileFind finder; BOOL bWorking = finder.FindFile("*.*"); while (bWorking) { bWorking = finder.FindNextFile(); CString filename= finder.GetFileName(); //獲得文件名 if(filename==savanewfilename || filename == "." || filename == ".."|| filename == "Recycled" || filename == "System Volume Information") { //若是是當前在存儲的文件夾或根目錄或上及目錄以及磁盤迴收站則不人任何處理 } else { DeleteDirectory(filename); //刪除目錄 } } } //------------------------------------------------- // 供Delete_file()調用 //------------------------------------------------- BOOL CVideoDemoDlg::DeleteDirectory(LPCTSTR DirName) { CFileFind tempFind; char tempFileFind[200]; sprintf(tempFileFind, "%s\\*.* ",DirName); BOOL IsFinded=(BOOL)tempFind.FindFile(tempFileFind); //判斷文件夾 while(IsFinded) { IsFinded=(BOOL)tempFind.FindNextFile(); //尋找下一個目錄 if(tempFileFind == str) { //若是等於當前正在存儲的文件夾就不進行任保操做 } if(!tempFind.IsDots()) { char foundFileName[200]; strcpy(foundFileName,tempFind.GetFileName().GetBuffer(200)); if(tempFind.IsDirectory()) { char tempDir[200]; sprintf(tempDir, "%s\\%s ",DirName,foundFileName); DeleteDirectory(tempDir); //刪除文件夾 } else { char tempFileName[200]; sprintf(tempFileName, "%s\\%s ",DirName,foundFileName); DeleteFile(tempFileName); //刪除文件 } } } tempFind.Close(); //當尋找文件的事件結束就結束尋找功能 RemoveDirectory(DirName); //刪除這個目錄 return TRUE; }
以上代碼是用於文件複製和刪除,可是其特殊性,是由其需求確認,正在寫的當前文件不能複製和刪除