How Can I Copy Two Files to Each of the Shared Folders on a Computer?


Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I copy two files to each of the shared folders on a computer?

— DL

SpacerHey, Scripting Guy! AnswerScript Center

Hey, DL. If you don’t subscribe to the Scripting Newswire (and surely everyone subscribes to the Scripting Newswire) you might have missed the announcement that the Scripting Guys will be attending TechEd IT Forum 2007, to be held November 12-16 in Barcelona, Spain. This was a trip the Scripting Guys were really hoping to make, and not just because it seemed like a good way to scam a free trip to Europe. Not that we didn’t want to scam a free trip to Europe but, in addition to that, nearly 60% of our Script Center traffic comes from outside the USA, with a good number of those people hailing from Europe. It seemed to make good business sense, although sometimes what makes good business sense to the Scripting Guys doesn’t seem to make good business sense to anyone else.

And so we began negotiating with our managers, trying to convince them that this made good business sense. When this appeal to reason failed to work, the Scripting Guy who writes this column burst into tears. After calming him down, management relented and agreed to put up the money to send the Scripting Guys to Barcelona.

Note. What’s that? Did Microsoft management also agree to put up the money to bring the Scripting Guys back home from Barcelona? You know, that’s a good question; we’d better check into that.

At any rate, if you’ve been saying to yourself, “TechEd IT Forum 2007? Why would I want to go to TechEd IT Forum 2007?” well, now you have an answer to that question: because TedEd IT Forum is in Barcelona, a world-class city if there ever was one.

Oh, and because the Scripting Guys will be there, too. Scripting Guy Jean Ross will be on hand to chat with you and answer your scripting questions, and Scripting Guy Greg Stemp will be on hand to eat tapas, try the traditional Catalan pork stew, and gorge himself on crema catalana.

Incidentally, if you’re planning to attend TechEd IT Forum, we’d love to hear from you; just send an email to (in English, if possible). Note that Greg, in particular, would be interested in hearing more about traditional Catalan delicacies like gato d’avellanes amb gelat de vainilla (hazelnut cake with vanilla ice cream).

Of course, as we noted, TechEd IT Forum isn’t until November, which means we need to do something to pass the time between now and then. Hey, we have an idea: what do you say we write a script that copies two files to each of the shared folders on a computer? You know, a script like this one:

Const OverwriteExisting = TRUE

strComputer = “atl-fs-01”

Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)

Set colItems = objWMIService.ExecQuery(“Select * From Win32_Share Where Type = 0”)

Set objFSO = CreateObject(“Scripting.FileSystemObject”)

For Each objItem in colItems strFolderName = objItem.Name If InStr(strFolderName, “$”) = 0 Then strPath = “\\” & strComputer & “\” & strFolderName & “\” objFSO.CopyFile “C:\Scripts\Test.txt”, strPath, OverwriteExisting objFSO.CopyFile “C:\Scripts\Test2.txt”, strPath, OverwriteExisting End If Next

You say that this doesn’t sound like a very good idea after all? Well, sorry; too late now.

Good idea or not, we might as well see if we can figure out how the script performs its magic. (Legal disclaimer: The script does not actually perform magic.) We start out by defining a constant named OverwriteExisting and setting the value to True; this tells the script to overwrite any existing instances of the two files should either one be found in any of the shared folders. We then use this block of code to connect to the WMI service on the target computer (that is, the computer we want to copy the files to):

strComputer = “atl-fs-01”

Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)

After connecting to the WMI service our next task is to retrieve a list of all the shared folders on the computer. That’s what this line of code is for:

Set colItems = objWMIService.ExecQuery(“Select * From Win32_Share Where Type = 0”)

As you can see, we’re selecting all instances of the Win32_Share class where the value of the Type property is equal to 0. Why the Where clause, and why Type equals 0? As it turns out, the Win32_Share class covers more than just shared folders; it also includes shared printers, shared devices such as CD-ROM drives, and administrative shares (such as C$). Because we’re only interested in shared folders, we limit the returned data to shared items of Type 0.

