# Solved: Batch file do delete old and different folders



## trukip (May 18, 2011)

I want to write dos batch file to delete old folders in a directory but keep 1 new folder. Here is the sample of my folders under D:\myfolders\ (Jack_20110507, Jack_20110508; James_20110507, James_20110508; Kenny_20110507, Kenny_20110508; ...). I would like to delete the older folders with timestamps *_20110507, but keep the ones with timestamps *_20110508. Your help is really appreciated!


----------



## Squashman (Apr 4, 2003)

Use a For loop to get all the directory names and use the underscore as the delimiter. This will split the directory name into 2 variables. Compare the dates to each other to find the largest date.

After the for loop do another Dir command and pipe that to the findstr command but do an inverse match. You will then have a 2nd pipe to the RMDIR command to delete the older directories.

I think that will work in Theory. I have never tried piping to the rmdir command. If that doesn't work we can always use a 2nd for loop.


----------



## Squashman (Apr 4, 2003)

Now that I think about this I think that this can be simplified alot more than the convoluted stuff I just did. I think we could simplify it to a single for loop.

In the for loop you would do a reverse name sort on the DIR command. This will make the newest directory name come to the top. In your for loop the second variable is your name. Use a set statement to subtract 1 from the date. Then use an IF EXIST command to see if the previous dates folder exists. If it does then delete it.


----------



## Ent (Apr 11, 2009)

As much as I love the concept of automating absolutely everything, this is sounding too complicated. Taking the example as representative of what folders are really there, all this complex code to find out whether there's a newer folder shouldn't be needed? 

Surely a bit more information is in order.

Are all the folders to keep dated to today?
Are all the folders to delete dated to yesterday/any other consistent date?
Are there actually any folders from yesterday or earlier that actually need keeping (because the new one doesn't exist)?


----------



## trukip (May 18, 2011)

Thanks Ent! Your 3 question summaries exactly what I am trying to do. I need to keep current dated folders and delete previous/yesterday folders. And if one was not created, keep one of the old one.


----------



## trukip (May 18, 2011)

Thanks Squashman! I used this script earlier but it was not giving me the correct results: I agree on your logic and not sure if below script could be twicked to accomplished it!

@Echo Off
set SRCDIR=D:\myfolders
set NUM2KEEP=1
If Exist "%temp%\tf}1{" Del "%temp%\tf}1{"
PushD %SRCDIR%
For /F "tokens=* skip=%NUM2KEEP%" %I In ('dir "%SRCDIR%" /AD /B /OD /TW') Do (
If Exist "%temp%\tf}1{" (
Echo %I:%~fI >>"%temp%\tf}1{") 
Else (Echo.>"%temp%\tf}1{"
Echo Following folders will be deleted.>>"%temp%\tf}1{"
Echo Name Path>>"%temp%\tf}1{"Echo %I:%~fI >>"%temp%\tf}1{"))
If Not Exist "%temp%\tf}1{" Echo No Folders Found to delete & Goto Done
Type "%temp%\tf}1{" | More
For /F "tokens=1* skip=3 Delims=:" %I In ('type "%temp%\tf}1{"') Do RD /S /Q "%I"


----------



## Squashman (Apr 4, 2003)

trukip said:


> Thanks Squashman! I used this script earlier but it was not giving me the correct results: I agree on your logic and not sure if below script could be twicked to accomplished it!
> 
> 
> ```
> ...


Because this batch file is going off of the actual Last Written date of the directory not the date in your filename. It will only keep the newest folder in the directory based on the last written date. It doesn't take into account the Name of the folder before the date. Basically if you have 6 folders in your source directory it is going to knock it down to 1 directory. The newest one written to.


----------



## Squashman (Apr 4, 2003)

I just realized this should be pretty darn easy now that I think about it. As long as the directory naming scheme is *Persons' name*, *Underscore* and then the Date this is just a nested for loop and use the skip command in the second for loop.


----------



## Squashman (Apr 4, 2003)

```
@Echo Off & SetLocal EnableExtensions
For /F "tokens=1* delims=_" %%A IN ('dir /b /ad') DO (
	For /F "skip=1 tokens=*" %%I IN ('dir /b /ad /O-N "%%A"*') DO rmdir /S /Q %%I
)
```
output

```
C:\Users\Squashman\batch>dir /ad /b
Jack_20110506
Jack_20110507
Jack_20110508
James_20110507
James_20110508
Kenny_20110507
Kenny_20110508

C:\Users\Squashman\batch>trukip.bat

C:\Users\Squashman\batch>dir /ad /b
Jack_20110508
James_20110508
Kenny_20110508

C:\Users\Squashman\batch>
```


----------



## trukip (May 18, 2011)

Squashman, It worked!! I just had to tweek %%I to "%%I" to capture files with space. Other than that your script is working and I really appreciate your help!! This is solved :up:


----------

