{"id":71153,"date":"2004-10-25T10:33:00","date_gmt":"2004-10-25T10:33:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2004\/10\/25\/how-can-i-send-data-from-one-script-to-another\/"},"modified":"2004-10-25T10:33:00","modified_gmt":"2004-10-25T10:33:00","slug":"how-can-i-send-data-from-one-script-to-another","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-send-data-from-one-script-to-another\/","title":{"rendered":"How Can I Send Data from One Script to Another?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! A while back you showed how we could use the InputBox function to prompt a user to enter a value we could then use in our script. What I\u2019d like to know is, is there any way I can take that value and use it in <I>another<\/I> script?<BR><BR>&#8212; JW<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, JW. We\u2019re guessing here, but apparently you\u2019d like to have a script that, among other things, prompted the user for some data. You\u2019d then like to be able to start a second script, and enable that second script to access data the user entered in the first script. And, of course, you\u2019d also like to know if such a thing is even possible.<\/P>\n<P>And the answer is (the suspense is killing you, isn\u2019t it?): sort of. Suppose you start Script A, and you enter a value. Can you now independently start Script B, and somehow have Script B peer into Script A and retrieve that value? No; that seems more akin to magic than to scripting. However, you <I>can<\/I> do this: start Script A and have the user enter the data. Then use Script A to start Script B; when you do that, you can pass the data along to Script B. That way, Script B will have full access to any information the user entered.<\/P>\n<P>To better explain this, let\u2019s take a look at a very simple example. We need two scripts: Script A and Script B. When we start Script A, the script will prompt the user to enter a number. Script A will then launch Script B; at the time it launches Script B, it will also pass along the number the user entered. Script B will then take that number, determine the square root, and report the answer.<\/P>\n<P><B>Note<\/B>. In a case like this, wouldn\u2019t it be easier to just have one script that both accepted the user input <I>and<\/I> calculated the square root? Yes. Remember, though, this is just a demonstration script designed to show you a way to pass data from one script to another; it\u2019s not intended to be an actual script you\u2019d make use of in real life.<\/P>\n<P>Here\u2019s Script A. Notice that it starts by creating an instance of the WSH Shell object; this object is used later on to launch Script B (which we\u2019ve named output.vbs; we\u2019ll name Script A input.vbs). We then use the InputBox function to ask the user to enter a number:<\/P><PRE class=\"codeSample\">Set objShell = CreateObject(&#8220;Wscript.Shell&#8221;)<\/p>\n<p>intValue = InputBox(&#8220;Please enter a number:&#8221;)\nstrCommandLine = &#8220;output.vbs &#8221; &amp; intValue\nobjShell.Run(strCommandLine)\n<\/PRE>\n<P>Notice what we do after the user enters a number (we\u2019ll pretend that the user entered the number 2). We have this line of code:<\/P><PRE class=\"codeSample\">strCommandLine = &#8220;output.vbs &#8221; &amp; intValue\n<\/PRE>\n<P>This code constructs a command for starting Script B. Suppose you wanted to start Script B and pass it the number 2 as a command-line argument. To do that, you would type this from the command prompt:<\/P><PRE class=\"codeSample\">output.vbs 2\n<\/PRE>\n<P>What we\u2019re doing here is building a string that contains these two items: the name of the script we\u2019re starting (<B>output.vbs<\/B>) and the command-line argument we\u2019re passing (<B>2<\/B>). Of course, we don\u2019t know that the argument is actually 2; we\u2019re just passing along whatever value the user entered. That\u2019s why we use the variable intValue instead. Notice, too, that output.vbs (plus a blank space) is included <I>inside<\/I> double-quote marks, and intValue is <I>outside<\/I> those double-quote marks. Why isn\u2019t intValue inside the double-quotes? That\u2019s easy; because then we would have constructed <I>this<\/I> command, which wouldn\u2019t pass the number 2 (or any other number the user entered), but would instead pass the string value <I>intValue<\/I>:<\/P><PRE class=\"codeSample\">output.vbs intValue\n<\/PRE>\n<P>Not what we want.<\/P>\n<P>After constructing the command line, we then use the Run method to launch Script B, passing the value entered by the user as a command-line argument. <\/P>\n<P>So then what does Script B look like? Well, it looks an awful lot like this:<\/P><PRE class=\"codeSample\">intValue = Wscript.Arguments.Item(0)\nintSquareRoot = Sqr(intValue)<\/p>\n<p>Msgbox &#8220;The square root of &#8221; &amp; intValue &amp; _\n    &#8221; is &#8221; &amp; intSquareRoot &amp; &#8220;.&#8221;\n<\/PRE>\n<P>As you can see, we assign the first (and in this case, the only) command-line argument to the variable intValue. We then use the <B>Sqr<\/B> function to determine the square root of that value. Finally, we display the answer. In this case we used VBScript\u2019s <B>Msgbox<\/B> function (as opposed to Wscript.Echo) to ensure that the answer appears in a message box. Had we used Wscript.Echo and Cscript was your default script host, a command window would open for a split second, the answer would appear on screen, and then the window would disappear before you had a chance to actually <I>see<\/I> the answer. Again, not what we wanted. The Msgbox function <I>always<\/I> displays data in a message box, regardless of the script host, so we went with Msgbox.<\/P>\n<P>Again, a very simple example, but it could easily be expanded to send 2, 3, 4 or any number of command-line arguments from Script A to Script B. This definitely works, although you could theoretically run into the limitations on the length of a command string if you tried to pass too much data. In that case, your best bet would be to have Script A save data to a text file, and then have Script B read the data from that same file. <\/P>\n<P>You also have to be careful about scripts that send command-line arguments that include spaces. For example, suppose you wanted to send the name <B>Ken Myer<\/B> to Script B. This command won\u2019t work; it send <I>two<\/I> arguments (<B>Ken<\/B> and <B>Myer<\/B>) rather than the single argument Ken Myer:<\/P><PRE class=\"codeSample\">output.vbs Ken Myer\n<\/PRE>\n<P>Because of the space between Ken and Myer (and the space is the way WSH determines where one argument ends and the next begins), you need to put Ken Myer in double quote marks, like this:<\/P><PRE class=\"codeSample\">output.vbs &#8220;Ken Myer&#8221;\n<\/PRE>\n<P>In turn, that means the command-string you construct must includes double-quote marks as well. Here\u2019s one way to do that; for more information, see this archived <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/qanda\/aug04\/hey0806.mspx\"><B><I>Hey, Scripting Guy!<\/I><\/B><\/A>column:<\/P><PRE class=\"codeSample\">strCommandLine = &#8220;output.vbs &#8221; &amp; chr(34) &amp; strValue &amp; chr(34)\n<\/PRE>\n<P>In this example, each instance of chr(34) inserts a double-quote mark. We thus end up with <B>output.vbs <\/B>plus <B>&#8220;<\/B> plus <B>Ken Myer<\/B> plus <B>&#8220;<\/B>, or:<\/P><PRE class=\"codeSample\">output.vbs &#8220;Ken Myer&#8221;\n<\/PRE><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! A while back you showed how we could use the InputBox function to prompt a user to enter a value we could then use in our script. What I\u2019d like to know is, is there any way I can take that value and use it in another script?&#8212; JW Hey, JW. We\u2019re [&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":[22,3,4,5],"class_list":["post-71153","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-retrieving-input","tag-scripting-guy","tag-scripting-techniques","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! A while back you showed how we could use the InputBox function to prompt a user to enter a value we could then use in our script. What I\u2019d like to know is, is there any way I can take that value and use it in another script?&#8212; JW Hey, JW. We\u2019re [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/71153","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=71153"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/71153\/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=71153"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=71153"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=71153"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}