{"id":66313,"date":"2006-10-06T09:07:00","date_gmt":"2006-10-06T09:07:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/10\/06\/how-can-i-pass-a-drive-letter-from-a-script-to-the-command-shell\/"},"modified":"2006-10-06T09:07:00","modified_gmt":"2006-10-06T09:07:00","slug":"how-can-i-pass-a-drive-letter-from-a-script-to-the-command-shell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-pass-a-drive-letter-from-a-script-to-the-command-shell\/","title":{"rendered":"How Can I Pass a Drive Letter From a Script to the Command Shell?"},"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! How can I pass the drive letter of a drive from a VBScript script to the command shell that called that script?<BR><BR>&#8212; JF<\/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, JF. You know, as kids grow up they pass through a phase where they believe that their parents are capable of doing \u2013 and of fixing \u2013 anything. Admittedly, children quickly learn the cold, harsh facts, but, at least for a little while, kids believe that parents can do no wrong. <\/P>\n<P>Of course, as a parent that\u2019s incredibly heart-warming; at the same time, however, it places a heck of a lot of pressure on you. After all, you feel obligated to live up to the faith that has been placed in you. As a result, you end up spending a ridiculous amount of time fiddling with duct tape and Super Glue, all in an often-futile attempt to fix something that you know <I>can\u2019t<\/I> be fixed. But with little Billy or little Betty counting on you, well, what else can you do?<\/P>\n<TABLE id=\"EAD\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. To tell you the truth, we have no idea why little Billy and little Betty are always bringing their broken toys over here. Why don\u2019t they ask their <I>own<\/I> parents for help?<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Here at <I>Hey, Scripting Guy!<\/I> we think of our readers as being like our own children. Granted, children who never call, children who never remember our birthdays, and children who only come around when they need something from us. But, hey, children nonetheless. And so, as Scripting Guys, we feel obligated to get out the duct tape and Super Glue and see if we can figure out a way to pass the drive letter of a drive from a VBScript script to the command shell.<\/P>\n<P>Before anyone asks, we should mention that JF noted that he can\u2019t write the drive letter to a file or the registry. That took care of our Plan A. And while he didn\u2019t specifically say this, we\u2019re also assuming that he\u2019d like the script to actually work; that eliminated our Plan B. Fortunately, Plan C seems to do the trick:<\/P><PRE class=\"codeSample\">strDrive = &#8220;D:&#8221;\nstrDrive = Left(strDrive,1)\nintDrive = Asc(strDrive) &#8211; 64<\/p>\n<p>Wscript.Quit(intDrive)\n<\/PRE>\n<P>What we have here is a very simple \u2013 but semi-clever \u2013 way to work around the limitations placed on us. We were aware that you can use Windows Script Host\u2019s <B>Quit<\/B> method to set the value of the errorlevel environment variable. As far as we knew, however, you could write only numeric values to errorlevel. As it turns out \u2013 and for once \u2013 we were right about that.<\/P>\n<P>Which meant that we had to find a way to work around <I>that<\/I> limitation as well. But hey, Scripting Guys \u2013 like parents \u2013 can do anything, right?<\/P>\n<P>The script itself starts out by assigning an arbitrary drive letter to a variable named strDrive. (We\u2019re assuming that, in a real script, you\u2019d have real code that goes out and determines the drive letter, perhaps WMI code that identifies the drive letter for your CD-ROM drive.) After assigning the value <I>D:<\/I> to strDrive we then use this line of code to grab just the first letter in that value:<\/P><PRE class=\"codeSample\">strDrive = Left(strDrive,1)\n<\/PRE>\n<P>Why? That\u2019s an easy one: we only need the drive <I>letter<\/I>; we have no use for the colon that follows the drive letter.<\/P>\n<P>Now, as we noted earlier, we can\u2019t write a string value (like <I>D<\/I>) to the errorlevel environment variable. Therefore, we use the VBScript <B>Asc<\/B> function to convert <I>D<\/I> to its ASCII (numeric) equivalent:<\/P><PRE class=\"codeSample\">intDrive = Asc(strDrive) &#8211; 64\n<\/PRE>\n<P>Good question: why <I>do<\/I> we subtract 64 from the ASCII value? Admittedly, we don\u2019t <I>have<\/I> to. However, the ASCII value of the letter <I>A<\/I> is 65; if we subtract 64 from 65 we get 1. That means, in our script, the value of <I>A<\/I> ends up being 1; coincidentally enough, <I>A<\/I> also happens to be letter number 1 in the English alphabet. The ASCII value of <I>D<\/I> is 68. If we subtract 64 from 68 we get 4, and <I>D<\/I> just happens to be letter number \u2013 right; you got it.<\/P>\n<P>From there all we have to do is call the Quit method to terminate the script, making sure to pass our \u201cdrive letter\u201d (or at least its numeric equivalent) as the sole parameter to the Quit method:<\/P><PRE class=\"codeSample\">Wscript.Quit(intDrive)\n<\/PRE>\n<P>What does all that gain us? Well, suppose we run our script, and then run the following little batch file:<\/P><PRE class=\"codeSample\">echo off\necho %errorlevel%\n<\/PRE>\n<P>The batch file will echo back the value 4, which happens to be the very same value we derived from our script. Neat, huh? All you have to do now is modify the batch file so that it converts the value 4 back to drive letter D. But that\u2019s something you\u2019ll have to do on your own. After all, we Scripting Guys can\u2019t do <I>everything<\/I> for you.<\/P>\n<P>And would it hurt you to call once in awhile? And maybe get a haircut? And just how long <I>do<\/I> you plan to spend at the bowling alley before you start looking for a real job? After all the money it cost to send you to college and get you that fancy degree, you end up \u2026<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I pass the drive letter of a drive from a VBScript script to the command shell that called that script?&#8212; JF Hey, JF. You know, as kids grow up they pass through a phase where they believe that their parents are capable of doing \u2013 and of fixing \u2013 anything. [&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-66313","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! How can I pass the drive letter of a drive from a VBScript script to the command shell that called that script?&#8212; JF Hey, JF. You know, as kids grow up they pass through a phase where they believe that their parents are capable of doing \u2013 and of fixing \u2013 anything. [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66313","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=66313"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66313\/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=66313"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=66313"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=66313"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}