5

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?

5 Answers 5

2

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%
4
  • 1
    It may work and I should try that and ofcourse as you said it's not recommended making server busy for unnecessary work.
    – user53864
    Jan 29, 2011 at 12:52
  • 2
    It works but it may be continuously executing the loop statement til 17:30. Isn't it possible like set n=presenttime-17:30 and then sleep n something like that which you could do?
    – user53864
    Jan 31, 2011 at 11:49
  • @user: Most of that type of command take a number of seconds as a parameter. You can do a calculation to get that value (it would be 17:30-presenttime rather than the way you had it, but the syntax would be more complex). Take a look at the timeout command and check the Resource Kits for a sleep command. Jan 31, 2011 at 14:53
  • To prevent high CPU usage, add timeout /T 1 > NUL after :loop
    – tigrou
    Jan 4, 2022 at 16:10
2

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

2
  • will it run at 5:30 if I append schtaskts schedule command in place of wait?.
    – user53864
    Jan 29, 2011 at 12:47
  • will it wait till 5:30 and start schedule command?
    – user53864
    Jan 29, 2011 at 15:29
1

The accepted answer is a helpful start, but there are a couple of problems:

  • %TIME% has a leading space instead of a leading zero before 10:00 (e.g. 9:30). This will result in comparisons like if 9:30 LSS 10:00 which will be false because 9 > 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 timeout to 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).

0

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.

1
  • Actually I don't want to run separately as I have many scheduled tasks and it's a bit confusing when I want to search for a particular task. So I thought of making it a single batch script.
    – user53864
    Jan 29, 2011 at 12:50
0

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.

You must log in to answer this question.

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