2

I have an application that updates a .txt file every 10 minutes. Once a day at the first time the file is updated after 0900 (9am) I want to send an email of that file. The updated file (pointed to by the SET command on line 3) can have a timestamp of anytime between 0900 and 0910.

What I’m proposing to do is run a batch file at 0857 every day that runs for 15 minutes checking the date stamp of the file until the hour becomes 09, then it sends the email and finishes.

In the code extract below to test the function, I’m having a problems with the simple compare statement:

 IF !hour! EQU "09" (GOTO :rundailymail) ELSE (Timeout /T 6). 

Even though (according to echo when I run it), hour is “09”, the compare returns false.

To test it you need to have a file with a timestamp of between 0900 and 0959.

I’m struggling, and have tried lots of things to get this working (and I’ve left some of the diagnostics in there). Any help or advice much appreciated.

@echo on
Setlocal EnableDelayedExpansion
SET filename="D:\Temp Files\test.txt"
IF NOT EXIST %filename% GOTO log
rem echo %filedatetime%

for /l %%x in (1, 1, 20) do (
    FOR %%f IN (%filename%) DO SET filedatetime=%%~tf
echo !filedatetime!
    SET hour= "!filedatetime:~11,-6!" 
Echo !hour!
    IF !hour! EQU "09" (GOTO :rundailymail) ELSE (Timeout /T 60)
)

:failedtofindfile
ECHO "Failed to find the right file timestamp"
goto end


:rundailymail
ECHO "send the daily email" 
goto end

:log
ECHO "FILE MISSING"
goto end

:end
Ross Ridge
  • 38,414
  • 7
  • 81
  • 112
SteveParry
  • 117
  • 1
  • 1
  • 10
  • 3
    You put an extra space after your `set hour=`. That means the space goes onto the beginning of `hour`. – librik Jul 03 '16 at 04:29
  • 2
    You also need quotes around `!hour!` - `if "!hour!" equ "09"` – SomethingDark Jul 03 '16 at 04:30
  • 2
    either use `set hour=value` or better `set "hour=value", no space. A space before `=` goes into the variable name whereas a space after it goes into the value – phuclv Jul 03 '16 at 05:23
  • Mofi, librik, SomethingDark and Lưu Vĩnh PhúcThank you very much for your assistance. Sorry being a bit slow replying - I was expecting instant emails advice when someone added. The corrections you provided work perfectly. Pesky spaces and semantics:-) Thanks again everyone. – SteveParry Jul 04 '16 at 04:49

1 Answers1

3

The batch code without assigning strings with the double quotes to environment variables and enclose instead the variable references where needed in double quotes:

@echo off
setlocal EnableDelayedExpansion
set "FileName=D:\Temp Files\test.txt"
if not exist "%FileName%" goto Log

for /L %%X in (1,1,20) do (
    for %%F in ("%FileName%") do set "FileDateTime=%%~tF"
    echo !FileDateTime!
    set "Hour=!FileDateTime:~11,2!"
    echo !Hour!
    if "!Hour!" == "09" (
        goto RunDailyMail
    ) else (
        timeout /T 60
    )
)

:FailedToFindFile
echo Failed to find the right file timestamp
goto End

:RunDailyMail
echo Send the daily email
goto End

:Log
echo FILE MISSING: %FileName%
goto End

:End
endlocal

The original code failed because of the space character after equal sign on line

SET hour= "!filedatetime:~11,-6!"

which is also assigned to the variable hour like the double quotes.

For that reason the compared strings are  "09" and "09" and the strings are not equal as it can be seen here now.

If the string comparison would be without usage of delayed expansion, the comparison would by chance work because string value of hour would be with the surrounding double quotes and the leading space on standard expansion would be just an additional space between command IF and the double quoted first string resulting in ignoring this extra space. But delayed expansion is required here and therefore the space is not ignored on the string comparison.

Set the answers on

why it is nearly always better to define variables with syntax set "variable=string value" and use double quotes around the variable references where needed.

BTW: The usage of CamelCase spelling for variable names and labels make them easier readable.

Community
  • 1
  • 1
Mofi
  • 46,139
  • 17
  • 80
  • 143