Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

delete folder/files

Posted on 2010-08-12
8
Medium Priority
?
795 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 2000 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 57

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 14
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 57

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
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 …
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

688 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