BOOL CopyFileEx( LPCWSTR lpExistingFileName, // pointer to name of an existing file LPCWSTR lpNewFileName, // pointer to filename to copy to LPPROGRESS_ROUTINE lpProgressRoutine, // pointer to the callback function LPVOID lpData, // to be passed to the callback function LPBOOL pbCancel, // flag that can be used to cancel the operation DWORD dwCopyFlags // flags that specify how the file is copied );
前两个还是比较简单的,第三个参数是一个回调函数,定义如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
DWORD CALLBACK CopyProgressRoutine( LARGE_INTEGER TotalFileSize, // total file size, in bytes LARGE_INTEGER TotalBytesTransferred, // total number of bytes transferred LARGE_INTEGER StreamSize, // total number of bytes for this stream LARGE_INTEGER StreamBytesTransferred, // total number of bytes transferred for // this stream DWORD dwStreamNumber, // the current stream DWORD dwCallbackReason, // reason for callback HANDLE hSourceFile, // handle to the source file HANDLE hDestinationFile, // handle to the destination file LPVOID lpData // passed by CopyFileEx );
The copy operation fails immediately if the target file already exists.
COPY_FILE_RESTARTABLE
Progress of the copy is tracked in the target file in case the copy fails. The failed copy can be restarted at a later time by specifying the same values for lpExistingFileName and lpNewFileName as those used in the call that failed.
#include<iostream> #include<windows.h> usingnamespacestd; DWORD CALLBACK CopyProgressRoutine( LARGE_INTEGER TotalFileSize, // total file size, in bytes LARGE_INTEGER TotalBytesTransferred, // total number of bytes transferred LARGE_INTEGER StreamSize, // total number of bytes for this stream LARGE_INTEGER StreamBytesTransferred, // total number of bytes transferred for // this stream DWORD dwStreamNumber, // the current stream DWORD dwCallbackReason, // reason for callback HANDLE hSourceFile, // handle to the source file HANDLE hDestinationFile, // handle to the destination file LPVOID lpData // passed by CopyFileEx ) { printf("%d\n",TotalBytesTransferred.u.LowPart); return PROGRESS_CONTINUE; } intmain() { CopyFileEx("1.zip","1\\3.zip",CopyProgressRoutine,NULL,NULL,COPY_FILE_RESTARTABLE); }
void CStudy_HistoryDlg::RecursiveDelete(CString szPath) { CFileFind ff; CString strPath = szPath; if ("\\" != strPath.Right(1)) { strPath += "\\"; } strPath += "*.*";//这个过滤器类型的东西貌似都是支持正则的。 BOOL bRet;//用来标记是不是已经删除完毕。 if (ff.FindFile(strPath))//这个应该就是文件夹的第一个指针开始。 { do { bRet = ff.FindNextFile();//将内部的那个指针往下移动一下 if (ff.IsDots())//为.或..这种的上级目录 continue; strPath = ff.GetFilePath(); if (!ff.IsDirectory())//看看是不是一个目录 { //是一个文件 //删除文件 ::SetFileAttributes(strPath,FILE_ATTRIBUTE_NORMAL);//先设置一下这个文件的属性,不要只读!!! ::DeleteFile(strPath);
}else { RecursiveDelete(strPath);//经典的递归调用 ::SetFileAttributes(strPath,FILE_ATTRIBUTE_NORMAL);//和上面一样 ::RemoveDirectory(strPath); //移除掉这个文件夹 } } while (bRet);
}
}
我都做了一定的注释,就是一个递归删除,使用了MFC的一个框架,说实话微软给的这个框架是真的好!
需要注意,我们使用DeleteFile这个函数的话呢,是不会进入回收站的!
移动文件MoveFileEx
这次还是在用个Ex加强版本的吧,看一下Ex版本的MSDN定义:
1 2 3 4 5
BOOL MoveFileEx( LPCTSTR lpExistingFileName, // pointer to the name of the existing file LPCTSTR lpNewFileName, // pointer to the new name for the file DWORD dwFlags // flag that specifies how to move file );