{"id":355,"date":"2014-08-08T08:16:00","date_gmt":"2014-08-08T07:16:00","guid":{"rendered":"http:\/\/pusling.com\/blog\/?p=355"},"modified":"2016-07-10T14:59:05","modified_gmt":"2016-07-10T13:59:05","slug":"fun-and-joy-with-bat-files","status":"publish","type":"post","link":"https:\/\/pusling.com\/blog\/?p=355","title":{"rendered":"Fun and joy with .bat files"},"content":{"rendered":"<p>Occasionally, one gets in touch with kind of &#8216;foreign&#8217; technologies and needs to get stuff working anyways.<\/p>\n<p>Recently, I had to do some various hacking with and around .bat files. Bat files are a kind of script files for Microsoft Windows.<\/p>\n<p><strong>Calling external commands<\/strong><\/p>\n<p>Imagine need to call some other command, let&#8217;s say git diff. So from a cmd thing, you would write<\/p>\n<pre class=\"lang:batch decode:true \" >git diff<\/pre>\n<p>similar to writing shell scripts on unixes. But there is a catch. If the thing you want to call is another bat-script, just calling it ensures it &#8216;replaces&#8217; the current script and never returns. So you need<\/p>\n<pre class=\"lang:batch decode:true \" >call git diff<\/pre>\n<p>if the command you want to run is a bat file and you want to return to your script.<\/p>\n<p><strong>Calling an external helper next to your script<\/strong><br \/>\nIf you for some reason needs to call some external helper placed next to your script, there is a helpful thing to do that as well. Imagine your helper is called <i>helper.bat<\/i><\/p>\n<pre class=\"lang:batch decode:true \" >call %~dp0helper.bat<\/pre>\n<p>is the very self-explanatory way of doing that.<\/p>\n<p><strong>Stopping execution of your script<\/strong><\/p>\n<p>If you somehow encounter some condition in your script that requires you to stop your script, there is a command &#8216;exit&#8217; handy. It even takes a argument for what error code there is.<\/p>\n<pre class=\"lang:batch decode:true \" >exit 2<\/pre>\n<p>stops your script with return code 2. But it also have the nice added feature that if you do it in a script you run by hand in a terminal, it also exits the terminal.<\/p>\n<p>Luckily there is also a fix for that:<\/p>\n<pre class=\"lang:batch decode:true \" >exit \/b 2<\/pre>\n<p>and it doesn&#8217;t exit your interactive terminal, and it sets the <i>%ERRORLEVEL%<\/i> variable to the exit code.<\/p>\n<p>Fortunately, the fun doesn&#8217;t stop here.<\/p>\n<p>If the script is run non-interactively, <i>exit \/b<\/i> doesn&#8217;t set the exit code for for example perl&#8217;s <i>system()<\/i> call. You need to use <i>exit<\/i> without <i>\/b<\/i> for that. So now you need two scripts. one for &#8220;interactive&#8221; use that calls <i>exit \/b<\/i> and a similar one using <i>exit<\/i> for use by other apps\/scripts.<\/p>\n<p>Or, we can combine some of our knowledge and add a extra layer of indirection.<\/p>\n<ul>\n<li>write your script for interactive use (with <i>exit \/b<\/i>) and let&#8217;s call it <i>script.bat<\/i><\/li>\n<li>create a simple wrapper script\n<pre class=\"lang:batch decode:true \" >call %~dp0script.bat\r\nexit %ERRORLEVEL%<\/pre>\n<\/li>\n<li>call the wrapper for non-interactive use<\/li>\n<\/ul>\n<p>and then success.<\/p>\n<p>Oh. and on a unrelated note. Windows can&#8217;t schedule tasks for users that aren&#8217;t logged in and don&#8217;t have a password set. The response &#8220;Access Denied&#8221; is the only clue given.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Occasionally, one gets in touch with kind of &#8216;foreign&#8217; technologies and needs to get stuff working anyways. Recently, I had to do some various hacking with and around .bat files. Bat files are a kind of script files for Microsoft Windows. Calling external commands Imagine need to call some other command, let&#8217;s say git diff. [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4],"tags":[],"class_list":["post-355","post","type-post","status-publish","format-standard","hentry","category-english","category-kde"],"_links":{"self":[{"href":"https:\/\/pusling.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/355","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pusling.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pusling.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pusling.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/pusling.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=355"}],"version-history":[{"count":7,"href":"https:\/\/pusling.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/355\/revisions"}],"predecessor-version":[{"id":440,"href":"https:\/\/pusling.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/355\/revisions\/440"}],"wp:attachment":[{"href":"https:\/\/pusling.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=355"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pusling.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=355"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pusling.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=355"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}