# Solved: Delete multiple folders using a batch file



## haishailu (Jun 9, 2012)

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.


----------



## Squashman (Apr 4, 2003)

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.


----------



## haishailu (Jun 9, 2012)

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"


----------



## Squashman (Apr 4, 2003)

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.


----------



## haishailu (Jun 9, 2012)

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.


----------



## Squashman (Apr 4, 2003)

Not sure how you extrapolated from my comments to use SKIP minus 1.

Read the help for the DIR command

```
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

```
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

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


----------



## haishailu (Jun 9, 2012)

Yippppeeee. It works. Thanks for the help.

Saves a lot of manual effort for me.


----------



## Squashman (Apr 4, 2003)

haishailu said:


> Yippppeeee. It works.


But, do you know how it works?


----------

