How Can I Map a Drive, Copy a File to That Drive, and Then Unmap the Drive?

How Can I Map a Drive, Copy a File to That Drive, and Then Unmap the Drive?

  • Comments 1
  • Likes
Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I map a drive, copy a file to that drive, and then unmap the drive?

-- ZK

SpacerHey, Scripting Guy! AnswerScript Center

Hey, ZK. You know, sometimes these questions are difficult for us to answer, simply because we don’t usually find ourselves performing the task being asked about. Remove every other line in a text file? Well, we’ve 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’t have an answer for you right off the top of our heads, but we’re pretty sure we can sit down and come up with something.

Ah, but this question is different. Perform some chore and then almost immediately have to undo it? That’s something we have plenty of experience with:

Set objNetwork = CreateObject("Wscript.Network")
objNetwork.MapNetworkDrive "Z:", "\\atl-fs-01\Public"

Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile "C:\Scripts\Test.txt", "Z:\"

objNetwork.RemoveNetworkDrive "Z:"

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 Wscript.Network 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’t 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:

objNetwork.MapNetworkDrive "Z:", "\\atl-fs-01\Public"

That’s a good question: what if drive Z is already mapped to some other location? Well, in that case, the script will fail; you can’t map a drive to a drive letter that’s already in use. But don’t worry; if that’s a concern just take a look at this Hey, Scripting Guy! column, which shows you how you can determine (and use) the next available drive letter in a script.

After the drive is mapped we’re ready to copy the file to that location. To do that we create an instance of the Scripting.FileSystemObject, then use the CopyFile method to copy the file C:\Scripts\Test.txt to drive Z. That’s what we do with these two lines of code:

Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile "C:\Scripts\Test.txt", "Z:\"

And that’s a good point: if the file Test.txt already exists on drive Z then the script will fail here, too; that’s because, by default, the FileSystemObject won’t overwrite an existing file. But, again, that’s nothing to worry about: just add the optional parameter True to enable the FileSystemObject to overwrite an existing copy of the file. If it’s OK to copy over Test.txt then just change the CopyFile command to look like this:

objFSO.CopyFile "C:\Scripts\Test.txt", "Z:\", True

Believe it or not, we’re almost done. (Man, we really do like answering questions like this!) All that’s left is to unmap drive Z, something we can do by calling the RemoveNetworkDrive method and specifying the drive letter of the drive to be removed:

objNetwork.RemoveNetworkDrive "Z:"

And that’s it. If you take a look at your local computer you won’t see a drive Z. And if you take a look at \\atl-fs-01\Public you should see a copy of Test.txt. We ended up exactly where we wanted to end up.

Which, now that you mention it, is a bit unusual for us, isn’t it?

So do the Scripting Guys really find themselves undoing things they just did or were we just being facetious? Let’s 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’t that … amusing ….

Fortunately the Scripting Guy who writes this column is used to stuff like that. Does something need new batteries? Then it’s a pretty safe bet that we will take the cover off the thing, remove the old batteries, and carefully replace the cover. It’s only then that he’ll 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’ll end up making before he comes back with eggs rather than, say, milk or bread. (Hint: One is not the correct answer.)

In other words, all in a day’s work.

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment