最近在项目中遇到一个需求,要求调用后删除该目录下的所有*.*文件
实现后发篇博客来记录一下,日后方便调用
#include "iostream.h"
#include "string.h"
#include "stdlib.h"
#include "time.h"
#include "math.h"
#include "windows.h"
#include "stdio.h"
#include "shellapi.h"
#include "fstream.h"
#include "string"
using namespace std;
int delFile( )
{
//清空特定文件夹中的所有文件
char* a=".";
char* b="";
WIN32_FIND_DATA FileData,FileData_0;
HANDLE hSearch,hSearch_0;
BOOL fFinished=FALSE;
//该文件夹为想要清空的文件夹,根据需要修改
hSearch=FindFirstFile("C:\\CAM_DATA\\*.*",&FileData);
//输入特定文件的绝对路径名
if(hSearch==INVALID_HANDLE_VALUE)
{
//printf("No files found.");
return -1;
}
while(!fFinished)
{
if(FileData.cFileName[0]!=a[0]){
b=FileData.cFileName;
//该文件夹为想要清空的文件夹,根据需要修改
string addr=string("C:\\CAM_DATA\\")+string("*.*");
//输入特定文件的绝对路径名
hSearch_0=FindFirstFile(addr.c_str(),&FileData_0);
while(FindNextFile(hSearch_0, &FileData_0)){
if(FileData_0.cFileName[0]!=a[0]){
//该文件夹为想要清空的文件夹,根据需要修改
string addr_0=string("C:\\CAM_DATA\\")+FileData_0.cFileName;
//输入特定文件的绝对路径名
DeleteFile(addr_0.c_str());
//清空特定文件夹中的子文件夹中的所有文件
}
}
}
if(!FindNextFile(hSearch,&FileData))
{
if(GetLastError()==ERROR_NO_MORE_FILES)
{
fFinished=TRUE;
}
else
{
//printf("Couldn't find next file.");
return -2;
}
}
}
FindClose(hSearch);
FindClose(hSearch_0);
return 0;
}
void main()
{
delFile();
}