{"id":55703,"date":"2008-04-25T01:15:00","date_gmt":"2008-04-25T01:15:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2008\/04\/25\/hey-scripting-guy-how-can-i-pass-command-line-arguments-to-a-batch-file-started-from-a-script\/"},"modified":"2008-04-25T01:15:00","modified_gmt":"2008-04-25T01:15:00","slug":"hey-scripting-guy-how-can-i-pass-command-line-arguments-to-a-batch-file-started-from-a-script","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-pass-command-line-arguments-to-a-batch-file-started-from-a-script\/","title":{"rendered":"Hey, Scripting Guy! How Can I Pass Command-Line Arguments to a Batch File Started From a Script?"},"content":{"rendered":"<p><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\" \/> <\/p>\n<p>Hey, Scripting Guy! I am trying to write a script that requests multiple pieces of information from a user; I then want to call a batch file, using each piece of information as a separate command-line argument. Unfortunately, though, I can\u2019t get this to work correctly. Can you help?<br \/>&#8212; BP<\/p>\n<p><img decoding=\"async\" height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\" \/><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\" \/><a href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><img decoding=\"async\" class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\" \/><\/a> <\/p>\n<p>Hey, BP. You know, at first we was not going to help, but then we have second thoughts and decide to help if could.<\/p>\n<p>Oh, in case you\u2019re wondering, the Scripting Editor is on vacation this week, which means we can write things any way we dang well please. Grammar? We don\u2019t need no stinkin\u2019 grammar. As the French philosopher Michel de Montaigne so aptly put it, \u201cThe greater part of the world&#8217;s troubles are due to questions of grammar.\u201d As for spelling, well, who even cares about korrect speling anyway? We\u2019ll do the best we can, but we\u2019re with A. A. Milne when it comes to spelling: our spelling is \u201c\u2026Wobbly.&nbsp; It&#8217;s good spelling but it Wobbles, and the letters get in the wrong places.\u201d<\/p>\n<p>Wee here you, A. A. We hear ewe.<\/p>\n<table class=\"dataTable\" id=\"E3C\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p><b>Note<\/b>. A. A. Milne, of course, is the creator of the immortal Winnie the Pooh, a bear of very little brain. (And no, that doesn\u2019t mean that Winnie the Pooh must be the true identity of the Scripting Guy who writes this column.) Michel de Montaigne was (and still is, we suppose) a 16<sup>th<\/sup>-century French writer and philosopher. After Montaigne was born his father decided that the boy must learn Latin as his first language; to make sure that happened, Dad only hired servants who could speak Latin, and everyone in the household was put under strict orders to speak Latin \u2013 and Latin only \u2013 to young Michel. Of course, this pedagogical approach paid enormous dividends; later in life Montaigne came up with such insightful bits of wisdom as \u201cWhen I play with my cat, how do I know that she is not passing time with me rather than I with her?\u201c<\/p>\n<p>Or, in Latin, \u201cUt EGO lascivio per meus cattus , quam operor EGO teneo ut is est non obduco vicis me quinymo quam EGO per suus?\u201d<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>There\u2019s also one other advantage to the Scripting Editor being on vacation: after all, she not only insists on correct spelling and correct grammar, but she also absolutely <i>refuses<\/i> to let us answer questions about passing command-line arguments to a batch file. Why? Who knows? Maybe her parents decided that \u201cgrouchy\u201d should be the first language <i>she<\/i> learned.<\/p>\n<table class=\"dataTable\" id=\"ETD\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\"><b>Note<\/b>. If so, mission accomplished!<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>But while the cat\u2019s away, the mice <i>will<\/i> answer questions about passing command-line arguments to a batch file. Or at least we\u2019ll answer <i>one<\/i> question about passing command-line arguments to a batch file. And if we use poor grammar when doing so, well, then us you can sue.<\/p>\n<p>Before showing you some code let\u2019s take a minute to explain the scenario, and the problem BP has run into. BP has written a script that solicits two pieces of information from a user: a user name and a computer name. He then wants to take those two pieces of information and use them as command-line arguments for a batch file named Namechange.bat. In order to test the script, he created a simple little batch file that does nothing more than echo back the two arguments he supplied:<\/p>\n<pre class=\"codeSample\">echo %1\necho %2\npause\n<\/pre>\n<p>That\u2019s great. However, suppose BP enters <b>kenmyer<\/b> as the user name and <b>atl-ws-001<\/b> as the computer name. Here\u2019s what he gets when he runs the batch file:<\/p>\n<pre class=\"codeSample\">C:\\scripts&gt;echo kenmyeratl-ws-001\nkenmyeratl-ws-001\n<\/pre>\n<p>Uh-oh. As you can see, the batch file doesn\u2019t think we sent it <i>two<\/i> command-line arguments; instead, it thinks we only provided a single command line argument: <b>kenmyeratl-ws-001<\/b>. Needless to say, that\u2019s no good, no good at all.<\/p>\n<p>Or, seeing as how the Scripting Editor isn\u2019t around to correct us, that ain\u2019t a-gonna do you no good, no good a\u2019tall.<\/p>\n<p>Or, even better: that does you about as much good as a screen door on a submarine.<\/p>\n<table class=\"dataTable\" id=\"E2E\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\"><b>Note<\/b>. Which, by the way, isn\u2019t really all that much good.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>So what\u2019s the problem? As it turns out, the problem lies in the way we call the batch file. BP was calling the batch file using a command similar to this:<\/p>\n<pre class=\"codeSample\">objShell.Run \"C:\\Scripts\\Namechange.bat \" &amp;strUser &amp;strComputer\n<\/pre>\n<p>That\u2019s a valid line of code, but what it does is create a command that, under the covers, looks like this:<\/p>\n<pre class=\"codeSample\">C:\\Scripts\\Namechange.bat kenmyeratl-ws-001\n<\/pre>\n<p>As you can see, the two command-line arguments have been scrunched together into a single argument. Why? Because we didn\u2019t specify that a blank space should appear between the two arguments. Granted, in the code itself there <i>is<\/i> a blank space between &amp;strUser and &amp;strComputer. However, that blank space doesn\u2019t appear when you concatenate the string values. If we wanted to, we could put a <i>ton<\/i> of blank spaces between the two variable names, like so: <\/p>\n<pre class=\"codeSample\">&amp;strUser                                                   &amp;strComputer\n<\/pre>\n<p>Now, guess how many blank spaces are going to appear between the two command-line arguments. You got it: 0.<\/p>\n<p>Fortunately, this is a very easy problem to fix. All we have to do is \u201cmanually\u201d insert a blank space between the two command-line arguments, like we did in this script:<\/p>\n<pre class=\"codeSample\">strUser = InputBox(\"Enter user name:\", \"RT Remote System Rename\")\nstrComputer = InputBox(\"Enter new computer name:\", \"RT Remote System Rename\")\n\nstrCommand = \"C:\\Scripts\\Namechange.bat \" &amp; strUser &amp; \" \" &amp; strComputer\n\nSet objShell = CreateObject(\"WScript.Shell\")\nobjShell.Run strCommand\n<\/pre>\n<p>Let\u2019s see if we can explain how this works. In the first two lines, we\u2019re simply using BP\u2019s two calls to the <b>InputBox<\/b> function, one call to retrieve the user name and a second call to retrieve the computer name. That part should be pretty straightforward.<\/p>\n<p>For that matter, this part should be pretty straightforward, too:<\/p>\n<pre class=\"codeSample\">strCommand = \"C:\\Scripts\\Namechange.bat \" &amp; strUser &amp; \" \" &amp; strComputer\n<\/pre>\n<p>All we\u2019re doing here is constructing the command that starts the batch file, then storing that command in a variable named strCommand. Notice that, this time around, our command combines <i>four<\/i> items:<\/p>\n<table class=\"\" cellSpacing=\"0\" cellPadding=\"0\" border=\"0\">\n<tbody>\n<tr>\n<td class=\"listBullet\" vAlign=\"top\">\u2022<\/td>\n<td class=\"listItem\">\n<p><b>C:\\Scripts\\Namechange.bat<\/b> , the path to the batch file. Note that, like BP, we were careful to include a blank space following the path name? Why? Because if we left that blank space out then our command line arguments would run smack dab into the path name, like so: <b>C:\\Scripts\\Namechange.batkenmyer atl-ws-001<\/b>. BP was right to include a blank space after the path name; he just didn\u2019t realize that he needed one between the two command-line arguments as well.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td class=\"listBullet\" vAlign=\"top\">\u2022<\/td>\n<td class=\"listItem\">\n<p><b>strUser<\/b>, the variable containing the first command-line argument.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td class=\"listBullet\" vAlign=\"top\">\u2022<\/td>\n<td class=\"listItem\">\n<p><b>&#8221; &#8220;<\/b>, a blank space.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td class=\"listBullet\" vAlign=\"top\">\u2022<\/td>\n<td class=\"listItem\">\n<p><b>strComputer<\/b>, the variable containing the second command-line argument.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Put them all together and they spell MOTHER.<\/p>\n<p>Oh, wait; sorry. Put them all together and they spell this:<\/p>\n<pre class=\"codeSample\">C:\\Scripts\\Namechange.bat kenmyer atl-ws-001\n<\/pre>\n<p>Which is way more like it.<\/p>\n<p>Incidentally, we didn\u2019t <i>have<\/i> to store this command in a variable. Like BP, we could have directly called this command from within the script:<\/p>\n<pre class=\"codeSample\">objShell.Run \"C:\\Scripts\\Namechange.bat \" &amp; strUser &amp; \" \" &amp; strComputer\n<\/pre>\n<p>So why <i>didn\u2019t<\/i> we directly call the command from within the script? Mainly for debugging and troubleshooting purposes. As BP discovered, it\u2019s not hard to determine that the script isn\u2019t working correctly; what\u2019s hard is determining <i>why<\/i> the script isn\u2019t working correctly. We know the batch file didn\u2019t realize we were sending it to command-line arguments, but we have no idea <i>why<\/i> the batch file didn\u2019t realize that.<\/p>\n<p>So how does storing our command in a variable help us troubleshoot problems with the script? That\u2019s easy. With BP\u2019s original script we never get to see the command that actually calls the batch file. With <i>our<\/i> script, however, we can easily view the command that actually calls the batch file; all we have to do is echo back the value of strCommand. Suppose we took BP\u2019s original script and stored his original batch file command in a variable. Now suppose we decide to echo back the value of that variable before we actually try calling the batch file. If we do that we\u2019ll see the following:<\/p>\n<pre class=\"codeSample\">C:\\Scripts\\Namechange.bat kenmyeratl-ws-001\n<\/pre>\n<p>Right then and there we know we have a problem. More important, we also know exactly what the problem is: no blank space between the command-line arguments. Armed with that information, we have a pretty good chance of being able to fix the script.<\/p>\n<p>Once we\u2019ve created our command we can then call the batch file using these two lines of code:<\/p>\n<pre class=\"codeSample\">Set objShell = CreateObject(\"WScript.Shell\")\nobjShell.Run strCommand\n<\/pre>\n<p>As you can see, there\u2019s nothing very fancy going on here: all we\u2019re doing is creating an instance of the <b>Wscript.Shell<\/b> object, then using the <b>Run<\/b> method to call the batch file, using the variable strCommand as the sole parameter to the Run method. Upon doing so, we should get back something that looks like this:<\/p>\n<pre class=\"codeSample\">C:\\scripts&gt;echo kenmyer\nkenmyer\n\nC:\\scripts&gt;echo atl-ws-001\natl-ws-001\n<\/pre>\n<p>That\u2019s definitely more like it.<\/p>\n<p>Incidentally, some of you might be wondering what the Script Editor does when she\u2019s on vacation. Well, Monday morning she read her email, then came into work for a meeting. Tuesday morning she read her email, then came into work to have lunch with the legendary Peter Costantini, recently returned from a trip to the planet Saturn. (Or maybe it was Spain. One of the two, anyway.) On Wednesday she came into work for another meeting, then decided to stick around the office for a few hours in order to get some things taken care of.<\/p>\n<p>And you guys thought we were joking when we told you how much fun the Scripting Editor was.<\/p>\n<p><i>That\u2019s all for now, folks. See you tomorrow, and having a nice daye!<\/i><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I am trying to write a script that requests multiple pieces of information from a user; I then want to call a batch file, using each piece of information as a separate command-line argument. Unfortunately, though, I can\u2019t get this to work correctly. Can you help?&#8212; BP Hey, BP. You know, at [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[2,3,4,5],"class_list":["post-55703","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-running","tag-scripting-guy","tag-scripting-techniques","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I am trying to write a script that requests multiple pieces of information from a user; I then want to call a batch file, using each piece of information as a separate command-line argument. Unfortunately, though, I can\u2019t get this to work correctly. Can you help?&#8212; BP Hey, BP. You know, at [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/55703","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=55703"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/55703\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=55703"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=55703"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=55703"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}