Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
636 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 Reply

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...