Writing a Batch File to delete all files [duplicate]
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 isC:\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".
精彩评论