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

Posted on 2011-03-05
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 70

Accepted Solution

Qlemo earned 500 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 70

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

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
This Micro Tutorial will give you a introduction in two parts how to utilize Windows Live Movie Maker to its maximum editing capability. This will be demonstrated using Windows Live Movie Maker on Windows 7 operating system.
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…
Suggested Courses

615 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