{"id":67023,"date":"2006-06-27T15:32:00","date_gmt":"2006-06-27T15:32:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/06\/27\/how-can-i-copy-a-file-numerous-times-and-rename-each-of-them-from-a-list-in-excel\/"},"modified":"2006-06-27T15:32:00","modified_gmt":"2006-06-27T15:32:00","slug":"how-can-i-copy-a-file-numerous-times-and-rename-each-of-them-from-a-list-in-excel","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-copy-a-file-numerous-times-and-rename-each-of-them-from-a-list-in-excel\/","title":{"rendered":"How Can I Copy a File Numerous Times and Rename Each of Them from a List in Excel?"},"content":{"rendered":"<p><IMG 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\"> \n<P>Hey, Scripting Guy! How can I copy a file 150 times and then rename each of those copies using a list of names found in an Excel spreadsheet?<BR><BR>&#8212; DF<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG 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 class=\"farGraphic\" title=\"TechNet Script Center\" border=\"0\" alt=\"TechNet Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, DF. You know, when we first started this column 2 years ago (must be time to mention the <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/qanda\/postcards\/default.mspx\"><B>500<\/B><SUP>th<\/SUP><B> Hey, Scripting Guy! celebration<\/B><\/A> again) our biggest concern was this: what are we going to do if no one ever writes in with a question? We had stockpiled a few questions, enough to get us through the first month, but after those first 30 days, then what? <\/P>\n<P>Well, if you\u2019ve ever wondered just how clueless the Scripting Guys really are, let\u2019s put it this way: nowadays this column receives 20 or more questions each and every day. For those of you who occasionally write in asking why we haven\u2019t answered your question, well, that\u2019s probably why: to say that we have a backlog of questions waiting to be answered would be a bit of an understatement.<\/P>\n<P>So then why did we answer DF\u2019s question? No reason, really; it\u2019s just luck of the draw more than anything else. OK, sure, DF <I>did<\/I> add the following P.S. to his email, but the Scripting Guys aren\u2019t swayed by such a blatant attempt to curry their favor:<\/P>\n<P>\u201cI love the column. It\u2019s one of my daily tasks to visit the site and see what you are doing that day.\u201d<\/P>\n<P>Sorry, DF: the Scripting Guys can\u2019t be bribed. (Well, actually we can easily be <I>bribed<\/I>; we just can\u2019t be flattered.)<\/P>\n<P>By astonishing coincidence, however, another reader named DF asked us the exact same question. And since this other DF didn\u2019t try to butter us up we decided it would be OK to answer his or her question. Just as long as we didn\u2019t answer the first DF\u2019s question; after all, the professional integrity of the <I>Hey, Scripting Guy!<\/I> column is at stake here.<\/P>\n<P>Or at least it would be if the column actually <I>had<\/I> any professional integrity. But that\u2019s beside the point.<\/P>\n<P>Here\u2019s the solution we came up with:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colFiles = objWMIService. _\n    ExecQuery(&#8220;Select * From CIM_DataFile Where Name = &#8216;C:\\\\Scripts\\\\Test.txt'&#8221;)<\/p>\n<p>Set objExcel = CreateObject(&#8220;Excel.Application&#8221;)\nobjExcel.Visible = True<\/p>\n<p>Set objWorkbook = objExcel.Workbooks.Open(&#8220;C:\\Scripts\\Names.xls&#8221;)<\/p>\n<p>intRow = 1<\/p>\n<p>Do Until objExcel.Cells(intRow,1).Value = &#8220;&#8221;\n    strNewName = objExcel.Cells(intRow, 1).Value<\/p>\n<p>    For Each objFile in colFiles\n        strCopy = &#8220;C:\\Scripts\\&#8221; &amp; strNewName\n        objFile.Copy(strCopy)\n    Next<\/p>\n<p>    intRow = intRow + 1\nLoop<\/p>\n<p>objExcel.Quit\n<\/PRE>\n<P>We start out with some basic WMI code that binds us to the file we want to copy. To do that, we assign the value dot (.) to a variable named strComputer; in a minute we\u2019ll use this variable to connect to the WMI service on the local computer. What\u2019s that? What if you want to bind to the WMI service (and to a file) stored on a remote computer? No problem; just assign the variable strComputer the name of that remote machine:<\/P><PRE class=\"codeSample\">strComputer = &#8220;atl-fs-01&#8221;\n<\/PRE>\n<P>After we connect to the WMI service we then use this line of code to bind to the file C:\\Scripts\\Test.txt:<\/P><PRE class=\"codeSample\">Set colFiles = objWMIService. _\n    ExecQuery(&#8220;Select * From CIM_DataFile Where Name = &#8216;C:\\\\Scripts\\\\Test.txt'&#8221;)\n<\/PRE>\n<P>No, we haven\u2019t been drinking (why does everyone always assume that?); we really <I>do<\/I> need to indicate the path as C:\\\\Scripts\\\\Test.txt. That\u2019s because the \\ is a reserved character in WMI; one of the consequences of being a reserved character is that any time we use the \\ in a Where clause we need to \u201cescape\u201d the character, which simply means we need to preface any \\ in the path with a second \\. Thus, C:\\Scripts\\Test.txt ends up as C:\\\\Scripts\\\\Test.txt.<\/P>\n<TABLE id=\"EEE\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. In all honesty, the Scripting Guys never take a good, stiff drink before writing this column. However, we have heard that some people take a good, stiff drink before <I>reading<\/I> this column. But that\u2019s nothing to be ashamed of; our editor has to do that, too.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>After binding to Test.txt (which, we should probably point out, is the file we\u2019re going to copy 150 times) we use these lines of code to create a visible instance of Microsoft Excel and open the spreadsheet C:\\Scripts\\Names.xls (the Excel document that contains all our new file names):<\/P><PRE class=\"codeSample\">Set objExcel = CreateObject(&#8220;Excel.Application&#8221;)\nobjExcel.Visible = True<\/p>\n<p>Set objWorkbook = objExcel.Workbooks.Open(&#8220;C:\\Scripts\\Names.xls&#8221;)\n<\/PRE>\n<P>Now it\u2019s time to take a good, stiff drink and get to work.<\/P>\n<P>Um, by which we mean now it\u2019s time to \u2026 roll up our sleeves \u2026 and get to work.<\/P>\n<P>To begin with, we assign the value 1 to a variable named intRow; we\u2019re going to use this variable to keep track of the current row in the spreadsheet. Speaking of which, we\u2019re assuming this spreadsheet simply lists file names in column A, that the worksheet does not have a header row, and that there are no blank spaces anywhere in the list. If that\u2019s not the case, you\u2019ll probably need to modify the code for navigating through the spreadsheet. For information on how you might do that, check out the <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/officetips\/archive.mspx\"><B>Office Space archive<\/B><\/A>.<\/P>\n<P>Our next step is to set up a Do Until loop that runs until we encounter a blank cell in column A; that loop looks like this:<\/P><PRE class=\"codeSample\">Do Until objExcel.Cells(intRow,1).Value = &#8220;&#8221;\n<\/PRE>\n<P>As you can see, we use the variable intRow to indicate the cell row; because intRow is initially assigned the value 1 that means we\u2019re looking at cell row 1, column 1 (or cell A1). If this cell is blank we exit the loop. <\/P>\n<P>But what it the cell <I>isn\u2019t<\/I> blank? Well, in that case we assign the value of the cell to a variable named strNewName (as you recall, the spreadsheet is simply a list of new file names, like Test001.txt, Test002.txt, etc.):<\/P><PRE class=\"codeSample\">strNewName = objExcel.Cells(intRow, 1).Value\n<\/PRE>\n<P>That brings us to this block of code:<\/P><PRE class=\"codeSample\">For Each objFile in colFiles\n    strCopy = &#8220;C:\\Scripts\\&#8221; &amp; strNewName\n    objFile.Copy(strCopy)\nNext\n<\/PRE>\n<P>What we have here is a For Each loop that walks through the collection of items returned by our WMI query. Granted, there can only be, at most, one file in this collection, the file C:\\Scripts\\Test.txt. However, even a one-item collection is still a collection, which is why we need the For Each loop. Inside that loop we use this line of code to construct a path for this first file copy:<\/P><PRE class=\"codeSample\">strCopy = &#8220;C:\\Scripts\\&#8221; &amp; strNewName\n<\/PRE>\n<P>We should note that this line of code might not be needed; if your Excel spreadsheet features full file paths (C:\\Scripts\\Test001.txt) instead of just file names (Test001.txt) then you don\u2019t need to manually create the path. Because DF &#8211; that is, because the <I>second<\/I> DF &#8211; specifically said file <I>names<\/I>, we decided to play it safe and construct a path consisting of the folder &#8211; <B>C:\\Scripts\\<\/B> &#8212; and the file name.<\/P>\n<P>Why do we even <I>need<\/I> a complete path? There\u2019s a good reason for that: we\u2019re about to copy the file using WMI, and WMI\u2019s <B>Copy<\/B> method requires a complete file path, even if you\u2019re copying the file to the same folder as the target file.<\/P>\n<P>Speaking of WMI\u2019s Copy method, in our very next line of code we use that method to copy the file Test.txt to the new file:<\/P><PRE class=\"codeSample\">objFile.Copy(strCopy)\n<\/PRE>\n<P>As you can see, there\u2019s nothing much to this: we simply call the Copy method, passing as the sole parameter the path to the new file. When we finish with this call we\u2019ll have two identical files: C:\\Scripts\\Test.txt and C:\\Scripts\\Test001.txt (or whatever file name we had in cell A1 of our spreadsheet). We increment the variable intRow by 1 and then loop around and repeat this process with the next name\/cell in the spreadsheet.<\/P>\n<TABLE id=\"EBG\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. We knew that <I>someone<\/I> would ask that: we increment intRow by 1 simply because this is the variable we use to determine the row in the spreadsheet. Having just taken care of row 1, we now need to move down to row 2. The only way to do that? Make sure that intRow is now equal to 2.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Eventually, we\u2019ll have copied Test.txt 150 times, once for each entry in the spreadsheet. Of course, one of the nice things about this script is that you aren\u2019t limited to making exactly 150 copies. Instead, the script will make as many copies as there are file names listed in the spreadsheet. <\/P>\n<P>So there you have it, DF. No, not you, the, uh, <I>other<\/I> DF \u2026. <\/P>\n<TABLE id=\"EVG\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P><B>Note<\/B>. OK, we admit it: all kidding aside we really did appreciate DF\u2019s &#8211; the <I>real<\/I> DF\u2019s &#8211; note; it\u2019s nice to know that people find this column useful. Thanks, DF. That said, we still can\u2019t guarantee that we\u2019ll ever be able answer every question that comes our way. However, there might be a way to answer at least some of those &#8211; hold on a second while we distract our editor. Say, isn\u2019t that a dangling participle over there?<\/P>\n<P>OK, she\u2019s gone. Now, didn\u2019t we hear something about bribery .\u2026<\/P>\n<P><I>Editor\u2019s Note: She\u2019s back, and hasn\u2019t had nearly enough to drink to let that get by. In case there are any lawyers out there reading this &#8211; and we know this is a very popular column with the lawyers &#8211; we were just kidding, the Scripting Guys do not accept bribes. Or if they do the editor knows nothing about it<\/I><\/P><\/TD><\/TR><\/TBODY><\/TABLE><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I copy a file 150 times and then rename each of those copies using a list of names found in an Excel spreadsheet?&#8212; DF Hey, DF. You know, when we first started this column 2 years ago (must be time to mention the 500th Hey, Scripting Guy! celebration again) our [&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":[710,711,48,3,5,6],"class_list":["post-67023","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-excel-spreadsheet","tag-excel-application","tag-microsoft-excel","tag-scripting-guy","tag-vbscript","tag-wmi"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I copy a file 150 times and then rename each of those copies using a list of names found in an Excel spreadsheet?&#8212; DF Hey, DF. You know, when we first started this column 2 years ago (must be time to mention the 500th Hey, Scripting Guy! celebration again) our [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67023","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=67023"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67023\/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=67023"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=67023"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=67023"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}