1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: Delete multiple folders using a batch file

Discussion in 'DOS/Other' started by haishailu, Jun 9, 2012.

Thread Status:
Not open for further replies.
Advertisement
  1. haishailu

    haishailu Thread Starter

    Joined:
    Jun 9, 2012
    Messages:
    4
    I have multiple folders that gets generated frequently, something like below.

    20120608.018
    20120608.019
    20120608.020
    20120608.021
    20120608.022

    I am looking for a batch file that gets the latest folder list and deletes all the folders except the latest one 20120608.022.

    Apparently I want to delete the old folder once a new one is generated.
     
  2. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,731
    Well you could in theory do this two different ways. If each of those folders is created sequentially then the newest folder technically has the newest date. So you could in theory use the DIR command to list the folders by date and tell it to list the newest first. You can use the SKIP option in a FOR loop to skip the first directory and then have it remove all others.

    You can also go off of the actual file name and do a reverse sort on that as well because you are using a YYYYMMDD.### format they will sort perfectly alphanumerically. So again you can put the DIR command inside a FOR loop and tell the DIR command to sort by filename in reverse order.
     
  3. haishailu

    haishailu Thread Starter

    Joined:
    Jun 9, 2012
    Messages:
    4
    Thanks for the quick reply.. I did some trials and below is what i am currently but this does not give me the result i am looking at. Please help.

    @echoff
    Pushd <Path>
    FOR /F "skip=1 delims=" %%a IN ('DIR 2*.* /D') DO RD /S /Q "%%a"
     
  4. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,731
    That is because the /D switch is for WIDE FORMAT.
    You need to use the /O switch with the D option and use the minus sign to reverse the sort order. By default the D option will sort OLDEST first if you read the help file. You want the newest first so that the SKIP option will not process the newest folder. You also need to use the /A switch to list directories only and the /B switch for the BARE format.
     
  5. haishailu

    haishailu Thread Starter

    Joined:
    Jun 9, 2012
    Messages:
    4
    I tried below, is the syntax correct?

    FOR /F "skip = -1 delims=" %%a IN ('DIR 2*.* /OD /B') DO RD /S /Q "%%a"

    Please help me with the syntax or an example.
     
  6. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,731
    Not sure how you extrapolated from my comments to use SKIP minus 1.

    Read the help for the DIR command
    Code:
    H:\>dir /?
    Displays a list of files and subdirectories in a directory.
    
    DIR [drive:][path][filename] [/A[[:]attributes]] [/B] [/C] [/D] [/L] [/N]
      [/O[[:]sortorder]] [/P] [/Q] [/S] [/T[[:]timefield]] [/W] [/X] [/4]
    
      [drive:][path][filename]
                  Specifies drive, directory, and/or files to list.
    
      [color=red]/A          Displays files with specified attributes.
      attributes   [b]D  Directories[/b]                R  Read-only files
                   H  Hidden files               A  Files ready for archiving
                   S  System files               -  Prefix meaning not[/color]
      /B          Uses bare format (no heading information or summary).
      /C          Display the thousand separator in file sizes.  This is the
                  default.  Use /-C to disable display of separator.
      /D          Same as wide but files are list sorted by column.
      /L          Uses lowercase.
      /N          New long list format where filenames are on the far right.
      [color=red]/O          List by files in sorted order.
      sortorder    N  By name (alphabetic)       S  By size (smallest first)
                   E  By extension (alphabetic)  D  By date/time (oldest first)
                   G  Group directories first    [b]-  Prefix to reverse order[/b][/color]
      /P          Pauses after each screenful of information.
      /Q          Display the owner of the file.
      /S          Displays files in specified directory and all subdirectories.
      /T          Controls which time field displayed or used for sorting
      timefield   C  Creation
                  A  Last Access
                  W  Last Written
      /W          Uses wide list format.
      /X          This displays the short names generated for non-8dot3 file
                  names.  The format is that of /N with the short name inserted
                  before the long name. If no short name is present, blanks are
                  displayed in its place.
      /4          Displays four-digit years
    
    Switches may be preset in the DIRCMD environment variable.  Override
    preset switches by prefixing any switch with - (hyphen)--for example, /-W.
    Please use CODE Tags when posting code
    Code:
    FOR /F "skip=1 delims=" %%a IN ('DIR 2*.* /AD /O-D /B') DO RD /S /Q "%%a"
    Or in my other description using the directory name in reverse order
    Code:
    FOR /F "skip=1 delims=" %%a IN ('DIR 2*.* /AD /O-N /B') DO RD /S /Q "%%a"
     
  7. haishailu

    haishailu Thread Starter

    Joined:
    Jun 9, 2012
    Messages:
    4
    Yippppeeee. It works. Thanks for the help.

    Saves a lot of manual effort for me.
     
  8. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,731
    But, do you know how it works?
     
  9. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1056393