Solved

delete folder/files

Posted on 2010-08-12
8
773 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
 
LVL 51

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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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 51

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now