{"id":1130,"date":"2014-09-12T15:38:35","date_gmt":"2014-09-12T15:38:35","guid":{"rendered":"http:\/\/scunster.co.uk\/?p=1130"},"modified":"2014-09-13T08:19:37","modified_gmt":"2014-09-13T08:19:37","slug":"use-command-line-switches-in-a-bat-file","status":"publish","type":"post","link":"https:\/\/scunster.co.uk\/?p=1130","title":{"rendered":"Use command line switches in a bat file"},"content":{"rendered":"<p>This explains how to use command line switches within a bat file.<\/p>\n<p>For example, you may want to add a switch to your file that runs only a specific section of your bat file. Such as adding a \/r switch<\/p>\n<pre>c:\\mybat.bat \/r<\/pre>\n<p>You target the \/r within your bat file using the %1 variable. So, within the below bat file we tell it that %1 = \/r<br \/>\nThen using an IF statement, we can tell the bat exactly what to do if our bat file is run with the \/r switch.<\/p>\n<pre>echo off\r\nif \"%1\"==\"\/r\" goto switch\r\necho no switch was used to launch me!\r\npause\r\n\r\n:switch\r\necho I was launched using the \/r switch!\r\npause<\/pre>\n<p>Go ahead, try it, save the following into notepad and save it as switch.bat<br \/>\nWhen you run it from the command prompt with and without the \/r switch you will see the difference.<\/p>\n<p>You can of course then use the same technique to get other information from the command line.<br \/>\nFor example, you may want to type in a directory location to perform backups etc and use that within the bat file.<br \/>\nThe method is exactly the same as above except that we will now be using two variables, %1 and %2<\/p>\n<pre>c:\\switch.bat \/r d:\\backup\\myfiles<\/pre>\n<p>within your bat file you could define the switches as follows<\/p>\n<pre>echo off\r\nif \"%1\"==\"\/r\" goto switch\r\necho no switch was used to launch me!\r\npause\r\n\r\n:switch\r\nSET data=%2\r\necho I was launched using the \/r switch and a data path as well!\r\necho the backup directory will now be set as %data%\r\necho and can be called anywhere in my bat file by typing %data%\r\npause<\/pre>\n<p>It is easy to add up to 9 switches \/ variables the same way, any more than that requires the use of another command called SHIFT, which we will talk about in a different guide.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This explains how to use command line switches within a bat file. For example, you may want to add a switch to your file that runs only a specific section of your bat file. Such as adding a \/r switch c:\\mybat.bat \/r You target the \/r within your bat file using the %1 variable. So, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[169,17,168],"tags":[197,152,106,217,181,218],"class_list":["post-1130","post","type-post","status-publish","format-standard","hentry","category-win-7-10-tips-tricks","category-windows","category-xp-tips-tricks","tag-bat","tag-command","tag-dos","tag-line","tag-switch","tag-variable"],"_links":{"self":[{"href":"https:\/\/scunster.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/1130","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/scunster.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/scunster.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/scunster.co.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/scunster.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1130"}],"version-history":[{"count":8,"href":"https:\/\/scunster.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/1130\/revisions"}],"predecessor-version":[{"id":1138,"href":"https:\/\/scunster.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/1130\/revisions\/1138"}],"wp:attachment":[{"href":"https:\/\/scunster.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1130"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/scunster.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1130"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/scunster.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}