PC Review


Reply
Thread Tools Rate Thread

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

 
 
Ragnar
Guest
Posts: n/a
 
      30th Jan 2006
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


 
Reply With Quote
 
 
 
 
Jerold Schulman
Guest
Posts: n/a
 
      31st Jan 2006
On Mon, 30 Jan 2006 17:20:44 -0500, "Ragnar" <(E-Mail Removed)> wrote:

>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
 
Reply With Quote
 
Ragnar Midtskogen
Guest
Posts: n/a
 
      1st Feb 2006
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


 
Reply With Quote
 
Michael Bednarek
Guest
Posts: n/a
 
      2nd Feb 2006
On Tue, 31 Jan 2006 22:12:56 -0500, Ragnar Midtskogen wrote in
microsoft.public.win2000.cmdprompt.admin:

>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.

--
Michael Bednarek http://mbednarek.com/ "POST NO BILLS"
 
Reply With Quote
 
Ragnar Midtskogen
Guest
Posts: n/a
 
      7th Feb 2006
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" <ROT13-(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Tue, 31 Jan 2006 22:12:56 -0500, Ragnar Midtskogen wrote in
> microsoft.public.win2000.cmdprompt.admin:
>
>>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.
>
> --
> Michael Bednarek http://mbednarek.com/ "POST NO BILLS"



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A batch file to delete *.bak files from the Recycle Bin? Bill in Co. Windows XP General 33 11th Oct 2009 10:05 AM
Batch file to delete underscore files. New Orleans Novice Windows XP New Users 12 17th Mar 2009 08:44 PM
Batch file to delete files/folders? Brian Wescombe Windows XP Help 1 1st Oct 2006 11:59 PM
Batch Script Delete File Older 1 Wk Date Modified =?Utf-8?B?Sm9lIEsu?= Windows XP Performance 0 12th Jun 2006 07:38 PM
DOS BATCH FILE TO NAME FILES BY DATE Adrian Moseley Microsoft Windows 2000 CMD Promt 2 10th Aug 2004 01:45 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:29 AM.