If Batch File Modified Date is older than 90 days, delete files

2.3k Views Asked by At

Basically what I'm trying to do is create a batch file and place it in my startup that will use the modified date of the same batch file and see if it is greater or less than 90 days old. If it isn't, nothing happens and life goes on, but if it is, I want it to delete the contents of my downloads folder. I have a little bit of coding, but I've been testing it, but for some reason it isn't wanting to work for me. Any help would be greatly appreciated.

forfiles -p "C:\LOCATION OF .BAT\" -s -m rmdownload.bat /D -90 /C "cmd /c del C:\user folder\Downloads"

I'm just starting out using batch commands and would really love the help

2

There are 2 best solutions below

0
On

This works for you if you are using Win2003 or WinXP and have forfiles.exe installed on your machine...

forfiles -p "C:\path_of_your_bat" -s -m rmdownload.bat -d -90 -c "cmd /c del C:\user folder\Downloads"

Later versions of Windows and Windows Server have it installed by default.

For Win7 or higher: Syntax has changed a little therefore the updated command is:

forfiles -p "C:\path_of_your_bat" -s -m rmdownload.bat /D -90 /C "cmd /c del C:\user folder\Downloads"
0
On

Assuming your target path contains spaces etc, then use this in your forfiles command. The 0x22 represents a double quote character.

If it echos the command then try it without the echo to actually perform the deletion

"cmd /c echo del 0x22C:\user folder\Downloads\*.*?0x22"