|
Answer» I have been given the job of creating a script to call five scripts called Monday to friday on a domain controller. So instead of five tasks in windows backup i create ONE script that calls one of the other day scripts i suppose it would go along like this.
If monday, run Monday script If Tuesday, run tuesday script.
And so on, is there an easy way to get this happening. Or would i be better of just creating 5 task in windows backup to run the individual scripts.
Im not a script person but i know the very basics.
Thanks for any helpThere are 2 ways to achieve this that I can think of.
#1 - scheduled task to trigger the BATCH to execute, using the built in task scheduler. http://technet.microsoft.com/en-us/library/cc748993.aspx
#2 - create a custom service ( requires adequate programming experience to create a custom service ) that triggers at say 12:01am and it calls to the batch file to execute. Then the service sits idle until the next day and it triggers the batch associated with what day it is.here is how you check day in batch, in case you only want it to run while your script is running.
if 'echo %date%' outputs Code: [Select]10/16/2013 Wed we can count over to where the day of the week starts: Code: [Select]1 0 / 1 6 / 2 0 1 3 _ W e d 1 2 3 4 5 6 7 8 9 10 11 12
This means that the 12th character of the output of %date% is W. Using this, we can tell %date% to start on the 11th character (12-1 because we want it to display the W) and output everything after that. To do this we use the syntax "%date:~11%".
On an off topic, we can add a stopping point by using %date:~11,1%. This will start on the 11th character, and display only 1 character.
Another (complicated) way we can do this is with 'wmic' and 'for /f'. Code: [Select]for /f "tokens=2 delims==" %%A in ('wmic path win32_localtime get dayofweek /VALUE') do set dayofWeek=%%A 1 = Monday ... 7 = Sunday
(This method may not work. It depends on your system.)I'd say it's best to use Windows built in scheduling function. In my experience it's just been more stable and easier to maintain that way. However if you didd want to go batch for a particular reason, I'd go with Lemonilla's first recommendation and do something like:
Code: [Select]Set day="%date:~11,1% Call %day% Exit
Then build each of your day scripts.
You can also just build each od your day scripts into the main script as their own section and replace Code: [Select]call %day% with Code: [Select]call:%day%. I could be off on the call syntax for that... Somehow I always forget that. :/ it would also probably be wise to put an ERRORLEVEL check in there too after the CALL... Just in case.
However you decide to do it, you kinda have to use the scheduler for at least the start script... Unless you left it running constantly and just pinging or waiting for nothing... Or looping in a FOR statement... None of which are ideal or stable.
So yeah... My advice, if you want to/have to go with .BAT, use the above method and build everything into a single script.This is a straight FORWARD way of doing it, but depends on the %date% variable format.
When I type this I get the following: C:\>echo %date% Thu 17/10/2013
So this batch file checks for the day name and launches the appropriate batch file.
Code: [Select]@echo off echo %date%|find "Mon" >nul && "monday.bat" echo %date%|find "Tue" >nul && "tuesday.bat" echo %date%|find "Wed" >nul && "wednesday.bat" echo %date%|find "Thu" >nul && "thursday.bat" echo %date%|find "Fri" >nul && "friday.bat" Quote from: foxidrive on October 17, 2013, 01:15:13 AM This is a straight forward way of doing it, but depends on the %date% variable format.
When I type this I get the following: C:\>echo %date% Thu 17/10/2013
So this batch file checks for the day name and launches the appropriate batch file.
Code: [Select]@echo off echo %date%|find "Mon" >nul && "monday.bat" echo %date%|find "Tue" >nul && "tuesday.bat" echo %date%|find "Wed" >nul && "wednesday.bat" echo %date%|find "Thu" >nul && "thursday.bat" echo %date%|find "Fri" >nul && "friday.bat"
Also a workable method!
|