Solved

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

Posted on 2011-03-05
3
958 Views
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.

Thanks!




SCRIPT:
********************************************************
:: EO 03.03.2011.
::ECHO OFF

CALL :_createshare
CALL :_listfiles
CALL :_count_Differential
CALL :_delete_all_but_last_dir
CALL :_deleteshare
GOTO :EOF

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

:_ListFiles
z:
DIR *Differential* /A:D /B /O:-N
GOTO :EOF

:_count_Differential
z:
:: 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"
GOTO :EOF


:_delete_all_but_last_dir
Z:
:: 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)
GOTO :EOF

:_deleteshare
NET USE Z: /DELETE /YES
GOTO :EOF

*********************************

OUTPUT:



Z:\>DIR *Differential* /A:D /B /O:-N 
eero_2011-03-05_13_00_37_(Differential)
eero_2011-03-02_13_00_31_(Differential)
eero_2011-03-01_13_00_37_(Differential)
eero_2011-02-28_13_00_54_(Differential)
eero_2011-02-28_12_26_32_(Differential)
eero_2011-02-06_08_00_33_(Differential)

Z:\>GOTO :EOF 

Z:\>CALL :_count_Differential 

Z:\>z:

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" 
6

Z:\>GOTO :EOF 

Z:\>CALL :_delete_all_but_last_dir 

Z:\>Z:

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

Z:\>(ECHO DIR ) 
DIR

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

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

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

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

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

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

Z:\>(ECHO /A:D ) 
/A:D

Z:\>(ECHO /O:-N ) 
/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

0
Comment
Question by:tangofil
  • 2
3 Comments
 
LVL 68

Accepted Solution

by:
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 ...
or
    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.
0
 

Author Comment

by:tangofil
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.

 
0
 
LVL 68

Expert Comment

by:Qlemo
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.
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

If you get continual lockouts after changing your Active Directory password, there are several possible reasons.  Two of the most common are using other devices to access your email and stored passwords in the credential manager of windows.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This Micro Tutorial will give you a basic overview of Windows DVD Burner through its features and interface. This will be demonstrated using Windows 7 operating system.
The viewer will learn how to successfully download and install the SARDU utility on Windows 7, without downloading adware.

930 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now