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