Computing.Net > Forums > Programming > Comparison of strings in batch file

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Comparison of strings in batch file

Reply to Message Icon

Name: Mark_B
Date: July 11, 2007 at 11:35:41 Pacific
OS: Server 2003
CPU/Ram: p4
Product: dell
Comment:

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=%1

rem 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




Sponsored Link
Ads by Google

Response Number 1
Name: tonysathre
Date: July 11, 2007 at 14:51:48 Pacific
Reply:

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


0

Response Number 2
Name: Mechanix2Go
Date: July 12, 2007 at 01:53:03 Pacific
Reply:

"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 EnableDelayedExpansion

for /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



0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


Batch to send a text mess... Print PreviousLine Using ...



Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Comparison of strings in batch file

nt batch to replace string in srt file www.computing.net/answers/programming/nt-batch-to-replace-string-in-srt-file/19260.html

extracting a part of string in Batch File www.computing.net/answers/programming/extracting-a-part-of-string-in-batch-file/18991.html

find and replace string in a file www.computing.net/answers/programming/find-and-replace-string-in-a-file/19987.html