I was in a need to automatically clean /delete temporary application files older then 30 or 90 days.
To achieve this I used the Windows Task scheduler and the windows ForFiles command.
The ForFiles command selects and executes a command on a file or set of files. This command is useful for batch processing.
More information : Forfiles | Microsoft Learn
Start Task Scheduler
Change the username to NT AUTHORITY/SYSTEM
The task will now run as SYSTEM user. The SYSTEM user has full access on the local machine only.
You’re not able to change whether the user is logged on or not because the task will always runs whether a user is logged on or not.
Create New Trigger
Create New Action
Fill in the fields as above.
Action Start a program
I used these commands :
Program / script | ForFiles |
Add arguments (download folder) | /P “C:\sam\download” /D -90 /C “cmd /c if @isdir==FALSE del /F /Q @file” |
/S /P “C:\sam\upload” /D -90 /C “cmd /c del /F /Q @file” |
Choose the desired settings at the settings tab.
Click OK to save your taks.
Some Examples :
# Delete files in root directory and not in subdirectories
ForFiles /P "C:\Temp\forfiles-test" /C "cmd /c if @isdir==FALSE del /F /Q @file"
# Delete files older then 30 days in root directory and not in subdirectories
ForFiles /P "C:\Temp\forfiles-test" /D -30 /C "cmd /c if @isdir==FALSE del /F /Q @file"
# Delete files older then 90 days in root directory and in subdirectories
ForFiles /S /P "C:\sam\download" /D-90 /C "cmd /c del /F /Q @file"
# Find all files in the directory C:\Temp\forfiles-test that are older than 3 days, and does a command on those files.
ForFiles /P "C:\Temp\forfiles-test\test.txt" /M *.* /D -3 /C "cmd /c dir @FILE"
ForFiles /P "C:\LOGS\" /S /M *.log /D -1 /C "CMD /C del /Q ""@FILE"""qq ~q
ForFiles /S /P C:\Windows\Temp /D -7
/C "cmd /c if @isdir==FALSE del /F /Q @file"
Parameter List:
/P pathname Indicates the path to start searching.
The default folder is the current working
directory (.).
/M searchmask Searches files according to a searchmask.
The default searchmask is '*' .
/S Instructs forfiles to recurse into
subdirectories. Like "DIR /S".
/C command Indicates the command to execute for each file.
Command strings should be wrapped in double
quotes.
The default command is "cmd /c echo @file".
The following variables can be used in the
command string:
@file - returns the name of the file.
@fname - returns the file name without
extension.
@ext - returns only the extension of the
file.
@path - returns the full path of the file.
@relpath - returns the relative path of the
file.
@isdir - returns "TRUE" if a file type is
a directory, and "FALSE" for files.
@fsize - returns the size of the file in
bytes.
@fdate - returns the last modified date of the
file.
@ftime - returns the last modified time of the
file.
To include special characters in the command
line, use the hexadecimal code for the character
in 0xHH format (ex. 0x09 for tab). Internal
CMD.exe commands should be preceded with
"cmd /c".
/D date Selects files with a last modified date greater
than or equal to (+), or less than or equal to
(-), the specified date using the
"MM/dd/yyyy" format; or selects files with a
last modified date greater than or equal to (+)
the current date plus "dd" days, or less than or
equal to (-) the current date minus "dd" days. A
valid "dd" number of days can be any number in
the range of 0 - 32768.
"+" is taken as default sign if not specified.
/? Displays this help message.