C language implements the method of emptying all the files in the specified folder

  • 2020-04-02 03:06:32
  • OfStack

This article illustrates a C implementation that emptying all files in a specified folder. Share with you for your reference. Specific analysis is as follows:

Recently I was doing a project on computer vision, the need to advance the video frame to experiment, when the amount of data is very big and subfolders and files in the folder many, manually delete these files are not reality, the author wrote a program today, delete all related files automatically by the machine, very fast, delete the time almost can be neglected.

The following code is for reference only.


#include "iostream.h"
//If the code needs debugging, simply change it to ""
#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;
void main()
{
//Empty all files in a specific folder
char* a=".";
char* b="";
WIN32_FIND_DATA FileData,FileData_0;
HANDLE hSearch,hSearch_0;
BOOL fFinished=FALSE;
hSearch=FindFirstFile("C:\experiment\results_stat\*.*",&FileData);
//Enter the absolute pathname of a particular file
if(hSearch==INVALID_HANDLE_VALUE)
{
printf("No files found.");
return;
}
while(!fFinished)
{
if(FileData.cFileName[0]!=a[0]){
b=FileData.cFileName;
string addr=string("C:\experiment\results_stat\")+string(b)+string("\")+string("*.*");
//Enter the absolute pathname of a particular file
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:\experiment\results_stat\")+string(b)+string("\")+FileData_0.cFileName;
//Enter the absolute pathname of a particular file
DeleteFile(addr_0.c_str());
//Empty all files in subfolders within a specific folder
}
}
}
if(!FindNextFile(hSearch,&FileData))
{
if(GetLastError()==ERROR_NO_MORE_FILES)
{
fFinished=TRUE;
}
else
{
printf("Couldn't find next file.");
return;
}
}
}
FindClose(hSearch);
FindClose(hSearch_0);
}

Hope that the article described in the C programming language for you to help.


Related articles: