Advertisement

There's no such thing as a stupid question, but they're the easiest to answer.
Login
Search

Advertisement

DOS/Other DOS/Other
Search Search
Search for:
Tech Support Guy > > >

Solved: Delete multiple folders using a batch file


(!)

haishailu's Avatar
haishailu haishailu is offline
Member with 4 posts.
THREAD STARTER
 
Join Date: Jun 2012
Experience: Intermediate
09-Jun-2012, 07:57 AM #1
Solved: Delete multiple folders using a batch file
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's Avatar
Trusted Advisor with 19,648 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
09-Jun-2012, 05:57 PM #2
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's Avatar
haishailu haishailu is offline
Member with 4 posts.
THREAD STARTER
 
Join Date: Jun 2012
Experience: Intermediate
11-Jun-2012, 03:47 AM #3
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's Avatar
Trusted Advisor with 19,648 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
11-Jun-2012, 08:34 AM #4
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's Avatar
haishailu haishailu is offline
Member with 4 posts.
THREAD STARTER
 
Join Date: Jun 2012
Experience: Intermediate
11-Jun-2012, 09:08 AM #5
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's Avatar
Trusted Advisor with 19,648 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
11-Jun-2012, 09:42 AM #6
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.

  /A          Displays files with specified attributes.
  attributes   D  Directories                R  Read-only files
               H  Hidden files               A  Files ready for archiving
               S  System files               -  Prefix meaning not
  /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.
  /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    -  Prefix to reverse order
  /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"
haishailu's Avatar
haishailu haishailu is offline
Member with 4 posts.
THREAD STARTER
 
Join Date: Jun 2012
Experience: Intermediate
11-Jun-2012, 11:28 AM #7
Yippppeeee. It works. Thanks for the help.

Saves a lot of manual effort for me.
Squashman's Avatar
Trusted Advisor with 19,648 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
11-Jun-2012, 12:01 PM #8
Quote:
Originally Posted by haishailu View Post
Yippppeeee. It works.
But, do you know how it works?
As Seen On

BBC, Reader's Digest, PC Magazine, Today Show, Money Magazine
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.


(clock)
THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


WELCOME
You Are Using: Server ID
Trusted Website Back to the Top ↑