Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 797
  • Last Modified:

delete folder/files

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
alisafia
Asked:
alisafia
  • 3
  • 2
  • 2
  • +1
1 Solution
 
supergggCommented:
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
 
alisafiaAuthor Commented:
Will this delete folders and files in them?
0
 
supergggCommented:
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Bill PrewCommented:
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
 
alisafiaAuthor Commented:
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
 
supergggCommented:
Here is a free util that will delete folders
 
http://winadmin.forret.com/scripts/warmzip/
 
0
 
Ben Personick (Previously QCubed)Lead Network EngineerCommented:


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
 
Bill PrewCommented:
==> 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now