{"id":1431,"date":"2016-12-17T00:42:12","date_gmt":"2016-12-17T00:42:12","guid":{"rendered":"http:\/\/blog.paranoidprofessor.com\/?p=1431"},"modified":"2016-12-17T00:42:12","modified_gmt":"2016-12-17T00:42:12","slug":"windows-scripting","status":"publish","type":"post","link":"https:\/\/blog.paranoidprofessor.com\/index.php\/2016\/12\/17\/windows-scripting\/","title":{"rendered":"windows scripting"},"content":{"rendered":"<p>I was asked the other day if I could take over a task that was running on windows machine. The sentence was hardly out of my boss\u2019s mouth when he said that \u201cyou unix guys don\u2019t like windows though\u201d. Well, he is correct but perhaps not for the same reasons that he thinks (who knows how bosses really think).<\/p>\n<p>I am not a big windows fan because although you can write batch scripts they tend to be fairly simplistic. When I say simplistic, I mean what is possible right out of the box without downloading third party apps, installing something from MSDN or other power tools.<\/p>\n<p>The \u201ctask\u201d that I was being assigned was about processing some data files by type every few minutes. I thought about using a for loop to process every PNG file, followed by a loop to process every XML file but the more I thought about it the sillier that seemed to me.<\/p>\n<p>Fortunately there is a way to examine the extension of a file even in windows. Simply add tilde and x when looking at the variable containing the filename.<\/p>\n<p>In one way, it is very unix like. The comparisons are case sensitive which is a bit of a bummer on windows, where filenames are not really case sensitive. I did have to add a bit of extra logic to deal with both the upper and lower case extensions but in a more normal environment they should all be consistent.<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n@echo off\r\ncd c:\\temp\\dosplay\r\nFOR \/f %%i in ('dir \/l \/b *.*') DO (\r\n\r\n@echo .\r\n@echo .\r\n@echo ext %%~xi\r\n@echo loop %%i\r\n\r\nif &quot;%%~xi&quot; == &quot;.PNG&quot; (\r\nstart mspaint %%i\r\n)\r\nif &quot;%%~xi&quot; == &quot;.png&quot; (\r\nstart mspaint %%i\r\n)\r\n\r\nif &quot;%%~xi&quot; == &quot;.XML&quot; (\r\nrem start notepad++ %%i\r\ncall vi %%i\r\n)\r\nif &quot;%%~xi&quot; == &quot;.xml&quot; (\r\nrem start notepad++ %%i\r\ncall vi %%i\r\n)\r\n@echo .\r\n@echo .\r\n)\r\n<\/pre>\n<p>In this\u00a0test I am using the start command to start executables but the call command when executing other batch scripts.<\/p>\n<p>The second half of the assignment was to pause for a given amount of time between processing files. On Unix or Linux I would use the sleep command, which does not exist on windows. I was actually surprised to find out that there are two different alternatives which work just as well.<\/p>\n<p>The first is the timeout command. It is actually the same as the sleep command but it actually counts down on the screen.<\/p>\n<div class=\"sbody-code\">\n<pre><code>c:\\temp\\dosplay&gt;timeout 10 &gt; output.txt\r\nc:\\temp\\dosplay&gt;type output.txt\r\nGewartet wird 10 Sekunden. Weiter mit beliebiger Taste. <\/code><\/pre>\n<\/div>\n<p>If this is ok in your output then just redirect this output in the same way as any other commands. When this is inacceptable them simply redirect the output to nul.<\/p>\n<div class=\"sbody-code\">\n<pre><code>timeout 10 &gt;nul<\/code><\/pre>\n<\/div>\n<p>The second method is actually essentially the same. The waitfor command will wait for a certain about of time before simply resuming.<\/p>\n<div class=\"sbody-code\">\n<pre><code>c:\\temp\\dosplay&gt;waitfor \/t 10 pause\r\nFEHLER: Beim Warten auf \"pause\" wurde das Zeitlimit \u00fcberschritten.<\/code><\/pre>\n<\/div>\n<p>This example will wait for the pause to finish. It actually won\u2019t finish nor will pressing any keys cause it to continue. It will simply will wait for the number of seconds given and then generate an error. It is possible to redirect this error to the nul output device as well.<\/p>\n<div class=\"sbody-code\">\n<pre><code>rem waitfor \/t 10 pause &gt;nul<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>I was asked the other day if I could take over a task that was running on windows machine. The sentence was hardly out of my boss\u2019s mouth when he said that \u201cyou unix guys don\u2019t like windows though\u201d. Well, &hellip; <a href=\"https:\/\/blog.paranoidprofessor.com\/index.php\/2016\/12\/17\/windows-scripting\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2,20],"tags":[18,39],"_links":{"self":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/1431"}],"collection":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/comments?post=1431"}],"version-history":[{"count":3,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/1431\/revisions"}],"predecessor-version":[{"id":1444,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/1431\/revisions\/1444"}],"wp:attachment":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/media?parent=1431"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/categories?post=1431"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/tags?post=1431"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}