How can use a CMD batch file to delete all directories except the newest one

Posted on 2011-03-05
Medium Priority
Last Modified: 2013-11-10
I am trying to delete all folders except the newest one. The first part of the script is just for display and testing.  The output is giving the results I am expecting until the last line of output. As far as I can tell there is nothing new on the line that does not work. All its parts have been tested separately above. What happens?  What needs to be done to get the IF statement working?  Or is there a different way to get the same job done?

I am looking both for a working solution and an explation of what is wrong in my logic.


:: EO 03.03.2011.

CALL :_createshare
CALL :_listfiles
CALL :_count_Differential
CALL :_delete_all_but_last_dir
CALL :_deleteshare

:: cmd does not allow modifications without mapping to a drive first
NET USE z: \\\backup\

DIR *Differential* /A:D /B /O:-N

:: if there is more than one file, the others can be deleted
:: plan a) was to use this 
:: I am unable to save the result of this as a variable I can use later
ECHO "the number of backup files is"
DIR *Differential* /A:D /B /O:-N|FIND /C "Differential"

:: just a test of statement
FOR /d %%i in  (DIR *Differential* /A:D /O:-N) DO (ECHO %%i)
:: this is the statement that should do the job but it does not
:: the plan b) was to recount number of folders before each deletion
FOR /d %%i in  (DIR *Differential* /A:D /O:-N) DO (IF (DIR *Differential* /A:D /B /O:-D|FIND /C "Differential") > 1 DO rmdir %%i)




Z:\>DIR *Differential* /A:D /B /O:-N 


Z:\>CALL :_count_Differential 


Z:\>ECHO "the number of backup files is" 
"the number of backup files is"

Z:\>DIR *Differential* /A:D /B /O:-N  | FIND /C "Differential" 


Z:\>CALL :_delete_all_but_last_dir 


Z:\>FOR / %i in (DIR *Differential* /A:D /O:-N) DO (ECHO %i ) 

Z:\>(ECHO DIR ) 

Z:\>(ECHO eero_2011-02-28_12_26_32_(Differential) ) 

Z:\>(ECHO eero_2011-03-05_13_00_37_(Differential) ) 

Z:\>(ECHO eero_2011-03-02_13_00_31_(Differential) ) 

Z:\>(ECHO eero_2011-03-01_13_00_37_(Differential) ) 

Z:\>(ECHO eero_2011-02-06_08_00_33_(Differential) ) 

Z:\>(ECHO eero_2011-02-28_13_00_54_(Differential) ) 

Z:\>(ECHO /A:D ) 

Z:\>(ECHO /O:-N ) 
*Differential* was unexpected at this time.
Z:\>FOR /d %i in  (DIR *Differential* /A:D /O:-N) DO (IF (DIR *Differential* /A:D /B /O:-D|FIND /C "Differential") > 1 DO rmdir %i)

Open in new window

Question by:tangofil
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 71

Accepted Solution

Qlemo earned 2000 total points
ID: 35046482
There are several errors in line 39.

You mixed up the syntax and purpose of FOR /D and FOR /F.
FOR /D is telling that you want to act on folders only (and do that non-recursively, only for one dir level).
FOR /F is telling you want to execute commands, and parse the output.
In your case a simple FOR /D is appropriate:
    FOR /d %%i IN (*Differential*) DO ...
    FOR /F "tokens=*" %%i IN ('dir /b /a:d /o:-n *Differential*') DO ...
The FOR /F dir uses /b to get rid of any header and footer lines - only path and name are displayed, in one contigious string (path is only displayed if you use the /s for subfolders, too).

BTW, you are sorting for the name descending - which is ok if you can rely on the date format used in the folder name to be sortable; e.g.  03 Mar 2010 is not. Better to sort for date (/o:-d).

Further you can't issue a command and try to evaluate the result in IF like you tried.
And FIND does not return the number of lines as result code, only if something is found or not.
Evaluating both is much more complex than that. I would use this approach:
FOR /F "skip=1 delims=" %%D in ('dir /b /o:-d /a:d *Differential*') do rd "%%~D"

Open in new window

It list all interesting folders, sorted by date descending. The "skip=1" skips the first line, which is the newest folder.

Author Comment

ID: 35046778
Excellent solution to my needs.

However, of some reason I do not quite understand if I sort the files by date, they are not always in the order of creation. Perhaps the folder date is date of modification or something similar, and not of creation which I would need. Because I am adding this code to a script I will use the name sorting, because I know that the format of the names will give me the correct answer everytime.

There is one small misreading of my code in your answer:  You are right about "FIND" but in my code it says "Find /C", which does give the count as you  can see in the output included. But it does really not matter, becase the solution you provided does not depend on the use of FIND and IF. You code is more simple and actually works well.

LVL 71

Expert Comment

ID: 35046803
Find /C still will only output the count to stdout. You would need to pipe that, or use it in FOR /F, to have access to the value.

For the sorting issue you might be right - the modification date is used by default. If you add /T:C to your dir command it will use the creation date instead.

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Starting up a Project
Suggested Courses

719 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