Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 582 Vote(s) - 3.52 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to sleep for five seconds in a batch file/cmd

#1
Windows's Snipping tool can capture the screen, but sometimes I want to capture the screen after five seconds, such as taking an image being displayed by the webcam. (Run the script and smile at the camera, for example.)

How do I sleep for 5 seconds in a batch file?
Reply

#2
Try the [Choice][1] command. It's been around since MSDOS 6.0, and should do the trick.

Use the /T parameter to specify the timeout in seconds and the /D parameter to specify the default selection and ignore then selected choice.

The one thing that might be an issue is if the user types one of the choice characters before the timeout period elapses. A partial work-around is to obfuscate the situation -- use the /N argument to hide the list of valid choices and only have 1 character in the set of choices so it will be less likely that the user will type a valid choice before the timeout expires.


Below is the help text on Windows Vista. I think it is the same on XP, but look at the help text on an XP computer to verify.

C:\>CHOICE /?

CHOICE [/C choices] [/N] [/CS] [/T timeout /D choice] [/M text]

Description:
This tool allows users to select one item from a list
of choices and returns the index of the selected choice.

Parameter List:
/C choices Specifies the list of choices to be created.
Default list is "YN".

/N Hides the list of choices in the prompt.
The message before the prompt is displayed
and the choices are still enabled.

/CS Enables case-sensitive choices to be selected.
By default, the utility is case-insensitive.

/T timeout The number of seconds to pause before a default
choice is made. Acceptable values are from 0 to
9999. If 0 is specified, there will be no pause
and the default choice is selected.

/D choice Specifies the default choice after nnnn seconds.
Character must be in the set of choices specified
by /C option and must also specify nnnn with /T.

/M text Specifies the message to be displayed before
the prompt. If not specified, the utility
displays only a prompt.

/? Displays this help message.

NOTE:
The ERRORLEVEL environment variable is set to the index of the
key that was selected from the set of choices. The first choice
listed returns a value of 1, the second a value of 2, and so on.
If the user presses a key that is not a valid choice, the tool
sounds a warning beep. If tool detects an error condition,
it returns an ERRORLEVEL value of 255. If the user presses
CTRL+BREAK or CTRL+C, the tool returns an ERRORLEVEL value
of 0. When you use ERRORLEVEL parameters in a batch program, list
them in decreasing order.

Examples:
CHOICE /?
CHOICE /C YNC /M "Press Y for Yes, N for No or C for Cancel."
CHOICE /T 10 /C ync /CS /D y
CHOICE /C ab /M "Select a for option 1 and b for option 2."
CHOICE /C ab /N /M "Select a for option 1 and b for option 2."

[1]:

[To see links please register here]

Reply

#3
If you have an appropriate version of Windows and the [Windows Server 2003 Resource Kit Tools][1], it includes a sleep command for batch programs. More at: [

[To see links please register here]

][2]


