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

本文介绍如何使用C语言在Windows环境下编写代码来删除指定目录下的文件,涉及文件操作和Windows API调用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/***********************************************************************
* 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 <windows.h>
#include <stdio.h>
#include <bitset>
#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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值