I have made a batch script for backup recently. Somewhere in the middle of script I'll have to wait for some time to be reached and then resume the next line of the script. I've scheduled the script at 4:00PM and after the wait command the next line should start at exactly 5:30PM. I thought of using SLEEP command but it's not sure that the commands used before the wait command will end up at certain time(due to inconsistent file sizes) but it's sure that they will be done by 5:00 or 5:10 and next it should execute wait command which waits for certain system clock. I'm checking if there is any command that waits or sleeps until the time specified reaches the system time and resumes there after. Anybody came across this situation and how was that resolved?
- 1,753
- 12
- 38
- 67
5 Answers
You're using two tags, WinXP and Server-2008-R2, so I'm not sure which environment you're running in.
Both environments have a command called schtasks that allows setting up scheduled tasks from the command-line, though the capabilities of this tool varies with the platform. It will allow you to set up a one-time task that will call the remainder of your script. It'll have to be in a separate file, but it'll get executed a single time at 5:30pm.
XP Syntax: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/schtasks.mspx
Server-2008 Syntax: http://technet.microsoft.com/en-us/library/cc725744%28WS.10%29.aspx#BKMK_once
- 135,853
Probably not the best way to do this, but here goes:
:loop
if %TIME% LSS 17:30:00.00 goto loop
echo Continuing at %TIME%
- 64,083
The accepted answer is a helpful start, but there are a couple of problems:
%TIME%has a leading space instead of a leading zero before10:00(e.g.9:30). This will result in comparisons likeif 9:30 LSS 10:00which will be false because9>1.
A simple workaround for this is to use%TIME: =0%to replace the space with a zero.Without any delays, the loop may consume a lot of CPU. So we can use
timeoutto sleep for a second between checks.
Here's a batch file, wait-until.cmd that fixes the problems.
@echo off
:loop
%SYSTEMROOT%\System32\timeout.exe 1 > nul
IF %TIME: =0% LSS %1 GOTO loop
You can call it with e.g. wait-until 09:30.
Because it uses string comparison, you can omit trailing 0 values from your time. So for example, 09:30:00.00 can be reduced to 09:30 (or even 09:3).
- 922
If you want a script that can be run at any time (not just up to the time you specify) - e.g. for something that is supposed to run at a set time every day - you can take @mwfearnley's improved answer (which should be the accepted one) and modify it as follows:
@echo off
:loop
timeout /t 1 > nul
set hhmm=%TIME:~0,5%
if %hhmm: =0% neq %1 goto loop
timeout /t 60
exit
Save as wait-until.cmd and run as follows:
wait-until 09:59
This will exit one minute after the time you specify. This is because we are using the neq operator and have to wait a minute for it to again yield true. exit is there so that if you run it with start /w, you are not waiting infinitely.
- 101
I don't know about any command to wait for a specific time but you might find some Googling for it. We solve similar issues by chopping the batch files into separate parts and scheduling them individually.
- 101