C C 刪除指定目錄

2021-06-29 15:19:07 字數 1651 閱讀 2918

用到shlwapi.h和tchar.h標頭檔案。

shlwapi.h是shlwapi.dll標頭檔案,shlwapi.dll(microsoft shell light-weight utility library)中定義了路徑相關的操作,也包括了部分檔案操作函式。由於shlwapi.dll屬於microsoft windows shell,因此這些檔案操作特點與通過shell進行檔案操作類似。

下面列出部分常用的檔案操作相關函式。

函式名作用

pathisdirectory

判斷乙個路徑是否有效目錄

pathisdirectoryempty

判斷乙個路徑是否空目錄

pathfileexists

判斷乙個路徑是否有效目錄或檔案

pathrenameextension

更改檔案的字尾名

shfileoperation

可實現檔案或目錄的複製、移動、重新命名和刪除操作,並可一次操作多個檔案或目錄

其中,shfileoperation可實現整個目錄的內容的複製和刪除。shfileoperation可一次實現多個目錄的複製或刪除,其輸入引數結構體shfileopstruct中的pfrom(源目錄)和pto(目的目錄)都可以輸入多個目錄,目錄之間通過』\0』分割,pfrom和pto必須以2個』\0』結尾。一般情況下,我們都只是對乙個目錄操作,因此,shfileoperation呼叫並不是很方便,下面就對shfileoperation進行包裝,提供更方便呼叫的目錄操作函式。

bool shdeletefolder(lpctstr pstrfolder)

if (ipathlen >= max_path)

/*確保目錄的路徑以2個\0結尾*/

zeromemory(tczfolder, (max_path+1)*sizeof(tchar));

_tcscpy(tczfolder, pstrfolder);

tczfolder[ipathlen] = _t('\0');

tczfolder[ipathlen+1] = _t('\0');

zeromemory(&fileop, sizeof(shfileopstruct));

fileop.fflags |= fof_silent; //不顯示進度

fileop.fflags |= fof_noerrorui; //不報告錯誤資訊

fileop.fflags |= fof_noconfirmation; //直接刪除,不進行確認

fileop.hwnd = null;

fileop.lpszprogresstitle = null;

fileop.wfunc = fo_delete;

fileop.pfrom = tczfolder; //要刪除的目錄,必須以2個\0結尾

fileop.pto = null;

fileop.fflags &= ~fof_allowundo; //直接刪除,不放入**站

/*刪除目錄*/

if (0 == shfileoperation(&fileop))

else

}

int main()

刪除指定目錄下指定字尾的檔案

定時清除計畫任務日誌檔案,避免占用太大磁碟空間 folderpath www server log 要操作的目錄 deltype array log foreach deltype as file type param path資料夾絕對路徑 file type待刪除檔案的字尾名 return vo...

刪除指定目錄下的所有svn目錄

windows registry editor version 5.00 hkey local machine software classes folder shell deletesvn delete svn folders hkey local machine software classes...

Linux刪除指定目錄檔案指令碼

刪除語法格式 find 對應目錄 mtime 天數 name 檔名 exec rm rf find u01 masicong mtime 10 name exec rm rf 將 u01 backups目錄下所有10天前帶 的檔案刪除 說明 u01 backups 想要進行清理的任意目錄 mtime...