Wait in windows script

Wait in windows script

  • Home
  • News
  • FAQ
  • Search
  • Scripting Languages
    • Batch Files
      • Getting Started
      • Batch Techniques
      • Batch HowTos
      • Commands
      • Command Line Switches
      • Shutdown Commands
      • Short Command Line Tips
      • Admin One-Liners
      • Examples
      • Samples Collections
      • Tools
      • Links
      • Books
      • Challenges
    • C#
      • Getting Started
      • C# Code Snippets
      • Examples
      • Development Software
        • Visual Studio
        • Visual Studio Community Edition
        • Visual Studio Code
      • Books
    • KiXtart
      • Getting Started
      • Examples
      • Links
      • Tools
      • Books
    • Perl
      • Getting Started
      • Examples
      • Links
      • Tools
    • PowerShell
      • Getting Started
      • PowerShell Code Snippets
      • Examples
      • Links
      • Tools
    • Regular Expressions
      • Getting Started
      • Expressions
      • Examples
      • Links
      • Tools
      • Books
    • Rexx
      • Getting Started
      • Examples
      • OS/2 LAN Server
      • Links
      • Tools
      • Books
    • VBScript & WSH
      • Getting Started
      • VBScript Techniques
      • Examples
      • HTA & WSC Examples
      • Links
      • Tools
      • Books
      • Challenges
  • Technologies
    • WMI
      • Getting Started
      • Examples
      • WMI Queries for Hardware
      • Links
      • Tools
      • Books
    • ADSI
      • Getting Started
      • Examples
      • Links
      • Tools
      • Books
    • Silent Installs
      • General
      • Windows Installer
      • Specific Software
      • Software Requirements
      • Hardware Requirements
  • Books
  • Scripting Tools
    • Batch Utilities
    • Compilers
    • Editors
    • Code Generators
    • Regular Expressions
    • Automation Tools
    • VBScript Add-Ons
    • Printing Tools
    • Inventory Tools
    • Shell Extensions
    • File Viewers
    • Backup
    • Security
    • The making Of.
  • Miscellaneous
    • Tweaks
    • Hardware
      • VoltCraft Energy Logger 3500 Configuration
      • A Fast Compact Flash Card Reader
    • Link Speed Test
    • Web Stuff
    • Conversions
    • My Photo Galleries
  • About This Site
    • Disclaimer
    • News
    • FAQ
    • Search
    • What’s New
    • Objective
    • Advertising
    • Privacy Policy
    • Site Policy
    • Credits
    • The Making Of.
    • Contact
    • Failed Mail
    • Donate

To make a batch file wait for a number of seconds there are several options available:

  • PAUSE
  • SLEEP
  • TIMEOUT
  • PING
  • NETSH (Windows XP/Server 2003 only)
  • CHOICE
  • CountDown
  • SystemTrayMessage
  • Other scripting languages
  • Unix ports
Note: Click a script file name to expand and view its source code; click the file name again, or the expanded source code, to hide the source code again.
To view the source code on its own, right-click the file name and choose Open or Open in separate tab or window.

PAUSE

The most obvious way to pause a batch file is of course the PAUSE command. This will stop execution of the batch file until someone presses «any key». Well, almost any key: Ctrl, Shift, NumLock etc. won’t work.
This is fine for interactive use, but sometimes we just want to delay the batch file for a fixed number of seconds, without user interaction.

SLEEP

SLEEP was included in some of the Windows Resource Kits.
It waits for the specified number of seconds and then exits.

will delay execution of the next command by 10 seconds.

Читайте также:  Sensors kxfusion windows 10 драйвер

There are lots of SLEEP clones available, including the ones mentioned in the UNIX Ports paragraph at the end of this page.

TIMEOUT

TIMEOUT was included in some of the Windows Resource Kits, but is a standard command as of Windows 7.
It waits for the specified number of seconds or a keypress, and then exits.
So, unlike SLEEP , TIMEOUT ‘s delay can be «bypassed» by pressing a key.

will delay execution of the next command by 10 seconds, or until a key is pressed, whichever is shorter.

You may not always want to abort the delay with a simple key press, in which case you can use TIMEOUT ‘s optional /NOBREAK switch:

You can still abort the delay, but this requires Ctrl+C instead of just any key, and will raise an ErrorLevel 1.

For any MS-DOS or Windows version with a TCP/IP client, PING can be used to delay execution for a number of seconds.

will delay execution of the next command for (a little over) 5 seconds seconds (default interval between pings is 1 second, the last ping will add only a minimal number of milliseconds to the delay).
So always specify the number of seconds + 1 for the delay.

The PING time-out technique is demonstrated in the following examples:

PMSleep.bat for Windows NT

PMSlpW9x.bat for Windows 95/98

NETSH

NETSH may seem an unlikely choice to generate delays, but it is actually much like using PING :

will ping localhost, which takes about 5 seconds — hence a 5 seconds delay.

NETSH is native in Windows XP Professional and later versions.
Unfortunately however, this trick will only work in Windows XP/Server 2003.

CHOICE

