利用遞迴刪除資料夾(資料夾中套資料夾)

2021-09-26 21:23:27 字數 1086 閱讀 8241

//刪除目錄

bool deldir(const ansistring &p)

if(p.isempty()||p.length()<4)

return false;//引數必須大於3,即不能為磁碟根目錄或空白

int len = p.length();

char* path = p.c_str();

ansistring dir = "";

ansistring str = "/";

if(path[len-1] != '//')

dir = p + str;

ansistring files = dir + "*.*"; //識別檔案

widestring b(files);

win32_find_data wfd;

handle hfind = findfirstfile(b.c_bstr(),&wfd);

bool ret = true;

ansistring tmp;

if(hfind != invalid_handle_value)  //是否有檔案

bool bfind = true;

while(bfind)

if(wfd.cfilename[0]!='.')

tmp = dir + wfd.cfilename;

if(wfd.dwfileattributes&file_attribute_directory)

{//刪除所有子目錄

ret = ret && deldir(tmp); //如果資料夾中有資料夾,呼叫自身進行遞迴刪除

else

{//刪除所有檔案

widestring c(tmp);

setfileattributes(c.c_bstr(),file_attribute_normal);

ret = ret && deletefile(tmp);

bfind = findnextfile(hfind,&wfd);

findclose(hfind);

if(ret)

return removedir(path);

return false;

python 遞迴刪除資料夾 遞迴複製資料夾

學過python os模組的人都知道python中的rmdir 函式只能刪除乙個空的資料夾,而remove 函式也只能刪除單個的檔案,沒有乙個現成的方法來刪除乙個資料夾 裡面有檔案 所以我們要借助遞迴去刪除乙個資料夾中的每乙個檔案 或者資料夾 下面是 遞迴刪除資料夾 import os defdel...

遞迴刪除資料夾跟拷貝資料夾

刪除檔案 存在檔案則直接刪除返回true,如果不存在返回false 刪除目錄 為空 直接刪除 不為空 刪不掉 需要先刪除資料夾裡面所有檔案,再刪除資料夾 不存在直接返回false 注意 delete方法 直接從磁碟中刪除,不能像 站一樣可以恢復 遞迴拷貝資料夾 param oldfile 源資料夾 ...

遞迴刪除資料夾

只能刪除目錄內的所有檔案,目錄內的目錄未刪除。因為當時os.rmdir 不知道加在哪,好像哪都不對,有知道的請告訴我。import os from os import path def diy remove the path if path.exists the path if path.isdir...