{"id":64273,"date":"2007-08-09T01:20:00","date_gmt":"2007-08-09T01:20:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/08\/09\/how-can-i-copy-two-files-to-each-of-the-shared-folders-on-a-computer\/"},"modified":"2007-08-09T01:20:00","modified_gmt":"2007-08-09T01:20:00","slug":"how-can-i-copy-two-files-to-each-of-the-shared-folders-on-a-computer","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-copy-two-files-to-each-of-the-shared-folders-on-a-computer\/","title":{"rendered":"How Can I Copy Two Files to Each of the Shared Folders on a Computer?"},"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 copy two files to each of the shared folders on a computer?<BR><BR>&#8212; DL <\/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, DL. If you don\u2019t <A href=\"mailto:scripter@microsoft.com?subject=Subscribe\"><B>subscribe<\/B><\/A> to the <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/newswire\/snwarch.mspx\"><B>Scripting Newswire<\/B><\/A> (and surely <I>everyone<\/I> subscribes to the <I>Scripting Newswire<\/I>) you might have missed the announcement that the Scripting Guys will be attending <A href=\"http:\/\/www.mseventseurope.com\/teched\/07\/itforum\/Pages\/Default.aspx\" target=\"_blank\"><B>TechEd IT Forum 2007<\/B><\/A>, to be held November 12-16 in Barcelona, Spain. This was a trip the Scripting Guys were <I>really<\/I> hoping to make, and not just because it seemed like a good way to scam a free trip to Europe. Not that we didn\u2019t <I>want<\/I> 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\u2019t seem to make good business sense to anyone else.<\/P>\n<P>And so we began negotiating with our managers, trying to convince <I>them<\/I> 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.<\/P>\n<TABLE class=\"dataTable\" id=\"E6D\" 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>. What\u2019s that? Did Microsoft management also agree to put up the money to bring the Scripting Guys back <I>home<\/I> from Barcelona? You know, that\u2019s a good question; we\u2019d better check into that.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>At any rate, if you\u2019ve been saying to yourself, \u201cTechEd IT Forum 2007? Why would I want to go to TechEd IT Forum 2007?\u201d 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.<\/P>\n<P>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 <I>crema catalana<\/I>. <\/P>\n<P>Incidentally, if you\u2019re planning to attend TechEd IT Forum, we\u2019d love to hear from you; just send an email to <A href=\"mailto:scripter@microsoft.com?subject=Barcelona\"><B>scripter@microsoft.com (in English, if possible)<\/B><\/A>. Note that Greg, in particular, would be interested in hearing more about traditional Catalan delicacies like <I>gato d&#8217;avellanes amb gelat de vainilla<\/I> (hazelnut cake with vanilla ice cream).<\/P>\n<P>Of course, as we noted, TechEd IT Forum isn\u2019t 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:<\/P><PRE class=\"codeSample\">Const OverwriteExisting = TRUE<\/p>\n<p>strComputer = &#8220;atl-fs-01&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colItems = objWMIService.ExecQuery(&#8220;Select * From Win32_Share Where Type = 0&#8221;)<\/p>\n<p>Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)<\/p>\n<p>For Each objItem in colItems\n    strFolderName = objItem.Name\n    If InStr(strFolderName, &#8220;$&#8221;) = 0 Then\n        strPath = &#8220;\\\\&#8221; &amp; strComputer &amp; &#8220;\\&#8221; &amp; strFolderName &amp; &#8220;\\&#8221;\n        objFSO.CopyFile &#8220;C:\\Scripts\\Test.txt&#8221;, strPath, OverwriteExisting\n        objFSO.CopyFile &#8220;C:\\Scripts\\Test2.txt&#8221;, strPath, OverwriteExisting\n    End If\nNext\n<\/PRE>\n<P>You say that this doesn\u2019t sound like a very good idea after all? Well, sorry; too late now.<\/P>\n<P>Good idea or not, we might as well see if we can figure out how the script performs its magic. (<B>Legal disclaimer<\/B>: 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):<\/P><PRE class=\"codeSample\">strComputer = &#8220;atl-fs-01&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)\n<\/PRE>\n<P>After connecting to the WMI service our next task is to retrieve a list of all the shared folders on the computer. That\u2019s what this line of code is for:<\/P><PRE class=\"codeSample\">Set colItems = objWMIService.ExecQuery(&#8220;Select * From Win32_Share Where Type = 0&#8221;)\n<\/PRE>\n<P>As you can see, we\u2019re selecting all instances of the <B>Win32_Share<\/B> class where the value of the <B>Type<\/B> 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\u2019re only interested in shared folders, we limit the returned data to shared items of Type 0. <\/P>\n<P>Which, needless to say, will be nothing but shared folders.<\/P>\n<P>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\u2019s the first thing we do inside that loop? That\u2019s easy: we grab the value of folder\u2019s <B>Name<\/B> property (that is, the share name) and store it in a variable named strFolderName.<\/P>\n<P>The next part of the script is optional, but we thought we\u2019d toss it in as a sort of a bonus. It\u2019s not unusual for computers (particularly servers) to have hidden shares (shares that are not readily visible over the network). Because these shares aren\u2019t 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?<\/P>\n<P>Interestingly enough, there\u2019s 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 <B>InStr<\/B> function to determine whether or not a $ character can be found in the folder name:<\/P><PRE class=\"codeSample\">If InStr(strFolderName, &#8220;$&#8221;) = 0 Then\n<\/PRE>\n<P>If a $ <I>is<\/I> 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\u2019ll assume that we\u2019re dealing with a hidden share and we do <I>not<\/I> copy any files to that folder.<\/P>\n<P>On the other hand, if InStr returns a 0 we\u2019ll assume that we are <I>not<\/I> dealing with a hidden share. Therefore, the next thing we do is execute this line of code:<\/P><PRE class=\"codeSample\">strPath = &#8220;\\\\&#8221; &amp; strComputer &amp; &#8220;\\&#8221; &amp; strFolderName &amp; &#8220;\\&#8221;\n<\/PRE>\n<P>What we\u2019re doing here is constructing a UNC path to the share folder, something we do by combining:<\/P>\n<TABLE class=\"\" cellSpacing=\"0\" cellPadding=\"0\" border=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>\\\\<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>atl-fs-01 (the name of the remote computer, stored in the variable strComputer)<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>\\<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>public (the name of the shared folder, stored in the variable strFolderName)<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>\\<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>Put them all together and you end up with a path similar to this:<\/P><PRE class=\"codeSample\">\\\\atl-fs-01\\public\\\n<\/PRE>\n<TABLE class=\"dataTable\" id=\"E2G\" 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>. Don\u2019t leave the trailing \\ off the end of the path. If you do, your script is going to fail.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Once we have the UNC path we can then copy our two files to this shared folder. How do we do that? Well, here\u2019s how we copy the first file:<\/P><PRE class=\"codeSample\">objFSO.CopyFile &#8220;C:\\Scripts\\Test.txt&#8221;, strPath, OverwriteExisting\n<\/PRE>\n<P>As you can see, all we do is call the <B>CopyFile<\/B> method, passing along three parameters:<\/P>\n<TABLE class=\"\" cellSpacing=\"0\" cellPadding=\"0\" border=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>C:\\Scripts\\Test.txt<\/B>, 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.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>strPath<\/B>, the variable containing the UNC path to the shared folder on the remote computer.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>OverwriteExisting<\/B>, the constant we defined at the beginning of the script.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>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.<\/P>\n<P>About the only thing to watch out for here are the share permissions: if you don\u2019t have the right to modify the shared folder over the network then the script will fail. And while it\u2019s theoretically possible to determine, and even to modify, share permissions using a script, well, that\u2019s 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.)<\/P>\n<P>That should do the trick, DL. Incidentally if you \u2013 or anyone else \u2013 is wondering exactly what the Scripting Guys are planning to <I>do<\/I> at TechEd IT Forum, well, to tell you the truth, we haven\u2019t decided what all we want to do there. We <I>do<\/I> know that we\u2019ll be giving away a bunch of <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/funzone\/bobbles.mspx\"><B>Dr. Scripto bobblehead dolls<\/B><\/A>; in addition, we\u2019ll be handing out hundreds of copies of <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/topics\/teched07\/swag.mspx\"><B>Dr. Scripto\u2019s Fun Book<\/B><\/A>. Beyond that we don\u2019t know, although we definitely plan to spend the time just hanging out and talking to people. <\/P>\n<P>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\u2019t be found anywhere, most likely because he went back for another slice of <I>gato d&#8217;avellanes amb gelat de vainilla<\/I>.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I copy two files to each of the shared folders on a computer?&#8212; DL Hey, DL. If you don\u2019t 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 [&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":[38,11,3,12,5],"class_list":["post-64273","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-files","tag-folders","tag-scripting-guy","tag-storage","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I copy two files to each of the shared folders on a computer?&#8212; DL Hey, DL. If you don\u2019t 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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64273","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=64273"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64273\/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=64273"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=64273"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=64273"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}