十年網站開發(fā)經驗 + 多家企業(yè)客戶 + 靠譜的建站團隊
量身定制 + 運營維護+專業(yè)推廣+無憂售后,網站問題一站解決
這篇“怎么用c語言刪除某個目錄下的文件”文章的知識點大部分人都不太理解,所以小編給大家總結了以下內容,內容詳細,步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“怎么用c語言刪除某個目錄下的文件”文章吧。
創(chuàng)新互聯公司是一家集網站建設,荷塘企業(yè)網站建設,荷塘品牌網站建設,網站定制,荷塘網站建設報價,網絡營銷,網絡優(yōu)化,荷塘網站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯網需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學習、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網站。
如何創(chuàng)建文件,讀寫文件,這些簡單的我想大家應該是比較熟悉的,我所介紹的是如何遍歷某個目錄,并且刪除該目錄下的文件(可以指定后綴名),并且也可以指定
文件的修改時間范圍(多少小時以前的舊文件可以刪除),下面就是簡單的函數實現,僅供初學者參考
#include#include #include #include #include #include #include #define file_max_len 256 void rmv_old_files(const char *path, const char *suf, int hours) { char filename[file_max_len] = {0}; struct tm *tm; struct dirent *dirp; struct stat statbuf; dir *dp = null; time_t curr_time; int namelen, offset; char *chtemp = null; curr_time = time((time_t*)null); dp = opendir(path); if (null == dp) { return; } while((dirp=readdir(dp)) != null) { if (strcmp(dirp->d_name, ".")==0 || strcmp(dirp->d_name, "..")==0) { continue; } namelen = strlen(dirp->d_name); chtemp = dirp->d_name; if (*suf != '\0') { offset = namelen-strlen(suf); if (offset<0 || strncmp(suf, chtemp+offset, strlen(suf))!=0) { continue; } } sprintf(filename, "%s%s", path, dirp->d_name); if (!stat(filename, &statbuf)) { /*check the st_mtime of the file, if more than retention_hours ago then delete it*/ if (curr_time-statbuf.st_mtime >= hours*3600 && s_isreg(statbuf.st_mode)) { unlink(filename); } } } closedir(dp); }
rm -f 指定目錄*
#最經典的方法,刪除指定目錄下的所有類型的文件
2.find 指定目錄 -type f -delete或find 指定目錄 -type f -exec rm -f {} \;
#用find命令查找指定目錄下的所有普通文件并刪除or用find命令的處理動作將其刪除
3.find 指定目錄 -type f | xargs rm -f
#用于參數列表過長;要刪除的文件太多
4.rm-f `find 指定目錄 -type f`
#刪除指定目錄下的全部普通文件
5.for delete in `ls –l 指定目錄路徑`;do rm -f * ;done
#用for循環(huán)語句刪除指定目錄下的所有類型的文件
以上就是關于“怎么用c語言刪除某個目錄下的文件”這篇文章的內容,相信大家都有了一定的了解,希望小編分享的內容對大家有幫助,若想了解更多相關的知識內容,請關注創(chuàng)新互聯行業(yè)資訊頻道。