C++ 和JAVA遞迴遍歷刪除目錄
JAVA:
import java.io.File;
public class DeleteDirectory { /** * 刪除空目錄 * @param dir 將要刪除的目錄路徑 */ private static void doDeleteEmptyDir(String dir) { boolean success = (new File(dir)).delete(); if (success) { System.out.println("Successfully deleted empty directory: " + dir); } else { System.out.println("Failed to delete empty directory: " + dir); } }
/** * 遞迴刪除目錄下的所有檔案及子目錄下所有檔案 * @param dir 將要刪除的檔案目錄 */ private static boolean deleteDir(File dir) { if (dir.isDirectory()) { String[] children = dir.list(); //遞迴刪除目錄中的子目錄下 for (int i=0; i<children.length; i++) { boolean success = deleteDir(new File(dir, children[i])); if (!success) { return false; } } } // 目錄此時為空,可以刪除 return dir.delete(); } /** *測試 */ public static void main(String[] args) { // doDeleteEmptyDir("D:\\jsoup"); String newDir2 = "D:\\jsoup"; boolean success = deleteDir(new File(newDir2)); if (success) { System.out.println("Successfully deleted populated directory: " + newDir2); } else { System.out.println("Failed to delete populated directory: " + newDir2); } } }
C++
// AA.cpp : 定義控制檯應用程式的入口點。 //
#include "stdafx.h" #include <direct.h> #include <iostream> #include <windows.h> #include <string> using namespace std;
BOOL IsDirectory(const char *pDir) { char szCurPath[500]; ZeroMemory(szCurPath, 500); sprintf_s(szCurPath, 500, "%s//*", pDir); WIN32_FIND_DATAA FindFileData; ZeroMemory(&FindFileData, sizeof(WIN32_FIND_DATAA));
HANDLE hFile = FindFirstFileA(szCurPath, &FindFileData); /**< find first file by given path. */
if (hFile == INVALID_HANDLE_VALUE) { FindClose(hFile); return FALSE; /** 如果不能找到第一個檔案,那麼沒有目錄 */ } else { FindClose(hFile); return TRUE; }
}
BOOL DeleteDirectory(const char * DirName) { // CFileFind tempFind; //宣告一個CFileFind類變數,以用來搜尋 char szCurPath[MAX_PATH]; //用於定義搜尋格式 _snprintf(szCurPath, MAX_PATH, "%s//*.*", DirName); //匹配格式為*.*,即該目錄下的所有檔案 WIN32_FIND_DATAA FindFileData; ZeroMemory(&FindFileData, sizeof(WIN32_FIND_DATAA)); HANDLE hFile = FindFirstFileA(szCurPath, &FindFileData); BOOL IsFinded = TRUE; while (IsFinded) { IsFinded = FindNextFileA(hFile, &FindFileData); //遞迴搜尋其他的檔案 if (strcmp(FindFileData.cFileName, ".") && strcmp(FindFileData.cFileName, "..")) //如果不是"." ".."目錄 { std::string strFileName = ""; strFileName = strFileName+DirName + "//" + FindFileData.cFileName; std::string strTemp; strTemp = strFileName; if (IsDirectory(strFileName.c_str())) //如果是目錄,則遞迴地呼叫 { printf("\n\r目錄為:%s", strFileName.c_str()); DeleteDirectory(strTemp.c_str()); } else { DeleteFileA(strTemp.c_str()); } } } FindClose(hFile);
BOOL bRet = RemoveDirectoryA(DirName); if (bRet == 0) //刪除目錄 { printf("刪除%s目錄失敗!/n", DirName); return FALSE; } return TRUE; }
void pri(BOOL flag){ cout<<flag; if(flag){ cout<< "是資料夾"; }else{ cout<< "不是資料夾"; } }
//建立 BOOL create(string dirName){ //string fileName = "C:\\test1.txt"; BOOL flag=CreateDirectory(dirName.c_str(),NULL); return flag; }
//刪除 BOOL del(string path){ BOOL flag = RemoveDirectory(path.c_str()); return flag; }
int main() { std::string s = "C:\\Dev"; string a="2018年9月26日"; string b="113913"; s = s+"//"+a+b; const char* c_doc = s.c_str(); //BOOL flag =create("C:\\test"); /* BOOL flag =del("C:\\test");*/ IsDirectory(c_doc); DeleteDirectory(c_doc); cout<<"處理完成"; system("pause"); return 0; }