Batch file variable comparison does not work

March 9, 2010 at 23:42:13
Specs: Windows XP
Hello everyone.
I am trying to develop a script which would loop through all the files in a directory (WinXP) and detect if a file has changed since the last run of the script.
I am dumping the timestamps into .stmp files then read and compare them.
Everything works perfectly except when it comes to comparing the current and recorded timestamp.
Here is my code

@echo off

SetLocal EnableDelayedExpansion

for /f "tokens=* delims=" %%T in ('dir /b/a-d *.txt') do (

echo %%T

for %%a in (%%T) do set timestamp= "%%~ta"

set /p oldtimestamp= <%%T".stmp"

echo !timestamp!
echo !oldtimestamp!


if NOT !timestamp! == !oldtimestamp! (
echo %%T file changed
)

echo !timestamp! >%%T".stmp"
)

Here is the output
C:\>check.bat
Log.txt
"03/10/2010 11:22 AM"
"03/10/2010 11:22 AM"
Log.txt file changed
timestamp.txt
"03/10/2010 11:04 AM"
"03/10/2010 11:04 AM"
timestamp.txt file chan
Trece.txt
"03/10/2010 11:05 AM"
"03/10/2010 11:05 AM"
Trece.txt file changed

As you can see the two timestamps match, but still the if statement is not able to detect that and gives false positive.

Your help will be highly appreciated.


See More: Batch file variable comparison does not work

Report •


#1
March 10, 2010 at 01:12:20
only guessing, take all spaces out of the IF line:
if NOT !timestamp! == !oldtimestamp! (

to:
if not !timestamp!==!oldtimestamp! (

H.t.h.


Report •

#2
March 10, 2010 at 02:56:17
Check that RoboCopy or XXCOPY do not have such an option. The default XCOPY will probably have not ... although it must be said' you can do this:

XCOPY /D:m-d-y Copies files changed on or after the specified date.
If no date is given, copies only those files whose
source time is newer than the destination time.

So, you cannot put a file here, but if you manage to put the date of the actual file, it WILL work. Note that it is a DATE, not a TIME.


Report •

#3
March 10, 2010 at 03:31:17
Looking at your code:

@echo off
SetLocal EnableDelayedExpansion
for /f "tokens=* delims=" %%T in ('dir /b/a-d *.txt') do (
    echo %%T
    for %%a in (%%T) do	set timestamp= "%%~ta"

No need for another for statement. Just do this:
set timestamp="%%~tT"
In fact, you don't even need quotes.
set timestamp=%%~tT

    set /p oldtimestamp= <%%T".stmp"

Be careful, %%T may contain spaces.
set /p oldtimestamp=<"%%T.stmp"

    echo !timestamp!
    echo !oldtimestamp!

These two lines are obviously there for debugging purposes.
You are wondering why the batch file thought the timestamps
were different, so you printed them out to check if they are the
same. When you do that, you should enclose strings in
quotes.
echo "!timestamp!"
echo "!oldtimestamp!"
This will highlight your problem.

    if NOT !timestamp! == !oldtimestamp! (
        echo %%T file changed 
    )
    echo !timestamp! >%%T".stmp"

And there's your problem! You have a space after the
timestamp that you write to the file, and no space after the
latest timestamp that you check it against.

Report •

Related Solutions

#4
March 15, 2010 at 23:05:08
Thank you very much! Extremely helpful! Works perfect now.

Report •


Ask Question