DOS Batch to determine last modify date for backups and delete files older.

Experts,

We're about to officially kick off our Symantec BackupExec migration accross our enterprise.  One unfortunate side-effect is that when I run the scripts to import the previous B2D media into our "media sets" - it resets the modify date on all of them and it messes with our retention policies.

So, here's what I'm thinking to get around this particular problem.

I'd like to just run a batch file that reads the "modify" date/time of the *.bkf files, determine the last date it was run, and then just completely nuke any files that are older than the last successful backup.  I *could* just go to town and delete ALL backup files on disk, as we also do backups to tape - but I'd like to consider this an exercise to see if I can't pull it off *with your guys' help of course*.

Here's what I'm working with so far, but I realize it's probably WAY off base.  If somebody could point me in the right direction, it would be really appreciated.  (This batch was copied from a site a while back, and I apologize in I can't remember for the life of me which one).

@echo off
set strDir="F:\B2D"
set strModDate=
pushd "%strDir%"
for /f "tokens=*" %%a in ('dir /b /od 2^>NUL') do set strModDate=%%a
if "%strModDate%"=="" goto :eof

Open in new window

LVL 5
usslindstromAsked:
Who is Participating?
 
Paul TomasiConnect With a Mentor Commented:
bill

A simple "%%~fA" would have sufficed - like this:

Oh, and don't forget the '2^>nul' to suppress the error messege if there are no files in the source folder. Also, I thought it might be prudent to specify the filespec '*.BKF' as well although opinions on this will vary.

echo off
for /f "skip=1 tokens=*" %%A in ('dir /a-d /b /o-d "F:\B2D\*.BKF" 2^>nul') do del "%%~fA"

Open in new window

BTW, I also considered the 'skip=1' route however, I didn't include it here because I favoured the code would allow scope to use the filename for something else later on....
0
 
OGrueCommented:
I'm using a small additional program for such tasks.
delold.exe
Works like charm ;-)
0
 
usslindstromAuthor Commented:
Found the reference to where I pulled the original script:

Credit goes to the "Devil_Himself"  <---  Nice username.  :)  - from this website:
http://www.computing.net/answers/programming/batch-to-check-last-modified-file/16351.html

-Just making sure I give credit where it's due.
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
usslindstromAuthor Commented:
That little utility looks perfect, but I'm still at the same spot with it - in that I'd need for the batch that calls it to dynamically figure out when the last successful .bkf was created, and delete anything prior.
0
 
ReneGeConnect With a Mentor Commented:
Try this

@echo off
set strDir="F:\B2D"
set strModDate=
pushd "%strDir%"
for /f "tokens=*" %%a in ('dir /b /od 2^>NUL') do set strModDate=%%~ta
if "%strModDate%"=="" goto :eof

Open in new window

0
 
ReneGeCommented:
If you have no special purpose for "2^>NUL", I would change line 5 to:
for /f "tokens=*" %%a in ('dir /b /od') do set strModDate=%%~ta

Cheers,
Rene
0
 
Paul TomasiConnect With a Mentor Commented:
Best to do it like this:

@echo off
set "file="
for /f "tokens=*" %%a in ('dir /a-d /o-d /tw "F:\B2D\*.bkf" 2^>nul') do (
  if not defined file (
    set file=%%a
  ) else (
    del "%%~fa"
  )
)

Open in new window

0
 
OGrueConnect With a Mentor Commented:
Maybe paultomasi solution works better - i've tried it this way:
@echo off
cd /d %0\..
if "%1"=="" goto error
cd "%1"
for /f "tokens=*" %%a in ('dir /b /od 2^>NUL') do set lastFile=%%a
attrib "%lastFile%" +h
del *.* /A-h /Q
attrib "%lastFile%" -h
goto eof

:error
echo No Path defined
:eof

Open in new window

Last date file gets attribute hidden, all files except hidden are deletet, attribute hidden is removed.
Insertet the Path as variable (batch.bat C:\folder\folder) to prevent issues if the batch lies within the directory with files which should be deletet.
0
 
Bill PrewCommented:
Have you tried making the files read only with the ATTRIB commnad before the backup runs?  That might prevent the utility from updating their modified date, but not sure.  Then you could keep as many copies as you want.

Of course another approach is to move the file out to a different subfolder each day after it's created, or right before the script runs.  Then you can again keep as many or as few archives as you want there.  I'm not big on only having one copy of the backup, typically like to have several.

Just some alternate thoughts...

~bp
0
 
usslindstromAuthor Commented:
Wow - your guys suggestions are all perfect.  Give me a minute to take it all in and try to understand what you're all showing me.
0
 
ReneGeCommented:
I finally had the time to carefully read your question and the following comments.  I see that my solution is not what you need, and you are in good hands with my fellow experts.

Cheers,
Rene
0
 
Bill PrewConnect With a Mentor Commented:
And if you really did want to just delete all but the 1 latest files, I think this would be a fine way to do that:

echo off
set BaseDir=F:\B2D
for /f "skip=1 tokens=*" %%A in ('dir /a:-d-s /b /o:-d "%BaseDir%\"') do del "%BaseDir%\%%~A"

Open in new window

~bp
0
 
usslindstromAuthor Commented:
Guys, I've VERY sorry for the late reply here.

Thank you all for giving your ideas on how to make this work.  I haven't had a chance to implement them, but after reading your scripts and then explinations of everything, I understand how I'm going to do this.

MUCH appreciated on everything.  Again, sorry for the late answer here.  (As soon as you guys get to the point of implementing VTC systems at your work, you'll see that every tiny little thing that comes up is a class 5 emergency, even if it's as simple as users needing to raise the volume on their local unit - but it's that sort of "fun" that's kept me from getting back to this EE Question in the first place).
0
 
Paul TomasiCommented:
Thank you gladly.
0
 
Bill PrewCommented:
Welcome.

~bp
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.