Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
633 views
in Technique[技术] by (71.8m points)

batch file - Delete sub directories older than 30 days

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...