Solved

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

Posted on 2011-03-05
3
957 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

OfficeMate Freezes on login or does not load after login credentials are input.
The recent Microsoft changes on update philosophy for Windows pre-10 and their impact on existing WSUS implementations.
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.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

771 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

9 Experts available now in Live!

Get 1:1 Help Now