will add a 10 seconds delay.
By using REM | before the CHOICE command, the standard input to CHOICE is blocked, so the only «way out» for CHOICE is the time-out specified by the /T parameter.
The idea was borrowed from Laurence Soucy, I added the /C parameter to make it language independent (the simpler REM | CHOICE /T:N,10 >NUL will work in many but not all languages).

The CHOICE delay technique is demonstrated in the following example, Wait.bat:

Note: The line IF ERRORLEVEL 255 ECHO Invalid parameter ends with an «invisible» BELL character, which is ASCII character 7 (beep) or ^G (Ctrl+G).

CountDown

For longer delay times especially, it would be nice to let the user know what time is left.
That is why I wrote CountDown.exe (in C#): it will count down showing the number of seconds left.
Pressing any key will skip the remainder of the count down, allowing the batch file to continue with the next command.

You may append the counter output to a custom text, like this ( @ECHO OFF required):

Читайте также:  Экзамен windows server administration fundamentals

SystemTrayMessage

SystemTrayMessage.exe is a program I wrote to display a tooltip message in the system tray’s notification area.
By default it starts displaying a tooltip which will be visible for 10 seconds (or any timeout specified), but the program will terminate immediately after starting the tooltip. The icon will remain in the notification area after the timeout elapsed, until the mouse pointer hovers over it.
By using its optional /W switch, the program will wait for the timeout to elapse and then hide the icon before terminating.

Display a tooltip message for 60 seconds while continuing immediately:

Display a tooltip message and wait for 60 seconds:

Or more sophisticated (requires CountDown.exe too):

Non-DOS Scripting

In PowerShell you can use Start-Sleep when you need a time delay.
The delay can be specified either in seconds (default) or in milliseconds.

The following batch code uses PowerShell to generate a delay:

Or if you want to allow fractions of seconds:

Note that starting PowerShell.exe in a batch file may add an extra second to the specified delay.

Use the SysSleep function whenever you need a time delay in Rexx scripts.
SysSleep is available in OS/2’s (native) RexxUtil module and in Patrick McPhee’s RegUtil module for 32-bits Windows.

Use the Sleep command for time delays in KiXtart scripts.

Use WScript.Sleep, followed by the delay in milliseconds in VBScript and JScript (unfortunately, this method is not available in HTAs).

The following batch code uses a temporary VBScript file to generate an accurate delay:

Or if you want to allow the user to skip the delay:

UNIX Ports

Compiled versions of SLEEP are also available in these Unix ports:

Batch (Windows) execute script wait for request and input the answer

the problem: I want to create a batchfile that executes other scripts. Then only problem is, you can not give the necessary input by parameters. The scripts ask for some data along the way. Is there any way to wait for a specific kind of string and answer based on this string?

kinda like VBS in secureCRT: crt.screen.WaitForString(«question»)

Edit: this is not the same as asking input from the user via the console. The batch has to give an answer automatically to the output generated by te launched script.

4 Answers 4

You can pipe or redirect a character or string to a bach script, but it is not possible for the script to wait for a question (input) and give an answer dependent on this. This belongs on the missing multitasking modus of the windows command line.

Can’t be done? Dingo’s kidneys!

Читайте также:  Linux windows 10 подключение флешки

If batch can be used to create the universe as we know it, there’s not much that it can’t do — if you’re flexible.

For instance, here’s a demo that will have a batch wait on the output of two processes — all it needs is a little co-operation.

This sends off two processes, proc1 and proc2 (I’m good at creating imaginative tasknames. ) It then waits until a file procN.finished is created and processes the corresponding procN.output (just puts the output line into envvar PROCn )

When both subsidiary procs are complete, it proceeds — well, to EOF in this case.

Here’s an example of a subsidiary proc I used for testing:

proc1.bat was the same, with a few numbers changed.

Works fine — with the SET displaying the end time of the two subsidiary processses as expected.

All it takes is to poke the appropriate output string into a file and use flag files to notify the controlling batch that the subsidiary processes have finished doing their work.

Of course, the original question is a litle hazy, and I may have it all completely confused.

Let’s analyze your request a little. As I understand it, you want something similar to this:

This way, when output.bat read «do you want to proceed?» from its STDIN, then gives a «yes» answer into scriptA.bat, right? However, there is no way that output.bat could provide any input to scriptA.bat! In order to do that, you must assemble something similar to this:

In this case, when output.bat detects «do you want to proceed?», it must send a signal of some type to input.bat so it answer «yes» into scriptA.bat’s STDIN! Of course, the signal sent from output.bat to input.bat can NOT be carried via a normal data stream, it must be of other type: the existence of a disk file, for example. Although previous scheme theoretically should work, there is another problem here.

When a Batch file executes, it continually polls the keyboard to detect a possible Ctrl-C key in order to cancel the execution. This behavior «eats» the characters that another process may sent via a pipeline. However, this behavior can be bypassed via another signal system so the right side of the pipe read an input as soon as the left side had output to it. At this post there is a Batch file that use this technique to create a Tee.bat program that duplicate its piped input in the screen and in a disk file.

Continuing with our topic, there is no way to solve your problem via a Batch file. However, it is relatively easy to do that in JScript, for example:

Оцените статью