Solved

delete folder/files

Posted on 2010-08-12
8
782 Views
Last Modified: 2012-05-10
I want to delete everything that is older than 30 days.

Will this work??

forfiles -p "C:\what\ever" -s -m *.* -d <30> -c "cmd /c rmdir @path /s"
0
Comment
Question by:alisafia
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 3

Expert Comment

by:superggg
ID: 33420964
try the following command to automatically delete files at the root of a directory. This command can be saved in a .bat file and scheduled with scheduled tasks.

forfiles /p "PATH" /s /d -30 /c "cmd /c del @file : date >= 30 days >NUL"

Change "PATH" with the path where you want the files to be deleted. If there are no spaces in the path you dont need the "-symbol.
0
 
LVL 1

Author Comment

by:alisafia
ID: 33421073
Will this delete folders and files in them?
0
 
LVL 3

Accepted Solution

by:
superggg earned 500 total points
ID: 33421298
Forfiles searches from the Top down, so a tree with just empty folders would end up having only the lowest level folder removed.
So, let's try this, use Forfiles to first get a list of folders older than 30 days, and create a batch file with the RD commands in it. Reverse sort that to start at the lowest level folder in each tree.
Then delete the files
Then run the batch file
Then clean up.
--------------------------
Set _tmpfl=%temp%\~rdfldr
Set _Source=C:\Test
If Exist "%_tmpfl%.*" Del "%_tmpfl%.*"
Forfiles /p "%_Source%" /S /D -30 /C "Cmd /C If @isdir==TRUE Echo @RD @Path 2^>Nul" >"%_tmpfl%.bat"
Forfiles /P "%_Source%" /S /D -30 /C "Cmd /C If @isdir==FALSE Del @Path"
Sort /R "%_tmpfl%.bat" /O "%_tmpfl%.cmd"
"%_tmpfl%.cmd"
If Exist "%_tmpfl%.*" Del "%_tmpfl%.*"  
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 53

Expert Comment

by:Bill Prew
ID: 33421477
If you don't mind a small free utility approach, this is a great tool I use frequently for this type of task:

http://home.mnet-online.de/horst.muc/win/delage.htm

http://home.mnet-online.de/horst.muc/wbat32.htm

~bp
0
 
LVL 1

Author Comment

by:alisafia
ID: 33421539
I could use a free tool, but this tool will not delete folders.

"Delage32 was not designed to delete directory structures depending on the age of the folder, regardless of the age of the files inside."

I need to delete folders and files longer than 30 days.
0
 
LVL 3

Expert Comment

by:superggg
ID: 33421638
Here is a free util that will delete folders
 
http://winadmin.forret.com/scripts/warmzip/
 
0
 
LVL 11

Expert Comment

by:Ben Personick
ID: 33422605


Just use this to delete any FOLDER that is a sub directory of the folder you specify:

(there is a slight difference if you use it in a batch or type it in the command prompt so I will list both here)

Command Line:

FOR /F %D IN ('DIR /A:D /B "C:\what\ever" ') DO RMDir /S /D "%D"

BATCH FILE:

FOR /F %%D IN ('DIR /A:D /B "C:\what\ever" ') DO RMDir /S /D "%%D"
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 33424347
==> I need to delete folders and files longer than 30 days.

Can we get a little more specific about what you want?  

If any file is older than 30 days, that's easy, we want to delete that.

But in the case where a folder is older than 30 days (and can you clarify, do you mean created date, or modified date?), then what should happen next.  Is that folder deleted, including all subfolders and files irregardless of the dates on those subfolders and files?  Or should the subfolder only be deleted if it is empty after removing all files older than 30 days?  Etc...

~bp
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question