Solved

delete folder/files

Posted on 2010-08-12
8
789 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
LVL 54

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 12
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 54

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

How our DevOps Teams Maximize Uptime

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

Question has a verified solution.

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

Suggested Solutions

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…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

752 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