[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Using FORFILES to delete subdirectories from target directory

Posted on 2011-05-02
Medium Priority
Last Modified: 2012-05-11
Given a target directory, C:\TEST with 2 sub-directories;
Using a command such as
FORFILES -pC:\Test -m*.* -s -d-30 -c"CMD /C del /q @FILE"
I am able to delete all files within the 2 sub-directories which satisfy the age parameter.

Instead of processing the files within the sub-directories, is there a way to process the  sub-folders and accordingly delete the sub-folders given an age parameter is met?
In other words, deletion is by sub-folders, regardless of age of the files within.
I have tried removing the recurse sub-directory switch, -s . But it does not seem to help.

Any suggestion from an Expert would be greatly appreciated.
Question by:garychu
  • 2
LVL 14

Expert Comment

ID: 35512928
Try this in a test pc

FORFILES -pC:\Test -m*.* -s -d-30 -c"CMD /C if +@ISDIR+==+TRUE+ (del /Q @FILE &  rd /S /Q @FILE)"

Bye Gastone
LVL 59

Expert Comment

by:Bill Prew
ID: 35513474
I don't think we need the DEL in the processing.  I also think you will need to use @PATH rather than @FILE.  So something like:

FORFILES -pC:\Test -m*.* -s -d-30 -c"CMD /C if @ISDIR==TRUE rd /s /q @PATH"

Open in new window

LVL 14

Accepted Solution

canali earned 2000 total points
ID: 35517953
Right DEL is not necessary, but using @PATH delete too much... (I'm using FORFILES v 1.1 - by Emmanuel Boersma - 4/98)
So something like should be ok:

FILES -pC:\Test -m*.* -s -d-30 -c"CMD /C if @ISDIR==TRUE rd /s /q @FILE"

Open in new window


Author Comment

ID: 35617724
Thank you very much indeed!
FORFILES -pC:\Test -m*.* -s -d-30 -c"CMD /C if @ISDIR==TRUE rd /s /q @FILE"
works exactly as I had wanted.
Have run tests using various combinations of Folder and File dates to confirm.
Using @PATH instead of @FILE would delete everything regardless.
Thanks again for the inputs.

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

872 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