本文實例講述了C語言實現清空指定文件夾中所有文件的方法。分享給大家供大家參考。具體分析如下:
最近筆者在做一個有關計算機視覺的項目,需要對提前的視頻幀進行實驗,當數據量很大且文件夾中的子文件夾和文件很多時,手工刪除這些文件不現實,筆者今天寫了一個程序,通過機器自動刪除所有相關文件,十分快速,刪除時間幾乎可以不計。
代碼如下,僅供參考。
#include "iostream.h"http://代碼如需調試,只需將改成""即可#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(){//清空特定文件夾中的所有文件char* a=".";char* b="";WIN32_FIND_DATA FileData,FileData_0;HANDLE hSearch,hSearch_0;BOOL fFinished=FALSE;hSearch=FindFirstFile("C://experiment//results_stat//*.*",&FileData);//輸入特定文件的絕對路徑名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("http://")+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://experiment//results_stat//")+string(b)+string("http://")+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;}}}FindClose(hSearch);FindClose(hSearch_0);}
希望本文所述對大家的C語言程序設計有所幫助。
新聞熱點
疑難解答
圖片精選