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:
  • 542 Vote(s) - 3.54 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sleeping in a batch file

#1
When writing a batch file to automate something on a Windows box, I've needed to pause its execution for several seconds (usually in a test/wait loop, waiting for a process to start). At the time, the best solution I could find uses ping (I kid you not) to achieve the desired effect. I've found a better write-up of it [here][1], which describes a callable "wait.bat", implemented as follows:

@ping 127.0.0.1 -n 2 -w 1000 > nul
@ping 127.0.0.1 -n %1% -w 1000> nul

You can then include calls to wait.bat in your own batch file, passing in the number of seconds to sleep.

[Apparently the Windows 2003 Resource Kit provides a Unix-like sleep command][2] (at last!). In the meantime, for those of us still using Windows XP, Windows 2000 or (sadly) [Windows NT][3], is there a better way?

I modified the `sleep.py` script in the [accepted answer][4], so that it defaults to one second if no arguments are passed on the command line:

<!-- language: python -->

import time, sys

time.sleep(float(sys.argv[1]) if len(sys.argv) > 1 else 1)

[1]:

[To see links please register here]

[2]:

[To see links please register here]

[3]:

[To see links please register here]

[4]:

[To see links please register here]

Reply

#2
You could use the Windows *cscript WSH* layer and this *wait.js* JavaScript file:

if (WScript.Arguments.Count() == 1)
WScript.Sleep(WScript.Arguments(0)*1000);
else
WScript.Echo("Usage: cscript wait.js seconds");
Reply

#3
Just put this in your batch file where you want the wait.

@ping 127.0.0.1 -n 11 -w 1000 > null
Reply

#4
Depending on your compatibility needs, either use `ping`:

ping -n <numberofseconds+1> localhost >nul 2>&1

e.g. to wait 5 seconds, use

ping -n 6 localhost >nul 2>&1

or on Windows 7 or later use `timeout`:

timeout 6 >nul
Reply

#5
Or command line Python, for example, for 6 and a half seconds:

python -c "import time;time.sleep(6.5)"
Reply

#6
The usage of [ping][1] is good, as long as you just want to "wait for a bit". This since you are dependent on other functions underneath, like your network working and the fact that there is nothing answering on 127.0.0.1. ;-) Maybe it is not very likely it fails, but it is not impossible...

If you want to be sure that you are waiting exactly the specified time, you should use the `sleep` functionality (which also have the advantage that it doesn't use CPU power or wait for a network to become ready).

To find an already made executable for sleep is the most convenient way. Just drop it into your Windows folder or any other part of your standard path and it is always available.

Otherwise, if you have a compiling environment you can easily make one yourself.
The `Sleep` function is available in `kernel32.dll`, so you just need to use that one. :-)
For VB / VBA declare the following in the beginning of your source to declare a sleep function:

private Declare Sub Sleep Lib "kernel32" Alias "Sleep" (byval dwMilliseconds as Long)

For C#:

[DllImport("kernel32.dll")]
static extern void Sleep(uint dwMilliseconds);

You'll find here more about this functionality (available since Windows 2000) in *[Sleep function][2]* (MSDN).

In standard C, `sleep()` is included in the standard library and in Microsoft's Visual Studio C the function is named `Sleep()`, if memory serves me. ;-) Those two takes the argument in seconds, not in milliseconds as the two previous declarations.

[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#7
In Notepad, write:

@echo off
set /a WAITTIME=%1+1
PING 127.0.0.1 -n %WAITTIME% > nul
goto:eof

Now save as wait.bat in the folder C:\WINDOWS\System32,
then whenever you want to wait, use:

CALL WAIT.bat <whole number of seconds without quotes>
Reply

#8
I like [Aacini's response][2]. I added to it to handle the day and also enable it to handle [centiseconds][1] (`%TIME%` outputs `H:MM:SS.CC`):

:delay
SET DELAYINPUT=%1
SET /A DAYS=DELAYINPUT/8640000
SET /A DELAYINPUT=DELAYINPUT-(DAYS*864000)

::Get ending centisecond (10 milliseconds)
FOR /F "tokens=1-4 delims=:." %%A IN ("%TIME%") DO SET /A H=%%A, M=1%%B%%100, S=1%%C%%100, X=1%%D%%100, ENDING=((H*60+M)*60+S)*100+X+DELAYINPUT
SET /A DAYS=DAYS+ENDING/8640000
SET /A ENDING=ENDING-(DAYS*864000)

::Wait for such a centisecond
:delay_wait
FOR /F "tokens=1-4 delims=:." %%A IN ("%TIME%") DO SET /A H=%%A, M=1%%B%%100, S=1%%C%%100, X=1%%D%%100, CURRENT=((H*60+M)*60+S)*100+X
IF DEFINED LASTCURRENT IF %CURRENT% LSS %LASTCURRENT% SET /A DAYS=DAYS-1
SET LASTCURRENT=%CURRENT%
IF %CURRENT% LSS %ENDING% GOTO delay_wait
IF %DAYS% GTR 0 GOTO delay_wait
GOTO :EOF


[1]:

[To see links please register here]

"Centisecond"
[2]:

[To see links please register here]

Reply

#9
I have been using this C# sleep program. It might be more convenient for you if C# is your preferred language:

<!-- language: c# -->

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace sleep
{
class Program
{
static void Main(string[] args)
{
if (args.Length == 1)
{
double time = Double.Parse(args[0]);
Thread.Sleep((int)(time*1000));
}
else
{
Console.WriteLine("Usage: sleep <seconds>\nExample: sleep 10");
}
}
}
}

Reply

#10
Using the `ping` method as outlined is how I do it when I can't (or don't want to) add more executables or install any other software.

You should be pinging something that isn't there, and using the `-w` flag so that it fails after that amount of time, not pinging something that _is_ there (like localhost) `-n` times. This allows you to handle time less than a second, and I think it's slightly more accurate.

e.g.

(test that 1.1.1.1 isn't taken)

ECHO Waiting 15 seconds

PING 1.1.1.1 -n 1 -w 15000 > NUL
or
PING -n 15 -w 1000 127.1 >NUL

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

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