Batch file to delete files by date (Or VBScript?)

R

Ragnar

Hello,

I need to delete SQL server backup files more than 5 days old.
The files are named in the format DBName_YYYYMMDD.bak.

Any example of how to do this in a batch file or a batch file that does
this, would be very helpful.

BTW, is there any advantage in doing this in a batch file as compared to
VBScript?
I can do it easily in VBScript but my batch file coding is very rusty.

Any help with this would be appreciated.
Ragnar
 
J

Jerold Schulman

Hello,

I need to delete SQL server backup files more than 5 days old.
The files are named in the format DBName_YYYYMMDD.bak.

Any example of how to do this in a batch file or a batch file that does
this, would be very helpful.

BTW, is there any advantage in doing this in a batch file as compared to
VBScript?
I can do it easily in VBScript but my batch file coding is very rusty.

Any help with this would be appreciated.
Ragnar
See tip 8293 » How can I return the date that is plus or minus n days from today?
in the 'Tips & Tricks' at http://www.jsifaq.com
See tip 8181 » How can I insure that a year, month, and day contain 4,2, and 2 digits, respectively?

@echo off
setlocal ENABLEDELAYEDEXPANSION
set Folder=C:\SQLBackupFolder
call DatePorM -5 FiveDays
set OLD=%FiveDays:~6,4%%FiveDays:~0,2%%FiveDays:~3,2%
for /f "Tokens=1-4" %%a in ('dir "%folder%\DBName_*.bak" /a /a-d^|find "/"') do (
for /f "Tokens=1-3 Delims=/ " %%i in ('@echo %%a') do (
set MM=%%i
set DD=%%j
set YY=%%k
)
call YYYYMMDD YY MM DD
if "!YY!!MM!!DD!" LSS "%OLD%" @Del /q "%Folder%\%%d"
)
endlocal

Jerold Schulman
Windows Server MVP
JSI, Inc.
http://www.jsiinc.com
http://www.jsifaq.com
 
R

Ragnar Midtskogen

Thank you Jerold,

I have pretty much decided I am going to consolidate several operations and
modify a VB app I already have written for a different purpose.
I need to compress the backup files, rename them by embedding a date string,
then copy them to another server, so I will just use that app to handle the
deletion too.

Ragnar Midtskogen
 
M

Michael Bednarek

I have pretty much decided I am going to consolidate several operations and
modify a VB app I already have written for a different purpose.
I need to compress the backup files, rename them by embedding a date string,
then copy them to another server, so I will just use that app to handle the
deletion too.

I found the problem with developing scripted batch operations using
VB/VBS is their lack of interactivity and difficulty in debugging.
That's why I prefer traditional batch command files.

While recent versions of Microsoft's CMD.EXE feature great improvements
over their predecessors, some of these improvements are syntactically
quite challenging. I decided that my time would be better spent actually
implementing solutions rather than wrestling with CMD's syntax, so I use
4NT as my command line and batch processor.

In your case, deleting files older than five days could look like this:
DEL /[d-5,1/1/1980] DBName_????????.bak
4NT's Date Ranges are documented at
<http://jpsoft.com/help/dateranges.htm>. 4NT is a commercial product;
4DOS, which also knows about Date Ranges (and will run under NT), is
free.

Pure CMD syntax for this operation would be a lot more complicated.
Alternatively, I believe FORFILES.EXE (Resource Kit) can also deal with
date ranges.
 
R

Ragnar Midtskogen

Thank you Michael,

I will take a look at 4NT, it might come in handy somewhere, but I have
decided to go VB. You can debug, even though you have to install VB on the
machine you are running, but I have had good experiences with that.

Ragnar

Michael Bednarek said:
I have pretty much decided I am going to consolidate several operations
and
modify a VB app I already have written for a different purpose.
I need to compress the backup files, rename them by embedding a date
string,
then copy them to another server, so I will just use that app to handle
the
deletion too.

I found the problem with developing scripted batch operations using
VB/VBS is their lack of interactivity and difficulty in debugging.
That's why I prefer traditional batch command files.

While recent versions of Microsoft's CMD.EXE feature great improvements
over their predecessors, some of these improvements are syntactically
quite challenging. I decided that my time would be better spent actually
implementing solutions rather than wrestling with CMD's syntax, so I use
4NT as my command line and batch processor.

In your case, deleting files older than five days could look like this:
DEL /[d-5,1/1/1980] DBName_????????.bak
4NT's Date Ranges are documented at
<http://jpsoft.com/help/dateranges.htm>. 4NT is a commercial product;
4DOS, which also knows about Date Ranges (and will run under NT), is
free.

Pure CMD syntax for this operation would be a lot more complicated.
Alternatively, I believe FORFILES.EXE (Resource Kit) can also deal with
date ranges.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top