Using FORFILES to delete subdirectories from target directory

Posted on 2011-05-02
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
    LVL 14

    Expert Comment

    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 51

    Expert Comment

    by:Bill Prew
    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

    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

    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

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    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 (…
    VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    732 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

    17 Experts available now in Live!

    Get 1:1 Help Now