How to make a batch file wait?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
MLGsuperGame414
Posts: 54
Joined: 10 Nov 2011 20:40

How to make a batch file wait?

#1 Post by MLGsuperGame414 » 11 Nov 2011 08:08

Hello I wanted to know how I should go about a line of code that causes my batch file to freeze for x amount of seconds. I am extremely new to batch so please elaborate on how it can be done and how it works please and thanks! :mrgreen:

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: How to make a batch file wait?

#2 Post by Ed Dyreen » 11 Nov 2011 09:13


Cat
Posts: 32
Joined: 11 Nov 2011 12:04

Re: How to make a batch file wait?

#3 Post by Cat » 11 Nov 2011 12:07

TIMEOUT /NOBREAK /T X>NUL

Where X is the number of seconds to wait before continuing.

MLGsuperGame414
Posts: 54
Joined: 10 Nov 2011 20:40

Re: How to make a batch file wait?

#4 Post by MLGsuperGame414 » 11 Nov 2011 15:31

Iv tried both of those and haven't had luck with either of them. Here is an example of the second one that didn't work.


@echo off
TIMEOUT /NOBREAK /T 100>NUL
Echo Hello world
TIMEOUT /NOBREAK /T 3>NUL
Echo Cluster Storm is loading.
TIMEOUT /NOBREAK /T .5>NUL
Echo Cluster Storm is loading..
TIMEOUT /NOBREAK /T .5>NUL
Echo Cluster Storm is loading...
TIMEOUT /NOBREAK /T .5>NUL
Echo Cluster Storm is loading.
TIMEOUT /NOBREAK /T .5>NUL
Echo Cluster Storm is loading..
TIMEOUT /NOBREAK /T .5>NUL
Echo Cluster Storm is loading...
TIMEOUT /NOBREAK /T .5>NUL
Echo Cluster Storm is loading.
TIMEOUT /NOBREAK /T .5>NUL
Echo Cluster Storm is loading..
TIMEOUT /NOBREAK /T .5>NUL
Echo Cluster Storm is loading...
TIMEOUT /NOBREAK /T .5>NUL
Echo Cluster Storm is loading.
TIMEOUT /NOBREAK /T .5>NUL
Echo Cluster Storm is loading..
TIMEOUT /NOBREAK /T .5>NUL
Echo Cluster Storm is loading...
TIMEOUT /NOBREAK /T .5>NUL
pause

Ocalabob
Posts: 79
Joined: 24 Dec 2010 12:16
Location: Micanopy Florida

Re: How to make a batch file wait?

#5 Post by Ocalabob » 12 Nov 2011 14:09

Possibly the batch file below will provide examples for your consideration.

Code: Select all

@echo off
setlocal
echo Time out for about 10 seconds WMIC
set a=Start Time Out at %time%
WMIC timezone list /every:10 /repeat:2>nul
set b=End   Time Out at %time%
echo %b%
echo %a%
echo.

::Or replace line four with below for the same results.
::WMIC timezone list /every:5 /repeat:3>nul

echo Time out for about 10 seconds PING
set a=Start Time Out at %time%
ping -n 10 127.0.0.1>nul
set b=End   Time Out at %time%
echo %b%
echo %a%
pause
endlocal


Note that WMIC requires XP or newer.

Best wishes MLGsuperGame414

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to make a batch file wait?

#6 Post by aGerman » 12 Nov 2011 14:47

The most popular way is the PING method. But I don't understand what that "Cluster Storm is loading" stuff is good for :? I will always be happy if a program doesn't waste my time :roll:
However, just for fun:

Code: Select all

@echo off

:: Settings.
set "text=Cluster Storm is loading"
set /a wait=100, dots=3, repeat=5

:: Create BackSpace and CarriageReturn characters.
for /f "tokens=1,3 delims=# " %%a in ('prompt #$H#^&for /f %%b in ^('copy /z "%~f0" nul'^) do %%b# 2^>nul') do set "BsCr=%%a%%b"

echo Please wait.

:: Display the fake waiting.
setlocal enabledelayedexpansion
  for /l %%i in (1,1,%dots%) do set "spaces=!spaces! "
  for /l %%i in (1,1,%repeat%) do (
    <nul set /p "=!BsCr!!text!!spaces!"
    <nul set /p "=!BsCr!!text!"
    for /l %%j in (1,1,%dots%) do (
      <nul set /p "=."
      >nul ping -n 1 -w %wait% 192.0.2.0
    )
  )
  echo(
endlocal

echo(
pause


Regards
aGerman

nitt
Posts: 218
Joined: 22 Apr 2011 02:43

Re: How to make a batch file wait?

#7 Post by nitt » 12 Nov 2011 15:21

MLGsuperGame414 wrote:Hello I wanted to know how I should go about a line of code that causes my batch file to freeze for x amount of seconds. I am extremely new to batch so please elaborate on how it can be done and how it works please and thanks! :mrgreen:


Stop false-leading him, everyone.

Just simply use:

Code: Select all

ping 0 -n 5 > nul


"5" is how long it will wait. Change it to a higher number to make it wake longer, change it to a lower number to make it wait less.
It's like a dekasecond or something.

An example code would be:

Code: Select all

@echo off
set /p num=How long do you want it to wait:
echo Waiting . . .
ping 0 -n %num% > nul
echo Done!
pause

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to make a batch file wait?

#8 Post by aGerman » 12 Nov 2011 16:00

You shouldn't use IP address 0 (or 0.0.0.0) since it could be a valid address (belongs to australia). Use 127.0.0.1 (local host) instead.
-n 5 means that 5 echo-requests will be sent with a delay of 1 second between each of them. For that reason only 4 seconds elapsed.

Code: Select all

@echo off
set /p "num=How long (s) do you want it to wait: "
set /a num+=1
echo Waiting . . .
>nul ping -n %num% 127.0.0.1
echo Done!
pause


Another way is to use the -w [milli seconds] option. In this case you have to make sure to send the request to a nonexisting (reserved) IP address (e.g. 192.0.2.0).

Code: Select all

@echo off
set /p "num=How long (ms) do you want it to wait: "
echo Waiting . . .
>nul ping -n 1 -w %num% 192.0.2.0
echo Done!
pause


However, both methods are incorrect because ping.exe needs some time (~100...300 ms) to load :wink:

Regards
aGerman

MLGsuperGame414
Posts: 54
Joined: 10 Nov 2011 20:40

Re: How to make a batch file wait?

#9 Post by MLGsuperGame414 » 12 Nov 2011 16:40

Well Ill try all of them, thank you guys.

nitt
Posts: 218
Joined: 22 Apr 2011 02:43

Re: How to make a batch file wait?

#10 Post by nitt » 12 Nov 2011 21:19

aGerman wrote:You shouldn't use IP address 0 (or 0.0.0.0) since it could be a valid address (belongs to australia). Use 127.0.0.1


No. I disagree. No one has the IP address 0.0.0.0.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to make a batch file wait?

#11 Post by aGerman » 13 Nov 2011 06:37

You're right nitt. I found that information somewhere in the internet but the ARIN should be more trusted than each other website.

Regards
aGerman

Post Reply