[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2012-09-06
15
Medium Priority
?
821 Views
Last Modified: 2012-09-20
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

0
Comment
Question by:usslindstrom
  • 4
  • 3
  • 3
  • +2
15 Comments
 
LVL 1

Expert Comment

by:OGrue
ID: 38375216
I'm using a small additional program for such tasks.
delold.exe
Works like charm ;-)
0
 
LVL 5

Author Comment

by:usslindstrom
ID: 38375219
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
 
LVL 5

Author Comment

by:usslindstrom
ID: 38375224
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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
LVL 10

Assisted Solution

by:ReneGe
ReneGe earned 400 total points
ID: 38375603
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
 
LVL 10

Expert Comment

by:ReneGe
ID: 38375621
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
 
LVL 11

Assisted Solution

by:paultomasi
paultomasi earned 800 total points
ID: 38375658
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
 
LVL 1

Assisted Solution

by:OGrue
OGrue earned 400 total points
ID: 38375687
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
 
LVL 59

Expert Comment

by:Bill Prew
ID: 38375757
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
 
LVL 5

Author Comment

by:usslindstrom
ID: 38377436
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
 
LVL 10

Expert Comment

by:ReneGe
ID: 38377652
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
 
LVL 59

Assisted Solution

by:Bill Prew
Bill Prew earned 400 total points
ID: 38377733
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
 
LVL 11

Accepted Solution

by:
paultomasi earned 800 total points
ID: 38378595
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
 
LVL 5

Author Closing Comment

by:usslindstrom
ID: 38420161
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
 
LVL 11

Expert Comment

by:paultomasi
ID: 38420229
Thank you gladly.
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 38420330
Welcome.

~bp
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month17 days, 16 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question