Some Handy MS DOS Batch Tricks – Infinite Loop and Wait Command

Categories: Tools
Tags: No Tags
Comments: 5 Comments
Published on: October 25, 2009

I am always interested in how to do stuff with Windows command line tools and BATCH scripts to make things easier and to automate processes that are related to the operating system itself or to the file system. I found some interesting tricks and tips in one of the recent issues of my favorite computer magazine from Germany called “CT Magazin” (CT stands for “Computer Technik”) by the publisher “Heise” (which I am still subscribed to here in the United States). I’d like to share those with you, but written in English language. I also included some additional tips and practical examples for using the tricks.

Infinite Loop

The obvious method to do an infinite loop within a BATCH script is the use of “LABELS” and a “GOTO” command within it that jumps to the same label address e.g.

   1: :MYLABEL

   2: … commands to execute here …

   3: GOTO :MYLABEL 

However, this code might ends up to be harder to read and it has the short-coming that it cannot be used from the command line directly and only from within a .BAT Batch script. But there is another way to create an infinite loop via BATCH commands.

   1: for /L %i in (0,0,0) do SINGLE-COMMAND

or if you want to execute a list of commands within the loop:

   1: for /L %i in (0,0,0) do (

   2:   … multiple commands here …

   3: )

where you have to replace “SINGLE-COMMAND” with your own command call or “… multiple commands here …” with the list of multiple commands that you would like to process.

The “0,0,0” stands for “from = 0”, “to = 0”, “step = 0” and is usually used for finite FOR-loops (known as FOR-TO-NEXT Loops in Visual Basic) like “1,10,1” where “from = 1”, “to = 10” and “step = 1”, performing 10 loops and returning the values “1,2,3 … 10” in the Batch-Variable%1” for additional processing.

Delay / Wait for XX seconds

MS DOS Batch does not have an equivalent for commands like “Delay” or “Wait” in Visual Basic, VBScript or JavaScript, but there is a trick that you can use to emulate such a command, getting the same effect.

   1: ping -n XX 127.0.0.1>NUL

where “XX” should be replaced with the number of seconds for the delay.  The command line tool “PING” makes a pause between two ping requests. The number of ping requests can be specified via the “-n” option. By specifying the address “127.0.0.1” (or “localhost”), you only ping your local computer, which should work without any problems, regardless, if you are connected to the Internet or work offline.  Since we don’t care about the actual results that are returned by the ping command, we redirect its output into the “Nirvana” (nothing) via the output-redirect command “>” to “NUL”.

Here is a practical example where you can use this track. This command shows a counter and a message in the window title that the application will complete in X seconds (and close the window, if you executed the script directly and not via command prompt):

   1: FOR /l %%a in (10,-1,1) do (TITLE %title% -- closing in %%as&ping -n 2 -w 1 127.0.0.1>NUL)

   2: TITLE Press any key to close the application&ECHO.

Combining the Two Tricks

The two tricks above can be combined within a single command line that can be called directly from the command-prompt or from within a .BAT Batch script.

   1: for /L %i in (0,0,0) do @YOUR-COMMAND & ping -n XX 127.0.0.1>NUL

Replace “YOUR-COMMAND” with the command to execute and “XX” with the frequency of executing the command in seconds. The @ in front of the command call specifies that the command itself will not be displayed, but only its results.

Free Some Resources by Pausing Services Temporary

To free up some system resources and make them available for other resources hungry processes that you have to run on your computer you can pause some Services that are not needed at the moment. This can be done manually via the “Services” application within your “Administrative Tools” that come with the Windows Operating System, but it is also possible to do this via a command line command, which could be called for multiple Services at once in a single .BAT Batch script. A second .BAT Script could also be created to continue the paused services once you are done. By the way, services that you do not need at all can (and probably also should) be disabled permanently. But this article is not about computer performance optimization and tweaking. There are plenty of articles about that available on the Internet.

To pause a Service call:

   1: sc pause SERVICENAME

and to continue the Service again:

   1: sc continue SERVICENAME

You can use “sc query type= service|more” to see a list of all running services and to get the KEY NAME of them. This query returns a lot of other stuff too and it is not that easy to see quickly, what services are there.

You might want to check out the command line tool “XNET”, which was part of the KIXTART script package up to version 2001 4.23 (download ZIP, 541 KB). The usage is “XNET LIST” to get a nice and easy to read list of ALL “active” services (= excluding DISABLED), with the “short name”, “Description (as much as there is space)” and the current status of it, like “Running”, “Stopped” etc.

Note: You cannot use the SC command to enable or disable any devices that you don’t want/need to use at the moment, for example “Bluetooth’” the “Wireless Network Adapter” when you are connected via regular wired LAN adapter. However, Microsoft made a command-line tool available at their website for download that can be used for this purpose. The tool is called “devcon.exe”.

Additional BATCH Script Related Posts

5 Comments
  1. Anonymous says:

    The -w parameter for ping is the number of milliseconds to wait, not seconds. “ping -w 1 -n 2″ is not two seconds of delay. Also, consider just pinging 1.1.1.1 to ensure that the ping actually takes as long as you want for the delay. Otherwise, the ping of 127.0.0.1 might go faster than the delay you need. The -w option is not the length of time to wait after a successful ping, it is the time to wait until giving up the ping. Suing 1.1.1.1 ensures that the ping program waits for the given amount of time.

  2. Anonymous says:

    I thnk bscripting (biterscripting.com) more better for things these.

    # My script
    while (true)
    do
    # Commands here.
    sleep 2
    done

  3. Greg says:

    To wait for a specified amount of time now you can use timeout:

    timeout [/t] timeout [/nobreak]

  4. Johng363 says:

    I really appreciate this post. I’ve been looking all over for this! Thank goodness I found it on Bing. You’ve made my day! Thanks again! eeeefaggegke

  5. Johne784 says:

    I was looking through some of your blog posts on this internet site and I conceive this web site is rattling informative ! Keep on posting . decfdkgkdedf

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

NOTE! I believe in the right for freedom of speech and personal opinion and are against censorship, so feel free to tell me what you think and let me and others hear your opinion on this subject, but please avoid using the f-word and s-word as much as you possibly can, because at the end of the day this blog exists for the purpose of useful exchanges of thoughts, ideas and opinions and not as a valve for your accumulated anger and frustration. Get a shrink for that! Thanks.

Welcome , today is Thursday, September 29, 2016