2011-08-30 126 views
1

可能重複:
Batch file to delete files older than N days編寫一個批處理文件來刪除所有文件

有人可以告訴我怎麼寫,我可以附加到一個預定的任務,將批量刪除文件IIS日誌超過2周。

我正在使用Windows XP。

謝謝。

+1

你遇到的麻煩,此部分,特別是? –

+0

這應該有所幫助。 http://stackoverflow.com/questions/51054/batch-file-to-delete-files-older-than-n-days –

+0

如果你的意思是實際上寫文件,只需使用記事本或任何其他文本編輯器並將其保存爲.bat或完成後重命名。如果你需要的內容,請參閱下面的VonC的帖子。 – Kevin

回答

6

使用forFiles在批處理文件:

forfiles -p C:\WINDOWS\system32\LogFiles\W3SVC1 -s -m *.log -d -14 -c "Cmd /C DEL @File" 

命令行PARAMS解釋說:

-d -14         // Specifies num days (14 for 2 weeks) 
-p C:\WINDOWS\system32\LogFiles\W3SVC1 // path 
-m *.log         // file mask 

你可以找到所有在我的崗位在TechNet鏈接。

你需要安裝一個雙贏服務器資源工具包,如Windows Server 2003 Resource Kit Tools

+0

這不適用於Windows XP。 – Tarik

+0

@Braveyard:看我的編輯。你需要資源工具包。可以是Win2k3或更高版本。 – Mrchief

+0

如果您的計算機上沒有默認安裝forfiles,請從[Microsoft FTP server](ftp://ftp.microsoft.com/ResKit/y2kfix/x86/)獲取它。將它放到C:\ WINDOWS \ system32 \ forfiles.exe – smoak

1

您可以使用script deleteoldfiles.bat作爲示例。
(摘自「How to Delete Old IIS Log Files」的博客文章)

下面的示例刪除命名的最後一次修改了以前1年(365天)「ex*.log」的所有文件。起始目錄是C:\Windows\System32\LogFiles

deleteoldfiles.bat C:\Windows\System32\LogFiles ex*.log 365 

下面的示例搜索所有子目錄中C:\Windows,並刪除所有文件名爲「dump.txt」這是最後的60天前修改:

deleteoldfiles.bat C:\Windows dump.txt 60 

從那裏,你可以安排該批次,確保o f the arguments and their enclosing quotes aren't an issue
例如見 「How to use the Windows Task Scheduler」。

1
forfiles -p "C:\what\ever" -s -m *.* -d <number of days> -c "cmd /c del @path" 

語法 FORFILES [/ P路徑] [/ M面膜] [/ S] [/ c命令] [/ d [+ | - ] {dd/MM/yyyy | DD}]

密鑰 /P路徑的路徑搜索(缺省值=當前文件夾)

/s的遞歸到子文件夾

/C命令來對每個文件執行的命令。 將命令字符串用雙引號括起來。 默認值=「CMD/C回聲@file」

  The Command variables listed below can also be used in the 
      command string. 

/d日期排序,最後修改日期選擇文件大於或

+0

看來這不適用於Windows XP。 – Tarik

相關問題