Page 1 of 1

How to make a batch file wait?

Posted: 11 Nov 2011 08:08
by MLGsuperGame414
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:

Re: How to make a batch file wait?

Posted: 11 Nov 2011 09:13
by Ed Dyreen

Re: How to make a batch file wait?

Posted: 11 Nov 2011 12:07
by Cat
TIMEOUT /NOBREAK /T X>NUL

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

Re: How to make a batch file wait?

Posted: 11 Nov 2011 15:31
by MLGsuperGame414
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

Re: How to make a batch file wait?

Posted: 12 Nov 2011 14:09
by Ocalabob
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

Re: How to make a batch file wait?

Posted: 12 Nov 2011 14:47
by aGerman
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

Re: How to make a batch file wait?

Posted: 12 Nov 2011 15:21
by nitt
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

Re: How to make a batch file wait?

Posted: 12 Nov 2011 16:00
by aGerman
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

Re: How to make a batch file wait?

Posted: 12 Nov 2011 16:40
by MLGsuperGame414
Well Ill try all of them, thank you guys.

Re: How to make a batch file wait?

Posted: 12 Nov 2011 21:19
by nitt
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.

Re: How to make a batch file wait?

Posted: 13 Nov 2011 06:37
by aGerman
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