I'm trying to create a batch script that will delete all sub directories which are older than 30 days. I'm really new to batch scripting so I'm just hoping that someone can help me out on this.
I was able to find a script that delete all sub directories in specified location.
@for /f "tokens=*" %%a in ('dir /s /b "C:/temp/test" 2^>NUL') do rd /s /q "%%a"
Can someone tweak this script so it deletes only directories which are 30 days old?
I think FORFILES is going to do it...
FORFILES -D 30 will iterate all files last modified over 30 days ago
So, something like...
FORFILES /S /D -30 /C "cmd /c IF @isdir == TRUE rd @path"
Should (untested) recursively remove all folders older than 30 days :)
Btw - Check out http://ss64.com/nt/ for more command line goodness
This is working fine for me:
Posted by Matt Roberts --> FORFILES /S /D -30 /C "cmd /c IF @isdir == TRUE rd @path"
Be careful when you start the ".bat" file with "Run as administrator", it's not using the current location of the file it takes this location --> "C:\Windows\system32"
For savety reasons add the /P "C:\temp"
FORFILES /P C:\temp /S /D -30 /C "cmd /c IF @isdir == TRUE rd @path"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With