Windows – Delete Old Files


A few cautionary warnings… this script will delete files from whatever location you specify as LOGPATH.  Use with care. As a best practice I always create my logs with a pre-fix. ex: CheckUpTime-.log which can then be used in the deletion script. This will ensure that you only delete logs that start with “CheckUpTime”. 

:: Delete log files older then 14 days
set LOGPATH=C:Logs

:: Echo’s logs which will be deleted (use this before using the command which contains “del”)
forfiles -p %LOGPATH% -m CheckUpTime-*.* -d -14 -c “cmd /c echo 0x22@Path@File0x22”

:: Actual Delete Command (remove :: to run command)
::forfiles -p %LOGPATH% -m CheckUpTime-*.* -d -14 -c “cmd /c del /q @path”

Advertisements

2 thoughts on “Windows – Delete Old Files

  1. Thanks just what I needed.
    Delete all c:\inetpub\wwwroot\ftpsap\FILES*_.TXT older than 30 days…
    forfiles -p c:\Inetpub\wwwroot\ftpsap /D -30 /M FILES_*.TXT /C “cmd /c del @path”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s