编写批处理文件以删除所有文件

时间:2011-08-30 20:22:47

标签: batch-file

有人可以告诉我如何编写一个批处理文件,我可以将其附加到计划任务中,该任务将删除超过2周的IIS日志。

3 个答案:

答案 0 :(得分:7)

在批处理文件中使用forFiles

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

Commandline params解释说:

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

您可以在我的帖子中找到technet链接中的所有内容。

您需要安装一个Win服务器资源工具包,例如Windows Server 2003 Resource Kit Tools

答案 1 :(得分: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中的所有子目录,并删除60天前最后修改过的名为“dump.txt”的所有文件:

deleteoldfiles.bat C:\Windows dump.txt 60

从那里,您可以安排该批次,确保the arguments and their enclosing quotes aren't an issue 例如,请参阅“How to use the Windows Task Scheduler”。

答案 2 :(得分:1)

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

语法       FORFILES [/ p Path] [/ m Mask] [/ s] [/ c Command] [/ d [+ | - ] {dd / MM / yyyy | dd}]

关键    / p Path搜索路径(默认=当前文件夹)

/ s递归到子文件夹

/ C命令为每个文件执行的命令。                 用双引号括起命令字符串。                 默认=“cmd / c echo @file”

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

/ D date选择上次修改日期大于或

的文件
相关问题