Win32 文件操作 示例代码

void show(const char *folderPath)
{
    CFileFind finder;
    CString path(folderPath);
    path += "*.*";
    BOOL bWorking = finder.FindFile(path);
    while (bWorking) {
        bWorking = finder.FindNextFile();
        cout << finder.GetFilePath() << endl;
    }
    finder.Close();
}
bool CopyFolder(const char *srcFolderPath, const char *dstFolderPath)
{
    CFileFind srcFinder, dstFinder;
    CString src(srcFolderPath);
    CString dst(dstFolderPath);
    if (!CreateDirectory(dst.GetBuffer(), 0)) return false;
    CFileFind finder;
    BOOL bWorking = finder.FindFile(src + "*.*");
    while (bWorking) {
        bWorking = finder.FindNextFile();
        if (finder.IsDots()) continue;
        CString fileName = finder.GetFileName();
        if (finder.IsDirectory()) {
            if (!CopyFolder((src + "" + fileName).GetBuffer(), dst + "" + fileName)) {
                finder.Close();
                return false;
            }
        }
        else {
            if (!CopyFile(src + "" + fileName, dst + "" + fileName, 0)) {
                finder.Close();
                return false;
            }
        }
    }
    finder.Close();
    return true;
}
bool DeleteFolder(const char *folderName)
{
    CString path(folderName);
    CFileFind finder;
    BOOL bWorking = finder.FindFile(path + "*.*");
    while (bWorking) {
        bWorking = finder.FindNextFile();
        if (finder.IsDots()) continue;
        CString fileName = finder.GetFileName();
        if (finder.IsDirectory()) {
            if (!DeleteFolder((path + "" + fileName).GetBuffer())) {
                finder.Close();
                return false;
            }
        }
        else {
            if (!DeleteFile((path + "" + fileName).GetBuffer())) {
                finder.Close();
                return false;
            }
        }
    }
    finder.Close();
    return RemoveDirectory(folderName);
}
Author: Zhiqiang Ma Posted on: Jan 30, 2009 Views: 207
Tags: , ,
Like this post? Subscribe full-text feeds from all Fclose.com blogs:
Add your comments, share your thoughts

Be nice. Keep it clean. Stay on topic. No spam.