{"id":31,"date":"2009-10-25T08:14:00","date_gmt":"2009-10-25T08:14:00","guid":{"rendered":"http:\/\/www.roysac.com\/blog\/wp-admin\/2009\/10\/some-handy-ms-dos-batch-tricks-infinite-loop-and-wait-command\/"},"modified":"2014-07-17T13:42:04","modified_gmt":"2014-07-17T21:42:04","slug":"some-handy-ms-dos-batch-tricks-infinite-loop-and-wait-command","status":"publish","type":"post","link":"http:\/\/www.roysac.com\/blog\/2009\/10\/some-handy-ms-dos-batch-tricks-infinite-loop-and-wait-command\/","title":{"rendered":"Some Handy MS DOS Batch Tricks ??? Infinite Loop and Wait Command"},"content":{"rendered":"<p>I am always interested in how to do stuff with <em>Windows<\/em> command line tools and <em>BATCH<\/em> 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 <em>Germany<\/em> called ???<a href=\"http:\/\/www.ctmagazin.de\/\" target=\"_blank\">CT Magazin<\/a>??? (CT stands for ???Computer Technik???) by the publisher ???Heise??? (which I am still subscribed to here in the<em> United States<\/em>). 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.<\/p>\n<h3>Infinite Loop<\/h3>\n<p>The obvious method to do an infinite loop within a <em>BATCH scr<\/em>ipt is the use of ???<strong><em>LABELS<\/em><\/strong>??? and a ???<strong><em>GOTO<\/em><\/strong>??? command within it that jumps to the same label address e.g.<\/p>\n<div>\n<div>\n<pre>   1: :MYLABEL<\/pre>\n<p><!--CRLF--><\/p>\n<pre>   2: ??? commands to execute here ???<\/pre>\n<p><!--CRLF--><\/p>\n<pre>   3: GOTO :MYLABEL <\/pre>\n<p><!--CRLF--><\/p>\n<\/div>\n<\/div>\n<p>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 <em><strong>.BAT<\/strong> Batch<\/em> script. But there is another way to create an infinite loop via <em>BATCH<\/em> commands.<\/p>\n<div>\n<div>\n<pre>   1: for \/L %i in (0,0,0) do SINGLE-COMMAND<\/pre>\n<p><!--CRLF--><\/p>\n<\/div>\n<\/div>\n<p>or if you want to execute a list of commands within the loop:<\/p>\n<div>\n<div>\n<pre>   1: for \/L %i in (0,0,0) do (<\/pre>\n<p><!--CRLF--><\/p>\n<pre>   2:   ??? multiple commands here ???<\/pre>\n<p><!--CRLF--><\/p>\n<pre>   3: )<\/pre>\n<p><!--CRLF--><\/p>\n<\/div>\n<\/div>\n<p>where you have to replace ???<strong>SINGLE-COMMAND<\/strong>??? with your own command call or ???<strong>??? multiple commands here ???<\/strong>??? with the list of multiple commands that you would like to process.<\/p>\n<p>The ???<strong>0,0,0<\/strong>??? stands for ???<em>from = 0<\/em>???, ???<em>to = 0<\/em>???, ???<em>step = 0<\/em>??? and is usually used for finite <strong>FOR<\/strong>-loops (known as <em>FOR-TO-NEXT<\/em> Loops in <em>Visual Basic<\/em>) like ???<em>1,10,1<\/em>??? where ???<em>from = 1<\/em>???, ???<em>to = 10<\/em>??? and ???<em>step = 1<\/em>???, performing 10 loops and returning the values ???<em>1,2,3 ??? 10<\/em>??? in the <em>Batch-Variable<\/em> ???<em><strong>%1<\/strong><\/em>??? for additional processing.<\/p>\n<h3>Delay \/ Wait for XX seconds<\/h3>\n<p><em>MS DOS<\/em> <em>Batch<\/em> does not have an equivalent for commands like ???<em>Delay<\/em>??? or ???<em>Wait<\/em>??? in <em>Visual Basic<\/em>, <em>VBScript<\/em> or <em>JavaScript<\/em>, but there is a trick that you can use to emulate such a command, getting the same effect.<\/p>\n<div>\n<div>\n<pre>   1: ping -n XX 127.0.0.1&gt;NUL<\/pre>\n<p><!--CRLF--><\/p>\n<\/div>\n<\/div>\n<p>where ???<strong>XX<\/strong>??? should be replaced with the number of seconds for the delay.?? The command line tool ???<em><strong>PING<\/strong><\/em>??? makes a pause between two ping requests. The number of ping requests can be specified via the <em>???-n???<\/em> option. By specifying the address<em> ???127.0.0.1???<\/em> (or ???<em>localhost<\/em>???), 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 ???<em>Nirvana<\/em>??? (nothing) via the output-redirect command <strong>???&gt;???<\/strong> to <strong>???NUL???<\/strong>.<\/p>\n<p>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):<\/p>\n<div>\n<div>\n<pre>   1: FOR \/l %%a in (10,-1,1) do (TITLE %title% -- closing in %%as&amp;ping -n 2 -w 1 127.0.0.1&gt;NUL)<\/pre>\n<p><!--CRLF--><\/p>\n<pre>   2: TITLE Press any key to close the application&amp;ECHO.<\/pre>\n<p><!--CRLF--><\/p>\n<\/div>\n<\/div>\n<h3>Combining the Two Tricks<\/h3>\n<p>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 <em>.BAT Batch<\/em> script.<\/p>\n<div>\n<div>\n<pre>   1: for \/L %i in (0,0,0) do @YOUR-COMMAND &amp; ping -n XX 127.0.0.1&gt;NUL<\/pre>\n<p><!--CRLF--><\/p>\n<\/div>\n<\/div>\n<p>Replace ???<strong>YOUR-COMMAND<\/strong>??? with the command to execute and ???<strong>XX<\/strong>??? with the frequency of executing the command in seconds. The<strong> @<\/strong> in front of the command call specifies that the command itself will not be displayed, but only its results.<\/p>\n<h3>Free Some Resources by Pausing Services Temporary<\/h3>\n<p>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 <em>Services<\/em> that are not needed at the moment. This can be done manually via the ???<em>Services<\/em>??? application within your ???<em>Administrative Tools<\/em>??? that come with the <em>Windows<\/em> Operating System, but it is also possible to do this via a command line command, which could be called for multiple <em>Services<\/em> at once in a single <em>.BAT Batch<\/em> script. A second <em>.BAT Script<\/em> 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.<\/p>\n<p>To pause a <em>Service<\/em> call:<\/p>\n<div>\n<div>\n<pre>   1: sc pause SERVICENAME<\/pre>\n<p><!--CRLF--><\/p>\n<\/div>\n<\/div>\n<p>and to continue the <em>Service<\/em> again:<\/p>\n<div>\n<div>\n<pre>   1: sc continue SERVICENAME<\/pre>\n<p><!--CRLF--><\/p>\n<\/div>\n<\/div>\n<p>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.<\/p>\n<table cellspacing=\"0\" cellpadding=\"5\">\n<tbody>\n<tr>\n<td>You might want to check out the command line tool ???<em>XNET<\/em>???, which was part of the <em><a href=\"http:\/\/www.kixtart.org\/\" target=\"_blank\">KIXTART<\/a><\/em> script package up to version 2001 4.23 (<a href=\"http:\/\/www.kixtart.org\/binary\/distrib\/KiX2001_423.zip\" target=\"_blank\">download ZIP, 541 KB<\/a>). The usage is ???<em>XNET LIST<\/em>??? 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.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Note:<\/strong> <em>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&#8217;??? 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 ???<\/em><a href=\"http:\/\/support.microsoft.com\/kb\/311272\" target=\"_blank\"><em>devcon.exe<\/em><\/a><em>???.<\/em><\/p>\n<h3>Additional BATCH Script Related Posts<\/h3>\n<ul>\n<li><a href=\"http:\/\/www.roysac.com\/blog\/2009\/05\/ms-dos-commands-wild-cards-inputoutput-redirection-and-variables\/\">MS DOS Commands, Wild Cards, Input\/Output Redirection and Variables<\/a><\/li>\n<li><a href=\"http:\/\/www.roysac.com\/blog\/2009\/08\/sorting-away-mp3-files-into-sub-directories-by-artist-name\/\">Sorting Away MP3 Files into Sub Directories by Artist Name<\/a><\/li>\n<li><a href=\"http:\/\/www.roysac.com\/blog\/2009\/05\/microsoft-internet-explorer-search-providers-import-and-export\/\">Microsoft Internet Explorer Search Providers Import and Export<\/a><\/li>\n<li><a href=\"http:\/\/www.roysac.com\/blog\/2009\/05\/some-handy-batch-and-vb-scripts-for-organizing-files\/\">Some Handy BATCH and VB Scripts for Organizing Files<\/a><\/li>\n<li><a href=\"http:\/\/www.roysac.com\/blog\/2009\/09\/script-tool-process-scene-releases-version-2-1-update\/\">Script Tool: Process Scene Releases Version 2.1 Update<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-31","post","type-post","status-publish","format-standard","hentry","category-tools"],"_links":{"self":[{"href":"http:\/\/www.roysac.com\/blog\/wp-json\/wp\/v2\/posts\/31","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.roysac.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.roysac.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.roysac.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.roysac.com\/blog\/wp-json\/wp\/v2\/comments?post=31"}],"version-history":[{"count":1,"href":"http:\/\/www.roysac.com\/blog\/wp-json\/wp\/v2\/posts\/31\/revisions"}],"predecessor-version":[{"id":897,"href":"http:\/\/www.roysac.com\/blog\/wp-json\/wp\/v2\/posts\/31\/revisions\/897"}],"wp:attachment":[{"href":"http:\/\/www.roysac.com\/blog\/wp-json\/wp\/v2\/media?parent=31"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.roysac.com\/blog\/wp-json\/wp\/v2\/categories?post=31"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.roysac.com\/blog\/wp-json\/wp\/v2\/tags?post=31"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}