windows删除一个目录下的文件c代码

/***********************************************************************
* deleteFile.h
* 删除一个目录下的文件
****************/
#ifdef __cplusplus 
extern "C" { 
#endif

#ifdef UNICODE
#define DelFile DelFileW
    void DelFileW(char *cFilePath);
#else
#define DelFile DelFileA
    void DelFileA(char *cFilePath);
#endif

#ifdef __cplusplus
} 
#endif  
/***********************************************************************
* deleteFile.cpp
****************/
#include 
#include 
#include 
#include "deleteFile.h"

using namespace std;
#ifdef UNICODE
void DelFileW(char *cFilePath)
{
    WIN32_FIND_DATA data;
    HANDLE hFind;
    //char cFullPath[100];
    //char cNewPath[100];
    WCHAR cFullPath[MAX_PATH] = { 0 };
    WCHAR cNewPath[MAX_PATH] = { 0 };
    char NewPath[MAX_PATH] = { 0 };
    char FullPath[MAX_PATH] = { 0 };
    wchar_t *ch_dot = L".";
    wchar_t *ch_double_dot = L"..";

    MultiByteToWideChar(CP_ACP, 0, cFilePath, strlen(cFilePath) + 1, cFullPath,
        sizeof(cFullPath) / sizeof(cFullPath[0]));
    wsprintf(cFullPath, L"%s\\*.*", cFullPath);

    hFind = FindFirstFile(cFullPath, &data);

    do
    {
        if ((!wcscmp(ch_dot, data.cFileName)) || (!wcscmp(ch_double_dot, data.cFileName)))
        {
            continue;
        }
        if (data.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
        {
            MultiByteToWideChar(CP_ACP, 0, cFilePath, strlen(cFilePath) + 1, cFullPath,
                sizeof(cFullPath) / sizeof(cFullPath[0]));
            wsprintf(cNewPath, L"%s\\%s", cFullPath, data.cFileName);
            WideCharToMultiByte(CP_ACP, 0, cNewPath, -1,
                NewPath, MAX_PATH, NULL, NULL);
            DelFileW(NewPath);//递归
        }

        // MessageBox(NULL,data.cFileName,"Look",0);
        MultiByteToWideChar(CP_ACP, 0, cFilePath, strlen(cFilePath) + 1, cFullPath,
            sizeof(cFullPath) / sizeof(cFullPath[0]));
        wsprintf(cFullPath, L"%s\\%s", cFullPath, data.cFileName);
        DeleteFile(cFullPath);

    } while (FindNextFile(hFind, &data));
    MultiByteToWideChar(CP_ACP, 0, cFilePath, strlen(cFilePath) + 1, cFullPath,
        sizeof(cFullPath) / sizeof(cFullPath[0]));
    RemoveDirectory(cFullPath);
    FindClose(hFind);
}
#else
void DelFileA(char *cFilePath)
{
    WIN32_FIND_DATA data;
    HANDLE hFind;
    char NewPath[MAX_PATH] = { 0 };
    char FullPath[MAX_PATH] = { 0 };
    const char *ch_dot = ".";
    const char *ch_double_dot = "..";

    sprintf(FullPath, "%s\\*.*", cFilePath);
    hFind = FindFirstFile(FullPath, &data);
    do{
        if ((!strcmp(ch_dot, data.cFileName)) || (!strcmp(ch_double_dot, data.cFileName)))
        {
            continue;
        }

        if (data.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
        {
            sprintf(NewPath, "%s\\%s", cFilePath, data.cFileName);
            DelFileA(NewPath);//递归
        }

        sprintf(FullPath, "%s\\%s", cFilePath, data.cFileName);
        DeleteFile(FullPath);
    } while (FindNextFile(hFind, &data));
    RemoveDirectory(cFilePath);
    FindClose(hFind);
}

#endif

你可能感兴趣的:(windows编程)