# Batch file to delete all but last two dates of .BAK files



## jwoollen (Apr 15, 2009)

*Server 2003 - Application: Legato Backup *
*James Woollen | Systems Engineer | Saddlewest Hotel & Casino | Pahrump, NV*
*email:* *[email protected]*

I found a batch file in a forum that _almost_ does what I need; but, it deletes folders and I need to loop through four folders and delete all but the last two .BAK files. The forum I found the closest solution is: http://forums.techguy.org/dos-pda-other/848854-solved-batch-file-delete-old.html I'll print that code at the end.

My path is: G:\Database_Backups\IVS...and I need to loop through four subfolders:
G:\Database_Backups\IVS\ezPay
G:\Database_Backups\IVS\master
G:\Database_Backups\IVS\model
G:\Database_Backups\IVS\msdb
Within these subfolders are files such as: 
ezPay_db_201008310206.BAK
ezPay_db_201009010206.BAK ...and so forth.

If these files (all but two) are not deleted, the drive fills quickly. At that time the normal backups cannot run. I need to leave the folders...but delete all but two of the files. As I noted before the referred to forum answer is close. I just need to get the files in the folders, (only the .BAK ones) and leave the folder intact.

Your assistance would be greatly appreciated. I made a post a while ago and got a couple of hundred views, but no answer or notice of improper placement or anything. If I'm in the wrong forum, or there is some issue with this post I need to know, please. ([email protected])

Here is the code that worked for the poster who wanted _folders_ deleted, (though it looks like the bottom got cut off):
________________________________ Code from above referenced forum -------------------------------------
@Echo Off
:: User Variables
:: Set this to the number of folders you want to keep
Set _NumtoKeep=2
:: Set this to the folder that contains the folders to check and delete
Set _Path=C:\Backup
If Exist "%temp%\tf}1{" Del "%temp%\tf}1{"
PushD %_Path%
Set _s=%_NumtoKeep%
If %_NumtoKeep%==1 set _s=single
Echo Please wait, searching for folders other than the %_s% most recent
For /F "tokens=* skip=%_NumtoKeep%" %%I In ('dir "%_Path%" /AD /B /O-D /TW') Do (
If Exist "%temp%\tf}1{" (
Echo %%~fI>>"%temp%\tf}1{"
) Else (
Echo.>"%temp%\tf}1{"
Echo Do you wish to delete the following folders?>>"%temp%\tf}1{"
Echo Name>>"%temp%\tf}1{"
Echo %%~fI>>"%temp%\tf}1{"
))
PopD
If Not Exist "%temp%\tf}1{" Echo No Folders Found to delete & Goto _Done
Type "%temp%\tf}1{" | More
Set _rdflag= /q
:_Prompt1
Set /P _resp=Delete All, None, or Prompt for each (A/N/P)?
If /I "%_resp:~0,1%"=="N" Goto _Done
If /I "%_resp:~0,1%"=="A" Goto _Removeold
If /I NOT "%_resp:~0,1%"=="P" (Echo A/N/P only please)&Goto _Prompt1
Set _rdflag=
:_Removeold
For /F "tokens=1* skip=3 Delims=:" %%I In ('type "%temp%\tf}1{"') Do (
If "%_rdflag%"=="" Echo Deleting
rd /s%_rdflag% "%%J")
:_Done
If Exist "%temp%\tf}1{" Del "%temp%\tf}1{"
--------------------- End of what appears in the forum ---------------------------------------
It appears something is missing at the bottom, such as an "exit" or something, but maybe I'm wrong.
Thanks

James Woollen


----------



## TheOutcaste (Aug 8, 2007)

This is easier that doing the folders, give this a try. It will keep the two newest *.bak* files based on date/time modified.
Doesn't present a list like the above, but that can be added if needed.
This will echo the file names it will delete to the screen for testing. To actually delete the files, remove the *@Echo* command in line 7

```
@Echo Off
SetLocal EnableDelayedExpansion
Set _Source=G:\Database_Backups\IVS
For %%I In (ezPay master model msdb) Do (
   PushD "%_Source%\%%I"
   If !Errorlevel!==0 For /F "Tokens=* skip=2 Delims=" %%J In ('Dir /A-D /B /O-D *.bak') Do [B][COLOR=Red]@Echo[/COLOR][/B] Del "%%J"
   PopD
)
```


----------



## TheOutcaste (Aug 8, 2007)

This version will list the files first, and let you delete all, none, or be prompted:

```
@Echo Off
SetLocal EnableDelayedExpansion
:: User Variables
:: Set this to the number of files you want to keep
Set _NumtoKeep=2
:: Parent Folder
Set _Source=G:\Database_Backups\IVS
:: Subfolders to check. Must be quoted if the name contains a space
Set _Folders="ezPay" "master" "model" "msdb"
::::::::::::::::::::::::::::::::::::::::::::::::::::::
::  Do Not Edit below this line
::::::::::::::::::::::::::::::::::::::::::::::::::::::
Set _TFile=%temp%\tf}1{
If Exist "%_TFile%" Del "%_TFile%"
Set _s=%_NumtoKeep%
If %_NumtoKeep%==1 set _s=single
Echo Please wait, searching for files other than the %_s% most recent
For %%I In (%_Folders%) Do (
  PushD "%_Source%\%%~I"
  If !Errorlevel!==0 For /F "Tokens=* skip=2 Delims=" %%J In ('Dir /A-D /B /O-D *.bak') Do (
    If Exist "%_TFile%" (
      Echo %%~fJ>>"%_TFile%"
    ) Else (
      Echo.>"%_TFile%"
      Echo Do you wish to delete the following Files?>>"%_TFile%"
      Echo.            Name>>"%_TFile%"
      Echo %%~fJ>>"%_TFile%"
    ))
   PopD)
If Not Exist "%_TFile%" Echo No Files Found to delete & Goto :EOF
Type "%_TFile%" | More
:_Prompt1
Set _Prompt=A
Set /P _resp=Delete All, None, or Prompt for each (A/N/P)?
If /I "%_resp:~0,1%"=="N" Goto _Done
If /I "%_resp:~0,1%"=="A" Goto _Removeold
If /I NOT "%_resp:~0,1%"=="P" (Echo A/N/P only please)&Goto _Prompt1
Set _Prompt=P
:_Removeold
For /F "tokens=* skip=3 Delims=" %%I In ('type "%_TFile%"') Do (
  If /I %_Prompt%==P (
    Set _resp=
    Set /P _resp=Do you wish to delete %%I ^(Y for yes, any key for no^) 
    If /I "!_resp:~0,1!"=="Y" Del "%%I"
    ) Else Del "%%I")
:_Done
If Exist "%_TFile%" Del "%_TFile%"
```
Edit: And nothing missing at the bottom, the If Exist line is the last line.


----------

