Automatically delete files older than 30 days via Task scheduler

Create task

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
Program / scriptForFiles
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”

Change Settings


For Files 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:\Temp\LOGS\" /S /M *.log /D -3 /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"

Leave a Reply

Your email address will not be published. Required fields are marked *