Which, needless to say, will be nothing but shared folders.

After we execute the query we get back a collection of shared folders on the remote computer. Once we have that collection we set up a For Each loop to loop through all those shared folders. What’s the first thing we do inside that loop? That’s easy: we grab the value of folder’s Name property (that is, the share name) and store it in a variable named strFolderName.

The next part of the script is optional, but we thought we’d toss it in as a sort of a bonus. It’s not unusual for computers (particularly servers) to have hidden shares (shares that are not readily visible over the network). Because these shares aren’t typically accessed by users, you might not feel the need to copy files to hidden shares. But how can you identify which shares are hidden and which ones are not?

Interestingly enough, there’s no property that distinguishes a hidden share from a regular share. However, hidden shares always have a name that ends in a dollar sign (for example, Public$). What we do in our next line of code is use the InStr function to determine whether or not a $ character can be found in the folder name:

If InStr(strFolderName, “$”) = 0 Then

If a $ is found then InStr returns a value greater than 0 (technically, it returns the character position in the folder name where the $ was found). In other words, if InStr returns anything other than 0 we’ll assume that we’re dealing with a hidden share and we do not copy any files to that folder.

On the other hand, if InStr returns a 0 we’ll assume that we are not dealing with a hidden share. Therefore, the next thing we do is execute this line of code:

strPath = “\\” & strComputer & “\” & strFolderName & “\”

What we’re doing here is constructing a UNC path to the share folder, something we do by combining:


atl-fs-01 (the name of the remote computer, stored in the variable strComputer)


public (the name of the shared folder, stored in the variable strFolderName)


Put them all together and you end up with a path similar to this:


Note. Don’t leave the trailing \ off the end of the path. If you do, your script is going to fail.

Once we have the UNC path we can then copy our two files to this shared folder. How do we do that? Well, here’s how we copy the first file:

objFSO.CopyFile “C:\Scripts\Test.txt”, strPath, OverwriteExisting

As you can see, all we do is call the CopyFile method, passing along three parameters:

C:\Scripts\Test.txt, the path to the file on the local computer. Can you copy a file from one remote computer to another? Yes, you can, but that can get a little tricky. If you need to do that, your best bet is to map a drive to the remote computer and then use that file path. For example, if you map drive F: to the Scripts folder on \\atl-ps-05 then you can simply copy a file using the path F:\Scripts\Test.txt.

strPath, the variable containing the UNC path to the shared folder on the remote computer.

OverwriteExisting, the constant we defined at the beginning of the script.

After that we use a similar call to CopyFile to copy our second file, then loop around and repeat the process with the next folder in the collection.

About the only thing to watch out for here are the share permissions: if you don’t have the right to modify the shared folder over the network then the script will fail. And while it’s theoretically possible to determine, and even to modify, share permissions using a script, well, that’s something that goes way beyond what we can do in this column. (And, to be honest, is way more complicated than it really ought to be. When it comes to security descriptors, we recommend that you bypass scripting and use a command-line tool such as Cacls.exe or Xcacls.exe.)

That should do the trick, DL. Incidentally if you – or anyone else – is wondering exactly what the Scripting Guys are planning to do at TechEd IT Forum, well, to tell you the truth, we haven’t decided what all we want to do there. We do know that we’ll be giving away a bunch of Dr. Scripto bobblehead dolls; in addition, we’ll be handing out hundreds of copies of Dr. Scripto’s Fun Book. Beyond that we don’t know, although we definitely plan to spend the time just hanging out and talking to people.

At any rate, if you happen to be in Barcelona November 12-16, be sure and look us up. Jean will be the Scripting Guy with the long hair and the baseball hat; Greg will be the Scripting Guy who can’t be found anywhere, most likely because he went back for another slice of gato d’avellanes amb gelat de vainilla.


Discussion is closed.

Feedback usabilla icon