Solved

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

Posted on 2011-03-05
3
961 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 69

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 69

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

A quick guide on how to use Group Policy to create a custom power plan and set it active on Windows 7.
On some Windows 7 (SP1) computers, Windows Update becomes super slow even the computer is reasonably fast.  There's one solution that seemed to have worked well for me (after trying a few other suggested solutions).
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

828 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