{"id":64863,"date":"2007-05-15T22:25:00","date_gmt":"2007-05-15T22:25:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/05\/15\/how-can-i-copy-the-same-file-to-a-number-of-different-computers\/"},"modified":"2007-05-15T22:25:00","modified_gmt":"2007-05-15T22:25:00","slug":"how-can-i-copy-the-same-file-to-a-number-of-different-computers","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-copy-the-same-file-to-a-number-of-different-computers\/","title":{"rendered":"How Can I Copy the Same File to a Number of Different Computers?"},"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! I regularly update a file that needs to be copied to all of my servers. How can I write a script that will copy the same file (to the same folder) on multiple computers?<BR><BR>&#8212; JU<\/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, JU. It\u2019s Monday, May 14<SUP>th<\/SUP> as we write this column, which mean that Mother\u2019s Day in the US is over. And that\u2019s good. Not that the Scripting Guys have anything against mothers, mind you. It\u2019s just that we agree with Anna Jarvis, the person largely responsible for getting Mother\u2019s Day proclaimed a holiday in the first place. After devoting years of her life to the cause, Jarvis soon came to regret having done so; she actually spent the final years of her life trying to get <I>rid<\/I> of Mother\u2019s Day. Why? Because she was extremely disappointed that a day set aside to honor mothers quickly became just another reason to sell flowers and greeting cards. She was totally aghast that, in just a few years, her simple little holiday had gone commercial.<\/P>\n<P>As a general rule the Scripting Guys are all for commercialization. (In fact, if anyone from Pepsi is reading this, don\u2019t you think <I>The Pepsi-Cola Bottling Company Presents Hey, Scripting Guy!<\/I> has a nice ring to it?) However, the combination of Mother\u2019s Day and commercialization can only mean one thing: the Scripting Dad and the Scripting Son have to go out shopping. Together.<\/P>\n<P>The Scripting Dad and the Scripting Son get along incredibly well, and they do a lot of things together. Truth be told, the Scripting Dad has always believed that if they ever had a contest for Father and Son Who Get Along the Best he and the Scripting Son would be a shoe-in \u2026 unless, of course, the contest was held at a shopping mall. At that point, all bets would be off.<\/P>\n<P>To give you a hint of what we\u2019re talking about, the two had no sooner walked into the mall when the Scripting Son found the \u2026 perfect \u2026 present. \u201cMom has an MP3 player,\u201d he said. \u201cLet\u2019s get her a case for that and then go.\u201d<\/P>\n<P>\u201cWell, we can get her the case,\u201d said the wise and thoughtful Scripting Dad. \u201cBut that\u2019s not enough; we\u2019ll have to get her something else as well.\u201d<\/P>\n<P>\u201cStop yelling at me! Every time we go shopping you end up yelling at me.\u201d<\/P>\n<P>Which, come to think of it, might very well be true.<\/P>\n<P>Believe it or not, the two eventually found a present, bought it, and made it back home safe and sound (a little bruised and battered, but both still alive). Interestingly enough, in addition to buying a present for his mom, the Scripting Son somehow managed to get two new T-shirts and a couple scoops of Double Chocolate frozen yogurt for himself out of the trip as well. <\/P>\n<P>Thank goodness he isn\u2019t spoiled.<\/P>\n<P>Oh, well. The moral of the story? If the Scripting Dad and the Scripting Son can survive a trip to the mall together, copying the same file to multiple computers ought to be a piece of cake:<\/P><PRE class=\"codeSample\">Const ForReading = 1\nConst OverwriteExisting = TRUE<\/p>\n<p>Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\nSet objFile = objFSO.OpenTextFile(&#8220;C:\\Scripts\\Computers.txt&#8221;)<\/p>\n<p>Do Until objFile.AtEndOfStream\n    strComputer = objFile.ReadLine\n    strRemoteFile = &#8220;\\\\&#8221; &amp; strComputer &amp; &#8220;\\C$\\Scripts\\Test.doc&#8221;\n    objFSO.CopyFile &#8220;C:\\Scripts\\Test.doc&#8221;, strRemoteFile, OverwriteExisting\nLoop\n<\/PRE>\n<P>Before we go much further we should point out that there are a number of different ways we could approach the issue of copying files to multiple computers. We chose to tackle this task by reading computer names from a text file. (This is known as the <I>Peter Costantini<\/I><I> Memorial Approac<\/I>h in honor of the legendary Peter Costantini, who never met a script he couldn\u2019t enhance by having it read computer names from a text file.) Would you rather select computer names from a dialog box, supply computer names as command-line arguments, or maybe grab the names of all the computers in an OU? Then you need to take a look at our <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/scripts\/templates\/default.mspx\"><B>Remote\/Multiple Computer Scripting Templates<\/B><\/A>, don\u2019t you? <\/P>\n<P>We\u2019re assuming that we have a very simple little text file (C:\\Scripts\\Computers.txt) containing the names of all the computers we need to copy the file to. That text file should look something like this:<\/P><PRE class=\"codeSample\">atl-fs-01\natl-fs-02\natl-fs-03\natl-fs-04\natl-fs-05\n<\/PRE>\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><B>Note<\/B>. In case you\u2019re wondering, this is known as the <I>Bob Wells Memorial Text File<\/I>, in honor of the legendary Bob Wells, who developed the official Scripting Guys computer naming convention back in 1768. Bob hailed from the legendary \u2013 and mythical \u2013 city of Atlanta, which is why our computer names always start with the letters <I>atl<\/I>.<\/P>\n<P>Like we said, in case you\u2019ve been wondering.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>As you might expect, our plan is to open the text file, read in the computer names, and then copy the file to each of those machines.<\/P>\n<P>To do that we start out by defining a pair of constants, ForReading and OverWriteExisting. We\u2019ll need the ForReading constant to open the text file for reading; we\u2019ll need the OverWriteExisting constant in order to copy over any existing version of the target file that might already be lingering on one of the remote computers.<\/P>\n<TABLE class=\"dataTable\" id=\"ETE\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P><B>Note<\/B>. Yes, there <I>are<\/I> a lot of nuances involved in copying files, aren\u2019t there? That\u2019s one good reason why you should read this month\u2019s <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/begin\/ss0507.mspx\"><B>Sesame Script<\/B><\/A> column.<\/P>\n<P>What do you mean you need <I>two<\/I> good reasons before you\u2019ll read the column? Fine; this month you\u2019ll also learn what the word <I>subrogation<\/I> means.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>With the constants defined we next create an instance of the <B>Scripting.FileSystemObject<\/B> object, then use the <B>OpenTextFile<\/B> method to open the file C:\\Scripts\\Computers.txt (the file containing our list of computer names):<\/P><PRE class=\"codeSample\">Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\nSet objFile = objFSO.OpenTextFile(&#8220;C:\\Scripts\\Computers.txt&#8221;)\n<\/PRE>\n<P>With the text file open our next step is to read through the file line-by-line. To do that we set up a Do Until loop designed to run until we\u2019ve reached the end of the file (or, if you prefer, until the file\u2019s <B>AtEndOfStream<\/B> property is True):<\/P><PRE class=\"codeSample\">Do Until objFile.AtEndOfStream\n<\/PRE>\n<P>The first thing we do inside that loop? We use the <B>ReadLine<\/B> method to read the initial line of the file (that is, the first computer name) and then store that value in a variable named strComputer:<\/P><PRE class=\"codeSample\">strComputer = objFile.ReadLine\n<\/PRE>\n<P>Now it gets a tiny bit tricky. If you\u2019re a devoted reader of <I>Hey, Scripting Guy!<\/I> (and you don\u2019t have to answer that; like Santa Claus, we know who\u2019s been bad or good) then you know that we always mention the fact that the FileSystemObject is designed to work against the local computer. And that\u2019s true; that\u2019s exactly what it\u2019s designed for. However, the FileSystemObject <I>can<\/I> work with UNC paths. That means that we can copy a file to a shared folder on a remote computer (for example, \\\\atl-fs-01\\Public\\Shared_Files). It also means that we can copy a file to <I>any<\/I> folder on a remote computer provided that we use the administrative share (e.g., \\\\atl-fs-01\\C$\\Scripts). In our example, we\u2019re going to use the administrative share.<\/P>\n<TABLE class=\"dataTable\" id=\"EPG\" 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>. Does that mean that this script will fail if you\u2019ve disconnected the administrative share on a computer? Yes. For better or worse you\u2019ll have to use either an administrative share or a shared folder in order to perform this task.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>In a minute or two we\u2019ll use the <B>CopyFile<\/B> method to copy the file to the first computer listed in Computers.txt. Before we can do that, however, we need two pieces of information: the complete path to the file we want to copy, and the complete path (including file name) to the location where we want the file copied. Determining the path to the file to be copied is easy; after all, that\u2019s a file stored on our local computer. (In this case, C:\\Scripts\\Test.doc). <\/P>\n<P>Determining the location on the remote computer is a bit more complciated; that\u2019s because we need to use UNC paths, and one key element of the UNC path \u2013 the computer name \u2013 will change with each iteration of our loop. Shoot; it <I>is<\/I> hopeless, isn\u2019t it?<\/P>\n<P>Oh, wait: no it\u2019s not. After all, that\u2019s what this line of code is for:<\/P><PRE class=\"codeSample\">strRemoteFile = &#8220;\\\\&#8221; &amp; strComputer &amp; &#8220;\\C$\\Scripts\\Test.doc&#8221;\n<\/PRE>\n<P>All we\u2019re doing here is constructing a UNC path, substituting the value of the variable strComputer for the computer name. When we\u2019re done combining the various parts of the path we\u2019ll end up with a value like this, which we then assign to the variable strRemoteFile:<\/P><PRE class=\"codeSample\">\\\\atl-fs-01\\C$\\Scripts\\Test.doc\n<\/PRE>\n<P>And guess what? That value just happens to be the UNC path we need in order to copy the file to the computer atl-fs-01. That\u2019s something we do using this line of code:<\/P><PRE class=\"codeSample\">objFSO.CopyFile &#8220;C:\\Scripts\\Test.doc&#8221;, strRemoteFile, OverwriteExisting\n<\/PRE>\n<P>Here, at long last, we call the CopyFile method, passing 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>The path to the local file (C:\\Scripts\\Test.doc), the file we want copied.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>The UNC path for the remote computer (stashed in the variable strRemoteFile).<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>The constant OverWriteExisting (just in case Test.doc already exists in the C:\\Scripts folder on atl-fs-01).<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>We copy the file, then loop around and repeat the process with the next computer in the list.<\/P>\n<P>And that, as they say in the movie business, is a wrap.<\/P>\n<P>That should get you started, JU. And don\u2019t worry: we wouldn\u2019t let you go without one last Mother\u2019s Day anecdote. When the Scripting Son was a few months old his Scripting Mom went back to work, and the Scripting Son started going to a day care in downtown Seattle. For Mother\u2019s Day the babies all \u201cmade\u201d their mothers little ceramic handprints engraved with their names and the date. (Yes, very cute.) When the Scripting Dad went to pick up the Scripting Son the Friday before Father\u2019s Day he assumed he\u2019d get a similar gift. Instead, he got absolutely nothing. He jokingly mentioned this to one of the day care providers, who replied \u2013 in all seriousness \u2013 \u201cWe don\u2019t do anything for Father\u2019s Day. Most children don\u2019t have fathers.\u201d<\/P>\n<P>Obviously the Scripting Dad should have paid more attention in his high school biology class.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I regularly update a file that needs to be copied to all of my servers. How can I write a script that will copy the same file (to the same folder) on multiple computers?&#8212; JU Hey, JU. It\u2019s Monday, May 14th as we write this column, which mean that Mother\u2019s Day in [&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-64863","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! I regularly update a file that needs to be copied to all of my servers. How can I write a script that will copy the same file (to the same folder) on multiple computers?&#8212; JU Hey, JU. It\u2019s Monday, May 14th as we write this column, which mean that Mother\u2019s Day in [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64863","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=64863"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64863\/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=64863"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=64863"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=64863"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}