Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi all,
I have a sql database, each night i have a batch file run to back this up to a specified folder; C:\Program Files\Microsoft SQL Server\MSSQL$SIMS\BACKUP
This backup is a zip file.
I now have the problem that i have to manually delete old backups. Does anyone have any ideas of how to write a batch file that will delete backups from over 3 weeks ago from that specified folder?
I'm quite stumped.Kind Regards
Steve

Hi,
Here we go again with "date math"; not easy.
If you can live with keeping the most recent, let's say 3, ZIPs, I've got a script you can use.
If at first you don't succeed, you're about average.M2

Thanks for replying,
Would be most greatfull for a script. Is it possible to keep the last 14?- if not, 3 is good.
Thank you.Kind Regards
Steve

This should do it.
You will need to change the second line to accomodate your directory. I used test.
Sine yours has spaces in it, enclose it in double quotes.pushd "C:\Program Files\Microsoft SQL Server\MSSQL$SIMS\BACKUP"
:: keep14z.bat
@echo off
pushd test:loop
dir /b/od *.zip > %TEMP%\templist
set /p file2del=<%TEMP%\templist
call :deleter %file2del%
popd
goto :eof:deleter
dir /a-d *.zip |find "File(s)">%TEMP%\#
for /f "tokens=1" %%T in (%TEMP%\#) do if %%T LEQ 14 goto :eof
del %1
goto :loop:eof
:: DONE
If at first you don't succeed, you're about average.M2

Thank you for that.
Unfortunatly though it will not work. it comes up saying that the specified path cannot be found?? yet the path in there is correct!?!? Any ideas why this would be?
Thank you for your time.Kind Regards
Steve

It is exactly the same as what you sent me. The machine i am testing on has all of the sql data on the c:\
pushd C:\Program Files\Microsoft SQL Server\MSSQL$SIMS\BACKUP
:: keep14z.bat
@echo off
pushd test:loop
dir /b/od *.zip > %TEMP%\templist
set /p file2del=<%TEMP%\templist
call :deleter %file2del%
popd
goto :eof:deleter
dir /a-d *.zip |find "File(s)">%TEMP%\#
for /f "tokens=1" %%T in (%TEMP%\#) do if %%T LEQ 14 goto :eof
del %1
goto :loop:eof
:: DONEForgive my ignorance, this is the first time i've tried scripting!
Kind Regards
Steve

Carefully re-read #3.
You need to REPLACE the second line in my script.
And you need your path enclosed in double quotes.
##################
:: keep14z2.bat
@echo off
pushd "C:\Program Files\Microsoft SQL Server\MSSQL$SIMS\BACKUP":loop
dir /b/od *.zip > %TEMP%\templist
set /p file2del=<%TEMP%\templist
call :deleter %file2del%
popd
goto :eof:deleter
dir /a-d *.zip |find "File(s)">%TEMP%\#
for /f "tokens=1" %%T in (%TEMP%\#) do if %%T LEQ 14 goto :eof
del %1
goto :loop:eof
:: DONE
If at first you don't succeed, you're about average.M2

right, it is now accepting the script, thanks- sorry-i didnt read your previous entry correctly.
When the file runs it loops saying;
Could not find path C:\PROGRAM FILES\MICROSOFT SQL SERVER\MSSQL$SIMS\BACKUP\copy.
i have checked the path and it is definately correct apart from that "\copy" is on the end-does it think that the \copy is part of the file path?
Any ideas?
Thank youKind Regards
Steve

its not accepting certain filenames while deleting can u help me out in this matter.
i want a similiar script for that which deletes the files which are old by more than 3.hi all this is ur friend

Hi monish,
Are you a different person, or the originator with a new name?
If at first you don't succeed, you're about average.M2

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |