What can I do, if I want to schedule a task on ,for example, fifth day in month, but not during the weekend. Example: if fifth day is saturday, so task will be scheduled on monday.
Thank you for answers.
Below is a solution I've used in the past for a similar task that I modified a little to potentially help with your need. The trick is to have the Task Scheduler execute a batch script and have the batch script that's executed have the logic in it to do the conditional checks to run or not.
You can use
START to execute executable files (an app), another batch script, execute PowerShell scripts, and other types of scripts. Sometimes the Task Scheduler scheduling isn't robust enough for special scheduling needs as in this case.
Essentially this solution will. . .
Trigger it to run every month on the
7thso if Saturday is the
5th, Sunday is the
6th, then Monday will be the
7thbut even if Sunday is the
6thit'll still run on Monday the
Incorporate a batch script that will run some initial conditional logic that will check for the day of the week and take action based on these conditions whether or not to create a file with the word
Runin it, but if it already has been created with
Runfor that month then
Note: The only thing you really need to adjust with this is to use the correct path in the
SET RunFile=C:\SomeFolder\SomePath\ portion to make it the correct folder or UNC path to create the run file, etc. Then just make the
<Rest of logic needed > just execute or run whatever logic or script the Task Scheduler is running now.
@ECHO ON ::: -- Set date variables for conditional logic for the current date and day of week FOR /F "TOKENS=1 DELIMS=." %%A IN ('WMIC OS GET LocalDateTime ^| FIND "."') DO SET rundt=%%~A SET rundt=%rundt:~0,6% FOR /F "TOKENS=1 DELIMS=." %%A IN ('WMIC PATH Win32_LocalTime GET DayOfWeek ^| FINDSTR /R [0-9]') DO SET DOW=%%~A ::: -- If the day today is 6 for Saturday or 7 for Sunday then EXIT IF %DOW%==6 EXIT IF %DOW%==7 EXIT ::: -- Set the run file location to create a file with "run" in it if the day is correct SET RunFile=C:\SomeFolder\SomePath\%rundt%.run ::: -- If "Run" already found in "~\<YYYYMM>.run" file then do not run because it already has this month FINDSTR /I Run "%RunFile%" IF ERRORLEVEL 1 (GOTO :Run) ELSE (EXIT) :Run ECHO Run>"%RunFile%" <Rest of logic needed > <CALL C:\folder\app.exe > EXIT