1

I have a scheduled batch script , based on date parameter it should run on Thu . but its not working as expected.

Find the below code.

::@echo off

set LOGFILE=D:\New_Folder\SCRIPTTEST\Rundaily1.log
SET RDAY="%date:~0,3%"

if %RDAY%=="Thu" echo "Starting Weekly Matching" >> %LOGFILE%
D:\New_Folder\SCRIPTTEST\Runthursday.bat

exit /b 0

as per above code , if date/day matches "Thu" it should write the log and execute another batch script .

but even on "Thu" its not writing the log and not running the script. can you please check and help to resolve the same. Please suggest.

1
  • Set echo to on and look real commands in CMD window.
    – Akina
    Sep 27, 2018 at 8:24

2 Answers 2

5

The IF command matches all characters, including the quotes - Thu does not match "Thu".

So either of the following will work:

if %RDAY%==Thu ...
if "%RDAY%=="Thu" ...

EDIT - I just realized you added quotes to the value when you defined RDAY. So my suggestion above in yellow is not quite correct. I prefer to use set "RDAY=%date:~0,3%", which does not add quotes to the value. But that is more a matter of style.

So presumably your IF statement is never true because your machine uses a different format for the %DATE% value that does not start with the day-of- week abbreviation

But there are other problems with your script:

Only your log line is conditional, the Thursday script will run everyday. This is easily fixed by adding parentheses to your IF block

if %DATE:~0,3%==Thu (
  echo "Starting Weekly Matching" >> %LOGFILE%
  D:\New_Folder\SCRIPTTEST\Runthursday.bat
)

The format of the %DATE% value is locale dependent. Your script will likely not work on another machine. You can use WMIC to easily determine if it is Thursday in a locale agnostic way:

wmic path win32_localtime get dayofweek | findstr 4 >nul && (
  echo "Starting Weekly Matching" >> %LOGFILE%
  D:\New_Folder\SCRIPTTEST\Runthursday.bat
)

But I have a question for you - Why are you scheduling a daily batch job that runs another batch only on Thursday? If you want to run a batch job on Thursdays, then use the Windows Task Scheduler to do just that. There is no need to determine if today is Thursday in your batch script, let the scheduler do the work.

2

When going through the script on my system %RDAY% appears to be the value "27" which is the currrent day of month and not the day "Thu" as you are expecting.

I would suggest trying the following:

::@echo off

set LOGFILE=D:\New_Folder\SCRIPTTEST\Rundaily1.log
for /f %%a in ('wmic path win32_localtime get dayofweek /format:list ^| findstr "="') do (set %%a)

if %dayofweek%==4 echo "Starting Weekly Matching" >> %LOGFILE% && D:\New_Folder\SCRIPTTEST\Runthursday.bat

exit /b 0

Found the day of week as numerical value from a stack overflow article (https://stackoverflow.com/questions/11364147/setting-a-windows-batch-file-variable-to-the-day-of-the-week).

I assume the batch file "RunThursday" you only want to be run if Thursday is true, your original approach would have executed the batch file every day as it was outside of the IF statement. Appending your IF statement with "&& " allows for the batch file to be executed as well.

Hopefully this helps.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .