Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete sub directories older than 30 days

Tags:

batch-file

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?

like image 539
Marquinio Avatar asked Dec 09 '22 14:12

Marquinio


2 Answers

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

like image 118
Matt Roberts Avatar answered Feb 24 '23 17:02

Matt Roberts


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"
like image 43
user948826 Avatar answered Feb 24 '23 17:02

user948826