{"id":68013,"date":"2006-02-07T12:40:00","date_gmt":"2006-02-07T12:40:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/02\/07\/how-can-i-determine-the-drive-letter-for-the-drive-my-script-is-currently-running-on\/"},"modified":"2006-02-07T12:40:00","modified_gmt":"2006-02-07T12:40:00","slug":"how-can-i-determine-the-drive-letter-for-the-drive-my-script-is-currently-running-on","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-determine-the-drive-letter-for-the-drive-my-script-is-currently-running-on\/","title":{"rendered":"How Can I Determine the Drive Letter for the Drive My Script is Currently Running On?"},"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! I have a script that runs off a CD and copies a bunch of files to a computer. However, in order to copy the files I need to know the complete file path, including the drive letter. How can I determine the drive letter for the drive my script is currently running on?<BR><BR>&#8212; RKG<\/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, RKG. You know, about a year or so ago one of the Scripting Guys installed &#8211; or, more accurately, <I>tried<\/I> to install &#8211; a relatively well-known application. <\/P>\n<TABLE id=\"E3C\" 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>. If this had been a Microsoft program we\u2019d tell you the name, but this <I>wasn\u2019t<\/I> a Microsoft program. As if Microsoft would ever release software that didn\u2019t work flawlessly right from the start!<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>As you probably already guessed, this application wouldn\u2019t install. Why not? Because the setup program assumed that the CD-ROM drive would always be drive D. When faced with a computer where the CD drive was on drive E, the installation program did the only thing it could do: it gave up. In order to install the application you had to download a patch that could deal with alternate drive letters. <\/P>\n<P>Of course, even then you were prompted to enter the drive letter to your CD-ROM; the patch couldn\u2019t figure out the drive letter for the CD drive, either. But at least you could finally install the software.<\/P>\n<P>Pretty sad story, isn\u2019t it? And what makes it sadder is that this multi-million dollar software company could have solved their problem using a script no more complicated than this:<\/P><PRE class=\"codeSample\">Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\nSet objShell = CreateObject(&#8220;Wscript.Shell&#8221;)<\/p>\n<p>strPath = objShell.CurrentDirectory\nstrDrive = objFSO.GetDriveName(strPath)<\/p>\n<p>Wscript.Echo strDrive\n<\/PRE>\n<P>Good point: we should have contacted this company and offered to act as high-priced installation consultants. <I>Now<\/I> you tell us!<\/P>\n<P>As you can see, this is a pretty simple little script. We begin by creating instances of two objects: <B>Scripting.FileSystemObject<\/B> and <B>Wscript.Shell<\/B>. We then encounter this line of code:<\/P><PRE class=\"codeSample\">strPath = objShell.CurrentDirectory\n<\/PRE>\n<P>It goes without saying (although we\u2019ll say it anyway) that the <B>CurrentDirectory<\/B> property reports back the current directory for the script. For example, suppose our CD-ROM is drive E, and the script is running in the Setup folder. Try to guess the value of the CurrentDirectory property:<\/P><PRE class=\"codeSample\">E:\\Setup\n<\/PRE>\n<P>You\u2019re right; that was a bit too easy, wasn\u2019t it?<\/P>\n<P>Once we have a file path we can then use the FileSystemObject\u2019s <B>GetDriveName<\/B> method to extract the drive letter from that path. That\u2019s what we do here:<\/P><PRE class=\"codeSample\">strDrive = objFSO.GetDriveName(strPath)\n<\/PRE>\n<P>We simply pass GetDriveName the file path in question, then store the returned value in the variable strDrive. And what do you suppose we get when we echo back the value of strDrive? You got it:<\/P><PRE class=\"codeSample\">E:\n<\/PRE>\n<P>All of which means that now <I>you\u2019re<\/I> ready to act as a high-priced installation consultant. When you get your first big job, remember: it was the Scripting Guys who helped you get started. And, yes, we <I>will<\/I> take a check. Thanks for asking.<I> (Editor\u2019s Note: Time for the editor to make the lawyers happy: As much as we\u2019d like to accept your check [money order, credit card number, cash, etc.], this column and all advice in it are provided free of charge. The Scripting Guys will have to continue to survive on their regular paychecks.)<\/I><\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I have a script that runs off a CD and copies a bunch of files to a computer. However, in order to copy the files I need to know the complete file path, including the drive letter. How can I determine the drive letter for the drive my script is currently running [&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":[40,2,3,4,5],"class_list":["post-68013","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-filesystemobject","tag-running","tag-scripting-guy","tag-scripting-techniques","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I have a script that runs off a CD and copies a bunch of files to a computer. However, in order to copy the files I need to know the complete file path, including the drive letter. How can I determine the drive letter for the drive my script is currently running [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68013","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=68013"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68013\/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=68013"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=68013"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=68013"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}