{"id":66053,"date":"2006-11-13T14:00:00","date_gmt":"2006-11-13T14:00:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/11\/13\/how-can-i-map-a-drive-copy-a-file-to-that-drive-and-then-unmap-the-drive\/"},"modified":"2006-11-13T14:00:00","modified_gmt":"2006-11-13T14:00:00","slug":"how-can-i-map-a-drive-copy-a-file-to-that-drive-and-then-unmap-the-drive","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-map-a-drive-copy-a-file-to-that-drive-and-then-unmap-the-drive\/","title":{"rendered":"How Can I Map a Drive, Copy a File to That Drive, and Then Unmap the Drive?"},"content":{"rendered":"<p><img decoding=\"async\" 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\"><\/p>\n<p>Hey, Scripting Guy! How can I map a drive, copy a file to that drive, and then unmap the drive?<\/p>\n<p>&#8212; ZK<\/p>\n<p><img decoding=\"async\" border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><img decoding=\"async\" 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 decoding=\"async\" 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><\/p>\n<p>Hey, ZK. You know, sometimes these questions are difficult for us to answer, simply because we don\u2019t usually find ourselves performing the task being asked about. Remove every other line in a text file? Well, we\u2019ve never done that, but we can probably figure it out given a little time and effort. Take all the data found in a column in an Excel spreadsheet and convert that to an array? Same thing: we don\u2019t have an answer for you right off the top of our heads, but we\u2019re pretty sure we can sit down and come up with something.<\/p>\n<p>Ah, but this question is different. Perform some chore and then almost immediately have to undo it? <i>That\u2019s<\/i> something we have plenty of experience with:<\/p>\n<pre class=\"codeSample\">Set objNetwork = CreateObject(\"Wscript.Network\")\nobjNetwork.MapNetworkDrive \"Z:\", \"\\\\atl-fs-01\\Public\"\nSet objFSO = CreateObject(\"Scripting.FileSystemObject\")\nobjFSO.CopyFile \"C:\\Scripts\\Test.txt\", \"Z:\\\"\nobjNetwork.RemoveNetworkDrive \"Z:\"\n<\/pre>\n<p>As you can see, this is probably much easier than you thought it would be. (You have to assume that if the Scripting Guys know how to do it it must not be too-terribly complicated.) We start out by creating an instance of the <b>Wscript.Network<\/b> object, the scripting object needed to map a drive on a computer. (We should add that the Network object lets you map a drive on only the local computer; you can\u2019t modify this script to map a drive on a remote machine.) We then use this line of code to map drive Z to the UNC path \\\\atl-fs-01\\Public:<\/p>\n<pre class=\"codeSample\">objNetwork.MapNetworkDrive \"Z:\", \"\\\\atl-fs-01\\Public\"\n<\/pre>\n<p>That\u2019s a good question: what <i>if<\/i> drive Z is already mapped to some other location? Well, in that case, the script will fail; you can\u2019t map a drive to a drive letter that\u2019s already in use. But don\u2019t worry; if that\u2019s a concern just take a look at this <a href=\"http:\/\/null\/technet\/scriptcenter\/resources\/qanda\/jan05\/hey0122.mspx\"><b><i>Hey, Scripting Guy!<\/i><\/b><\/a> column, which shows you how you can determine (and use) the next available drive letter in a script.<\/p>\n<p>After the drive is mapped we\u2019re ready to copy the file to that location. To do that we create an instance of the <b>Scripting.FileSystemObject<\/b>, then use the <b>CopyFile<\/b> method to copy the file C:\\Scripts\\Test.txt to drive Z. That\u2019s what we do with these two lines of code:<\/p>\n<pre class=\"codeSample\">Set objFSO = CreateObject(\"Scripting.FileSystemObject\")\nobjFSO.CopyFile \"C:\\Scripts\\Test.txt\", \"Z:\\\"\n<\/pre>\n<p>And that\u2019s a good point: if the file Test.txt already exists on drive Z then the script will fail here, too; that\u2019s because, by default, the FileSystemObject won\u2019t overwrite an existing file. But, again, that\u2019s nothing to worry about: just add the optional parameter <b>True<\/b> to enable the FileSystemObject to overwrite an existing copy of the file. If it\u2019s OK to copy over Test.txt then just change the CopyFile command to look like this:<\/p>\n<pre class=\"codeSample\">objFSO.CopyFile \"C:\\Scripts\\Test.txt\", \"Z:\\\", True\n<\/pre>\n<p>Believe it or not, we\u2019re almost done. (Man, we really <i>do<\/i> like answering questions like this!) All that\u2019s left is to unmap drive Z, something we can do by calling the <b>RemoveNetworkDrive<\/b> method and specifying the drive letter of the drive to be removed:<\/p>\n<pre class=\"codeSample\">objNetwork.RemoveNetworkDrive \"Z:\"\n<\/pre>\n<p>And that\u2019s it. If you take a look at your local computer you won\u2019t see a drive Z. And if you take a look at \\\\atl-fs-01\\Public you <i>should<\/i> see a copy of Test.txt. We ended up exactly where we wanted to end up.<\/p>\n<p>Which, now that you mention it, <i>is<\/i> a bit unusual for us, isn\u2019t it?<\/p>\n<p>So do the Scripting Guys <i>really<\/i> find themselves undoing things they just did or were we just being facetious? Let\u2019s put it this way. Awhile back the Scripting Guy who writes this column sat down to answer a question. Not only did he know the answer to the question, but he found the column amazingly easy to write: the words just flowed. Moments before he published the article he discovered why the column was so easy to write: just a few weeks before he had answered that very same question and written that exact same column. Isn\u2019t that \u2026 amusing \u2026.<\/p>\n<p>Fortunately the Scripting Guy who writes this column is used to stuff like that. Does something need new batteries? Then it\u2019s a pretty safe bet that we will take the cover off the thing, remove the old batteries, and carefully replace the cover. It\u2019s only then that he\u2019ll realize the new batteries are still sitting there. Does he need to run to the store and get eggs (and only eggs)? You can probably guess how many trips to the store he\u2019ll end up making before he comes back with <i>eggs<\/i> rather than, say, milk or bread. (Hint: One is <i>not<\/i> the correct answer.)<\/p>\n<p>In other words, all in a day\u2019s work.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I map a drive, copy a file to that drive, and then unmap the drive? &#8212; ZK Hey, ZK. You know, sometimes these questions are difficult for us to answer, simply because we don\u2019t usually find ourselves performing the task being asked about. Remove every other line in a text [&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-66053","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 a drive, copy a file to that drive, and then unmap the drive? &#8212; ZK Hey, ZK. You know, sometimes these questions are difficult for us to answer, simply because we don\u2019t usually find ourselves performing the task being asked about. Remove every other line in a text [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66053","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=66053"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66053\/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=66053"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=66053"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=66053"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}