开发者

Writing a Batch File to delete all files [duplicate]

开发者 https://www.devze.com 2023-03-31 16:28 出处:网络
This question already has answers here: Batch file to delete files older than N days (25 answers) Closed 4 years ago.
This question already has answers here: Batch file to delete files older than N days (25 answers) Closed 4 years ago. 开发者_JS百科

Can somebody tell me how I write a batch file that I can attach to a scheduled task that will delete IIS logs that are older than 2 weeks.


Use forFiles in a batch file:

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

Commandline params explained:

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

You can find all in the technet link in my post.

You will need to install one the Win server resource kits such as Windows Server 2003 Resource Kit Tools


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

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

Key /p Path The Path to search (default=current folder)

/s Recurse into sub-folders

/C command The command to execute for each file. Wrap the command string in double quotes. Default = "cmd /c echo @file"

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

/D date Select files with a last modified date greater than or


You could use the script deleteoldfiles.bat as an example.
(From "How to Delete Old IIS Log Files" blog post)

The following example deletes all files named “ex*.log” that were last modified over 1 year (365 days) ago. The starting directory is C:\Windows\System32\LogFiles:

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

The following example searches all subdirectories in C:\Windows and deletes all files named “dump.txt” that were last modified over 60 days ago:

deleteoldfiles.bat C:\Windows dump.txt 60

From there, you can schedule that batch, making sure of the arguments and their enclosing quotes aren't an issue.
See for instance "How to use the Windows Task Scheduler".

0

精彩评论

暂无评论...
验证码 换一张
取 消