[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#4
Well this works if you have choice or ping.

@echo off
echo.
if "%1"=="" goto askq
if "%1"=="/?" goto help
if /i "%1"=="/h" goto help
if %1 GTR 0 if %1 LEQ 9999 if /i "%2"=="/q" set ans1=%1& goto quiet
if %1 GTR 0 if %1 LEQ 9999 set ans1=%1& goto breakout
if %1 LEQ 0 echo %1 is not a valid number & goto help
if not "%1"=="" echo.&echo "%1" is a bad parameter & goto help
goto end

:help
echo SLEEP runs interactively (by itself) or with parameters (sleep # /q )
echo where # is in seconds, ranges from 1 - 9999
echo Use optional parameter /q to suppress standard output
echo or type /h or /? for this help file
echo.
goto end

:askq
set /p ans1=How many seconds to sleep? ^<1-9999^>
echo.
if "%ans1%"=="" goto askq
if %ans1% GTR 0 if %ans1% LEQ 9999 goto breakout
goto askq

:quiet
choice /n /t %ans1% /d n > nul
if errorlevel 1 ping 1.1.1.1 -n 1 -w %ans1%000 > nul
goto end

:breakout
choice /n /t %ans1% /d n > nul
if errorlevel 1 ping 1.1.1.1 -n 1 -w %ans1%000 > nul
echo Slept %ans1% second^(s^)
echo.

:end

just name it sleep.cmd or sleep.bat and run it
Reply

#5
An improvement of the code proposed by the user Aacini, It has resolution of hundredths of a second and does not fail when the time reaches 23:59:59,99:

for /f "tokens=1,2,3,4 delims=:," %%A in ("%TIME%") do set /a HH=%%A, MM=1%%B-100, SS=1%%C-100, CC=1%%D-100, TBASE=((HH*60+MM)*60+SS)*100+CC

:: Example delay 1 seg.
set /a TFIN=%TBASE%+100

:ESPERAR
for /f "tokens=1,2,3,4 delims=:," %%A in ("%TIME%") do set /a HH=%%A, MM=1%%B-100, SS=1%%C-100, CC=1%%D-100, TACTUAL=((HH*60+MM)*60+SS)*100+CC

if %TACTUAL% lss %TBASE% set /a TACTUAL=%TBASE%+%TACTUAL%
if %TACTUAL% lss %TFIN% goto ESPERAR
Reply

#6
I was trying to do this from within an msbuild task, and choice and timeout both did not work due to I/O redirection.

I ended up using sleep.exe from

[To see links please register here]

, which is nice because it doesn't require any install and it's tiny.

----

Trying with `choice`:

<Target Name="TestCmd">
<Exec Command="choice /C YN /D Y /t 5 " />
</Target>

Results in:

TestCmd:
choice /C YN /D Y /t 5

EXEC : error : The file is either empty or does not contain the valid choices. [test.proj]
[Y,N]?
C:\test.proj(5,9): error MSB3073: The command "choice /C YN /D Y /t 5 " exited with code 255.

----

Trying with `timeout`:

<Target Name="TestCmd">
<Exec Command="timeout /t 5 " />
</Target>


Results in:

TestCmd:
timeout /t 5
EXEC : error : Input redirection is not supported, exiting the process immediately. [test.proj]
C:\test.proj(5,7): error MSB3073: The command "timeout /t 5 " exited with code 1.


----

Aside:

I am actually using `<Exec Command="sleep 2 & dbghost.exe" />` because I am executing dbghost.exe multiple times in parallel and it creates temp files/databases based on the current epoch time in seconds - which of course means if you start multiple instances, each uses the same temp name. I was originally trying to use MSBuild Extension Pack `Thread.Sleep` command, but it seems that (usually) it was running the sleep task fine, but then starting the `<exec>` task in all threads at the same time, and of course dbghost.exe would fail with conflicts. So far, using sleep.exe seems to be more reliable.


Reply

#7
I think the following command can help:

pause 5

The syntax of the pause command is:
pause *d* \\\\where d represents the duration in seconds

I am using Windows 7 (32 bit), but I don't know about the others.
Reply

#8
On newer Windows OS versions you can use the command

sleep /w2000

in a DOS script (`.cmd` or `.bat`) to wait for 2s (2000 ms - substitute the time in ms you need). Be careful to include the `/w` argument - without it the whole computer is put to sleep! You can use `-m` instead of `/m` if you wish and optionally a colon (:) between the w and the number.
Reply

#9
You can use [VBScript][1], for example, file `myscript.vbs`:

set wsobject = wscript.createobject("wscript.shell")

do while 1=1
wsobject.run "SnippingTool.exe",0,TRUE
wscript.sleep 3000
loop

Batch file:

cscript myscript.vbs %1

[1]:

[To see links please register here]

Reply

#10
I use the following method entirely based on Windows XP capabilities to do a delay in a batch file:

### File DELAY.BAT

@ECHO OFF
REM DELAY seconds

REM GET ENDING SECOND
FOR /F "TOKENS=1-3 DELIMS=:." %%A IN ("%TIME%") DO SET /A H=%%A, M=1%%B%%100, S=1%%C%%100, ENDING=(H*60+M)*60+S+%1

REM WAIT FOR SUCH A SECOND
:WAIT
FOR /F "TOKENS=1-3 DELIMS=:." %%A IN ("%TIME%") DO SET /A H=%%A, M=1%%B%%100, S=1%%C%%100, CURRENT=(H*60+M)*60+S
IF %CURRENT% LSS %ENDING% GOTO WAIT

You may also insert the day in the calculation so the method also works when the delay interval pass over midnight.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through