Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Ok, this is making me pull my hair out, and i am sure its going to be something very silly that i have over looked. I am doing a dir of a directory of backup files (.bak) and piping this to a file. I then read each line in, strip off the bits i dont need, and compare the date/time (part of the file name) to determine which is the most recent. Sounds easy eh! here is the example file name and code :
file name format :
"DPBC004_db_200707111248.BAK"i then strip off the first parts using :
for /F "tokens=3* delims=_ " %%i in (c:\scripts\filelistbak.txt) do (CALL :recentfile %%i)then the bit of code for :recentfile is :
:recentfile
rem first split the date from the file name
echo passed parameter [%1]
set date=%1rem strip off the .bak"
set stripped=%date:.bak"=%echo stripped parameter [%stripped%]
echo Bakdate before [%bakdate%]if %stripped% gtr %bakdate% (
echo I am bigger
echo really i am
set bakdate=%stripped%
)
echo bakdate value [%bakdate%]I initilise the variable bakdate to 0 at the top of the script. The first compare works like a charm, and the variable is changed to the appropriate date/time, but on subsquent file names which are more recent (ie the value is greater), this is not picked up, and variable bakdate doesnt change.
As the field i am comparing starts off as a char variable before i strip off the .bak" do i have to change it somehow to make it into a numeric variable in order to do the compare?
I have looked around and i cant seem to find anything, but as i said, am sure its something obscenely easy and simple that i have just missed.
Any help you can give would be greatfully received by me, and my hairdresser who soon wont have anything left to cut.
Regards
Mark

First of all, it's not a good idea to change %date%. Also this part:
echo Bakdate before [%bakdate%]
if %stripped% gtr %bakdate% (
...
)doesn't make sense because %bakdate% isn't declared yet.
"Computer security." — Oxymoron

"Ok, this is making me pull my hair out, and i am sure its going to be something very silly that i have over looked."
Gee, I thought that only happened to me. LOL
I'll spend some time on this but right now I'm not focused.
Is the task to get the name of the latest .bak file which matches the name layout specified?
If so, try this:
::== get latest DPBC004_db_*.bak into var
@echo off
setLocal EnableDelayedExpansionfor /f "tokens=* delims= " %%a in ('dir/s/b/a-d/od DPBC004_db_*.bak') do (
set latest=%%a
)
echo !latest! is the latest bak file
:: done
=====================================
If at first you don't succeed, you're about average.M2

![]() |
Batch to send a text mess...
|
Print PreviousLine Using ...
|

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