{"id":65083,"date":"2007-04-13T21:06:00","date_gmt":"2007-04-13T21:06:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/04\/13\/how-can-i-map-drive-x-on-a-computer-but-only-if-drive-x-isnt-already-in-use\/"},"modified":"2007-04-13T21:06:00","modified_gmt":"2007-04-13T21:06:00","slug":"how-can-i-map-drive-x-on-a-computer-but-only-if-drive-x-isnt-already-in-use","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-map-drive-x-on-a-computer-but-only-if-drive-x-isnt-already-in-use\/","title":{"rendered":"How Can I Map Drive X on a Computer, But Only If Drive X isn\u2019t Already in Use?"},"content":{"rendered":"<p><IMG 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\"> \n<P>Hey, Scripting Guy! How can I map drive X on a computer, but only if drive X isn\u2019t already in use?<BR><BR>&#8212; PN<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG 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 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> \n<P>Hey, PN. You know, this is almost too good to be true (but, sadly, it <I>is<\/I> true). The Scripting Guy who writes this column was busy writing this column. And because today just happens to be Friday the 13<SUP>th<\/SUP> the theme for the day was, as you might expect, Friday the 13<SUP>th<\/SUP>. <\/P>\n<P>In typical <I>Hey, Scripting Guy!<\/I> fashion, he began by recounting a little bit of the history of both Friday and of the number 13. For example, in both Hindu and Norse culture, it\u2019s unlucky to invite 13 people over for dinner (especially if you\u2019re the one who\u2019s supposed to do all the cooking). In the Middle Ages 13 was considered bad because witches supposedly met in groups of 13: 12 witches plus the Devil.<\/P>\n<TABLE class=\"dataTable\" id=\"EKD\" 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>. The Scripting Editor tells us that this is ridiculous; after all, her coven hardly ever has 13 people at a meeting.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Likewise, Fridays have long been viewed with a jaundiced eye. For example, tradition holds that Adam and Eve first tasted the forbidden fruit on a Friday; needless to say, that didn\u2019t work out too well for the two of them. In ancient Rome Fridays served as Execution Day, something that was apparently so popular that the British adopted the idea and turned every Friday into Hangman\u2019s Day. <\/P>\n<P>Here at Microsoft we\u2019ve carried on that tradition in equally-ominous fashion: Friday is a popular day for team meetings.<\/P>\n<P>At any rate, the Scripting Guy who writes this column went on to relate that it was inevitable that Friday \u2013 the worst day of the week \u2013 would eventually be paired with 13, the unluckiest number. The net result? Friday the 13<SUP>th<\/SUP>, a day when something terrible is <I>bound<\/I> to happen. At that point, of course, the Scripting Guy who writes this column was about to dismiss this all as superstitious nonsense when it happened: his screen went black, the computer rebooted itself, and, upon restarting, displayed this message:<\/P><PRE class=\"codeSample\">Non-system or disk error.\n<\/PRE>\n<P>Uh-oh. <\/P>\n<P>Believe it or not, in the middle of writing a column debunking the idea of Friday the 13<SUP>th<\/SUP> being unlucky our computer died. If it wasn\u2019t <I>our<\/I> computer we\u2019d find that kind of funny. <I>(The Scripting <\/I><I>Ed<\/I><I>itor found it somewhat amusing.)<\/I><\/P>\n<TABLE class=\"dataTable\" id=\"EPE\" 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>. No, we don\u2019t believe this was karma, either. However, we aren\u2019t going to say that just in case it <I>was<\/I> karma. We\u2019re having enough problems today without adding bad karma to the mix.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>So do we still think that this notion about Friday the 13<SUP>th<\/SUP> being unlucky is nothing but superstitious nonsense? Of course. After all, Friday the 13<SUP>th<\/SUP> might not have been particularly lucky for us (or at least for our computer), but it turned out to be plenty lucky for PN:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService=GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colItems = objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_LogicalDisk Where DeviceID = &#8216;X:'&#8221;)<\/p>\n<p>If colItems.Count = 0 Then\n    Set objNetwork = CreateObject(&#8220;Wscript.Network&#8221;)\n    objNetwork.MapNetworkDrive &#8220;X:&#8221;, &#8220;\\\\atl-fs-01\\public&#8221;\nEnd If\n<\/PRE>\n<P>As you can see, this is a pretty easy little script to write. (Which is good, seeing as we no longer have a computer on which to write it.) We start out, as we so often do, by connecting to the WMI service on the local computer. Could we run this same script on a remote computer? Well, maybe. It\u2019s easy enough to determine whether drive X is in use on a remote machine; unfortunately, it\u2019s a bit harder to map a drive on a remote machine. That\u2019s because Windows Script Host, and the WSH Network object, are designed to work locally rather than remotely. (And yes, it <I>is<\/I> interesting that something named the Network object can\u2019t function over the network, isn\u2019t it?) On the other hand, you can always find workarounds for these things; for one suggestion, see <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/sept04\/hey0901.mspx\"><B>this previous<\/B><B><I> Hey, Scripting Guy!<\/I><\/B><B> column<\/B><\/A>.<\/P>\n<P>After we make the connection we then use the <B>ExecQuery<\/B> method, and the following line of code, to return a collection of all the drives on the computer that have a <B>DeviceID<\/B> equal to X:<\/P><PRE class=\"codeSample\">Set colItems = objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_LogicalDisk Where DeviceID = &#8216;X:'&#8221;)\n<\/PRE>\n<P>Why do we do that? Well, this is a quick and easy way of determining whether drive X is already in use. If drive X is already mapped (to either a physical drive or a network drive) then we\u2019ll end up with one item in our collection. If drive X is <I>not<\/I> in use (meaning that the drive letter is available to us) then we\u2019ll have zero items in our collection.<\/P>\n<P>With that in mind, our next step is to check the collection <B>Count<\/B> to determine whether any drive Xs are in use:<\/P><PRE class=\"codeSample\">If colItems.Count = 0 Then\n<\/PRE>\n<P>If the Count is equal to 0, then drive X is available. Therefore, we go ahead and create an instance of the <B>Wscript.Network<\/B> object and then use the <B>MapNetworkDrive<\/B> method to map the folder \\\\atl-fs-01\\public to drive X:<\/P><PRE class=\"codeSample\">Set objNetwork = CreateObject(&#8220;Wscript.Network&#8221;)\nobjNetwork.MapNetworkDrive &#8220;X:&#8221;, &#8220;\\\\atl-fs-01\\public&#8221;\n<\/PRE>\n<P>What if the Count <I>isn\u2019t<\/I> equal to 0? Well, that can only mean one thing: drive X must already be in use. Therefore, and per PN\u2019s wishes, we don\u2019t do anything at all.<\/P>\n<TABLE class=\"dataTable\" id=\"EXG\" 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 drive X is in use wouldn\u2019t it be better to go ahead and map the drive to the next available drive letter? Maybe. If that\u2019s something you\u2019d like to do then you\u2019re in luck, too: we just happen to have <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/jan05\/hey0122.mspx\"><B>another column<\/B><\/A> on drive mapping, this one showing how you can determine the next available drive letter on a computer.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Anyway, we hope that helps, PN. And like we said, don\u2019t worry too much about it being Friday the 13<SUP>th<\/SUP>; theScripting Guys don\u2019t believe that Friday the 13<SUP>th<\/SUP> is any worse than any other Friday. After all, being a Friday (13<SUP>th<\/SUP> or otherwise) meant that we had to come in to work today. That pretty much ruled out this being a <I>lucky<\/I> day right there, regardless of whether our not any of our computers died.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I map drive X on a computer, but only if drive X isn\u2019t already in use?&#8212; PN Hey, PN. You know, this is almost too good to be true (but, sadly, it is true). The Scripting Guy who writes this column was busy writing this column. And because today just [&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":[3,185,12,5],"class_list":["post-65083","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-shared-folders-and-mapped-drives","tag-storage","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I map drive X on a computer, but only if drive X isn\u2019t already in use?&#8212; PN Hey, PN. You know, this is almost too good to be true (but, sadly, it is true). The Scripting Guy who writes this column was busy writing this column. And because today just [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65083","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=65083"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65083\/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=65083"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=65083"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=65083"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}