Computer Hope

Microsoft => Microsoft DOS => Topic started by: blastman on February 22, 2008, 03:10:56 AM

Title: help with IF statement
Post by: blastman on February 22, 2008, 03:10:56 AM
Hi guys,

I'm having problems with my if statement....

I want to compare %oldprice% (which is the last modified date of a file in a  certain directory) with %newdateP% (which is the date of the last used file, exported to a txt file in the format "11/02/2008 12:16")

Code: [Select]
REM Searching for latest Graphic file and setting varaible's

set directory="t:\graphic_updates"
cd /d %directory%

FOR /F  %%i IN ('dir /b /a-d /od') DO (
   set lastfileP=%%i
   set newdateP=%%~ti
   )

set /p oldprice=<"T:\log file\price.txt"

if /i "%newdateP%" GTR "%oldprice%" (goto update)

echo.No update to be done
pause

:update
echo.Update to be done.
del "T:\log file\price.txt"
echo.%newdateP%>>"T:\log file\price.txt"
pause

The problem is that no matter what date is in the txt file, it still whats to update.

If change the date in the price.txt to;

"10/02/2008 12:16" - it wants to update
"11/02/2008 12:16" - it wants to update - this is the exacly the same as %newdateP% and I don't want it to update.
"12/02/2008 12:16" - it doesn't want to update


I've tried it will LSS and swapping the varibkles around but i still get the same response.

any ideas???

cheers guys  ;)



Title: Re: help with IF statement
Post by: Sidewinder on February 22, 2008, 06:38:41 AM
It is easier (and more logical) to compare dates in YYYYMMDD format. I would suggest you parse out the day, month and year values and swap them around.

On a more practical note, your code as written will end up in the :update code; either from the goto or by falling into after the pause statement. Try using a goto :eof in place of the pause.

 8)

Title: Re: help with IF statement
Post by: Dias de verano on February 22, 2008, 07:01:06 AM
The GTR, LSS EQU etc tests are arithmetic tests, and give unexpected results when used to compare strings, for example "2" GTR "101" will return true because ASCII string comparisons stop if the first character of either string is greater than the other, and 2 has a higher ASCII code than 1.

Better to parse out the numbers and use set /a to load a numerical variable with e.g. 20080222 which is today's date in yyyymmdd format. Don't bother with hhmmss because batch arithmetic will run out of bits of precision.

Title: Re: help with IF statement
Post by: blastman on February 26, 2008, 07:29:33 AM
The GTR, LSS EQU etc tests are arithmetic tests, and give unexpected results when used to compare strings, for example "2" GTR "101" will return true because ASCII string comparisons stop if the first character of either string is greater than the other, and 2 has a higher ASCII code than 1.

Better to parse out the numbers and use set /a to load a numerical variable with e.g. 20080222 which is today's date in yyyymmdd format. Don't bother with hhmmss because batch arithmetic will run out of bits of precision.

I understand what you mean about those co-op's being numerical so having strange results with stings.

How would I convert the %newdateP% (which is in 11/02/2008 12:16 format) to 11022008 then???

cheers for the help.
Title: Re: help with IF statement
Post by: gpl on February 26, 2008, 07:56:22 AM
Assuming your regional settings are UK format (from your example I think that is safe), use this :

Set Today=%Date:~6,4%%Date:~3,2%%Date:~0,2%

Change Date to newdateP in each of the 3 occurrences above for your code.
Graham
Title: Re: help with IF statement
Post by: blastman on February 26, 2008, 07:59:29 AM
sorry. I'm not sure i get it.

that line you've posted will make a variable 'today' with todays date in the right format. yes??
Title: Re: help with IF statement
Post by: gpl on February 26, 2008, 08:10:33 AM
Sorry, I wasnt as clear as I could have been - I just pasted an example from a bit of code Im working on.

Change your line

Code: [Select]
if /i "%newdateP%" GTR "%oldprice%" (goto update)
to

Code: [Select]
if /i "%newdateP:~6,4%%newdateP:~3,2%%newdateP:~0,2%" GTR "%oldprice:~6,4%%oldprice:~3,2%%oldprice:~0,2%" (goto update)
note - its is just 1 line above, ignore the wrapping on the screen

Graham

Title: Re: help with IF statement
Post by: Dias de verano on February 26, 2008, 08:55:25 AM
gpl has noted correctly that for a numerical date comparison to work, the date number has to be in the format YYYYmmDD with the year first

thus

Quote

yesterday : 20080225
today     : 20080226
tomorrow  : 20080227

Title: Re: help with IF statement
Post by: blastman on February 27, 2008, 02:05:57 AM
OK. I've cheated..... ;)


I have some UNIX tools installed on this machine so I've used 'sed' and 'cut' to take the date from the format the it is currently in and rearrange to the YYYYmmdd format.

Although my if statement still seems to to 'update' even if both variables that it's compairing are the same.  (and no I'm not using GEQ or LEQ co-op's)

I'll post some code. (it bit look abit complex............)
Title: Re: help with IF statement
Post by: blastman on February 27, 2008, 07:15:56 AM
hey,

I managed to get it working!!  ;D

Turns out it was a simple mistake.

nevermind, it's all working now.

cheers for your input guys. :)