{"id":54703,"date":"2009-01-01T11:50:00","date_gmt":"2009-01-01T11:50:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2009\/01\/01\/hey-scripting-guy-how-can-i-copy-a-file-multiple-times-and-give-a-unique-name-to-each-file\/"},"modified":"2009-01-01T11:50:00","modified_gmt":"2009-01-01T11:50:00","slug":"hey-scripting-guy-how-can-i-copy-a-file-multiple-times-and-give-a-unique-name-to-each-file","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-copy-a-file-multiple-times-and-give-a-unique-name-to-each-file\/","title":{"rendered":"Hey, Scripting Guy! How Can I Copy a File Multiple Times and Give a Unique Name to Each File?"},"content":{"rendered":"<h2><img decoding=\"async\" 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\"> <\/h2>\n<p>Hey, Scripting Guy! I have a file that I would like to copy a number of times. This file is basically a template. I have an Excel spreadsheet that contains a list of names that I would like to use for the newly copied files. Can you help me?<\/p>\n<p>&#8211; PR<\/p>\n<p><img decoding=\"async\" height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><img decoding=\"async\" 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\"><\/p>\n<p>Hi PR,<\/p>\n<p>Sorry, I\u2019m having another music flashback. I am sitting here listening to <a href=\"http:\/\/encarta.msn.com\/trio.html\" target=\"_blank\">The Kingston Trio<\/a> singing &#8220;The Escape of Old John Webb,&#8221; and I\u2019m sipping a cup of Constant Comment tea with a cinnamon stick in it (I do not like the harsher flavor of cassia). It is a rather cold and foggy day in <a href=\"http:\/\/maps.live.com\/?q=charlotte%20north%20carolina&amp;mkt=en-US&amp;FORM=BYLH\" target=\"_blank\">Charlotte, North Carolina, USA<\/a>, and from my window I can see the trees shivering so much their leaves have left. Speaking of flashbacks, it seems we have had a question like this before. Let&#8217;s turn on the way-back machine, which is how we will find <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/jun06\/hey0627.mspx\" target=\"_blank\">this article<\/a> that was written for VBScript. The Kingston Trio is over. I have grabbed my Zune, and turned on <a href=\"http:\/\/en.wikipedia.org\/wiki\/Vampire_Weekend_%28album%29\" target=\"_blank\">Vampire Weekend<\/a>. Let&#8217;s update the script to Windows PowerShell.<\/p>\n<table class=\"dataTable\" id=\"EID\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\">For information about working with files and folders in Windows PowerShell see <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/pstips\/oct07\/pstip1005.mspx\" target=\"_blank\">this article<\/a>. You may also wish to see <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/topics\/msh\/cmdlets\/files.mspx\" target=\"_blank\">this section<\/a> of &#8220;What Can I Do with Windows PowerShell&#8221; that deals specifically with files and folders. The <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx\" target=\"_blank\">Windows PowerShell scripting hub<\/a> is a good place to get started with Windows PowerShell, and it includes download links and other items of use. The <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/begin\/archive.mspx\" target=\"_blank\">Sesame Script archive<\/a> has several VBScript articles dealing with files and folders. They are worth a look for VBScript examples. The community script repository has a good selection of <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/csc\/scripts\/files\/default.mspx\" target=\"_blank\">files and folders scripts<\/a> in VBscript. You also may want to check the <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/files.mspx\" target=\"_blank\">Hey, Scripting Guy! archive<\/a> for a number of VBScript examples of working with files and folders.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>Here\u2019s today\u2019s <\/p>\n<p>Here\u2019s today\u2019s script:<\/p>\n<pre class=\"codeSample\">$templateFile = \"C:\\fso\\FoldersAndFilesTemplate.txt\"\n$excelSheet = \"C:\\fso\\FoldersAndFiles.xls\"\n$excel = New-Object -comobject Excel.Application\n$excel.visible = $false\n$workbook = $excel.Workbooks.Open($excelSheet)\n$worksheet = $workbook.sheets.item(1)\n$rowMax = ($worksheet.usedRange.rows).count\n$column = 1\nFor($row = 1 ; $row -le $rowMax ; $row ++)\n{\n $fileName = $excel.cells.item($row,$column).value2\n $fileName += \".txt\"\n \"copying $fileName\"\n Copy-Item -path $templateFile -Destination \"C:\\fso\\fso1\\$fileName\"\n}\n$excel.quit()\n<\/pre>\n<p>Now the first thing we do is assign a string to the <b>$templateFile<\/b> variable for the path to the template file. This is seen here:<\/p>\n<pre class=\"codeSample\">$templateFile = \"C:\\fso\\FoldersAndFilesTemplate.txt\"<\/pre>\n<p>The next thing we do is assign a string for the path to the Microsoft Excel spreadsheet. The <b>$excelSheet<\/b> variable is used to hold that path: <\/p>\n<pre class=\"codeSample\">$excelSheet = \"C:\\fso\\FoldersAndFiles.xls\"<\/pre>\n<p>We next create the <b>Excel.Application<\/b> object. When we work with Microsoft Excel from within a script, we always need to create an instance of the <b>Excel.Application<\/b> object. This is a COM object and is the same one used in VBScript. We use the <b>New-Object<\/b> cmdlet with the <b>\u2013comobject<\/b> parameter to create the object. We store the returned object in the <b>$excel<\/b> variable as seen here: <\/p>\n<pre class=\"codeSample\">$excel = New-Object -comobject Excel.Application<\/pre>\n<p>After the Excel Application object has been created, we decide to make Excel invisible, so we set the visible property to <b>false<\/b>. This is different than what was done in the original VBScript. In my testing, I found it rather annoying for Excel to jump up and steal the focus of the screen when I was interested in seeing the folders being created. In reality, there is no reason to make Excel visible, so I just make it invisible. This is seen here (if you feel you are missing out by not having Excel open, by all means make it visible):<\/p>\n<pre class=\"codeSample\">$excel.visible = $false<\/pre>\n<p>If the Excel spreadsheet were made visible, you would see this spreadsheet:<\/p>\n<p><img decoding=\"async\" height=\"389\" alt=\"Image of the Excel spreadsheet made visible\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/january\/010109\/hsg_ff4_01.jpg\" width=\"500\" border=\"0\"><\/p>\n<p>&nbsp;<\/p>\n<p>Next we need to open the Excel workbook. To do this, we use the <b>Open<\/b> method from the <b>Workbooks<\/b> object. We store the returned <b>workbook<\/b> object in a variable named\u2014surprisingly enough\u2014<b>$workbook<\/b>. This is seen here:<\/p>\n<pre class=\"codeSample\">$workbook = $excel.Workbooks.Open($excelSheet)<\/pre>\n<p>Once we have a <b>workbook<\/b> object, we can use the <b>sheets<\/b> property from the <b>workbook<\/b> object to reference the first spreadsheet. Since we were lazy and did not delete or even rename the extra spreadsheets, we need to use a number to reference the first worksheet. We do this here: <\/p>\n<pre class=\"codeSample\">$worksheet = $workbook.sheets.item(1)<\/pre>\n<p>We now need to find out how many rows are occupied in the spreadsheet. To do this, we use the <b>usedRange<\/b> property of the <b>worksheet<\/b> object to return a <b>range<\/b> object. We then use the <b>rows<\/b> property to return a <b>rows<\/b> collection from which we get the count. This is also something that was not done in the original script. The VBScript used a <b>Do\u2026Until<\/b> loop and looked for a cell that had a value of <b>&#8220;&#8221;<\/b>. While that generally works, there is no reason to do that because we can use the <b>usedRange<\/b> property. This is seen here: <\/p>\n<pre class=\"codeSample\">$rowMax = ($worksheet.usedRange.rows).count<\/pre>\n<p>We create a variable named <b>$column<\/b> and set it equal to 1. This will be used to reference the first column. Even though I could just use 1 later on in the script, it makes the script easier to read if we use a variable such as <b>$column<\/b> to refer to the column. This is shown here:<\/p>\n<pre class=\"codeSample\">$column = 1<\/pre>\n<p>It is time to loop through the rows in the Excel spreadsheet. To do this, we are going to use a <b>For<\/b> statement. The <b>For<\/b> statement begins with three parts. The first is where we are starting from. Here we are starting at 1. The second part is how far we are going. Here we are going until the value of the <b>$row<\/b> variable is less than or equal to the value of <b>$rowMax<\/b>. The <b>$rowMax<\/b> variable is used to hold the maximum number of rows in the spreadsheet that are occupied. The last section is how we are going to get there. We are going to get there by incrementing the value of the <b>$row<\/b> variable one at a time. This section of code is seen here:<\/p>\n<pre class=\"codeSample\">For($row = 1 ; $row -le $rowMax ; $row ++)<\/pre>\n<p>Now we read the data file name that is stored in the cell. To do this, we use the <b>item<\/b> method of the <b>cells<\/b> collection from the Excel <b>application<\/b> object. We return the value of the <b>value2<\/b> property and store it in the <b>$filename<\/b> variable. Next we add the extension <b>.txt<\/b> to the end of each file name. This is because it was easier to create a series of file names in Excel without having the file extension at the end. This is seen here: <\/p>\n<pre class=\"codeSample\">{\n $fileName = $excel.cells.item($row,$column).value2\n $fileName += \".txt\"\n<\/pre>\n<p>Next we print out a message that we are copying the file, and then we use the <b>copy-item<\/b> cmdlet to copy the template file to the destination. This is seen&nbsp;here: <\/p>\n<pre class=\"codeSample\"> \"copying $fileName\"\n Copy-Item -path $templateFile -Destination \"C:\\fso\\fso1\\$fileName\"\n<\/pre>\n<p>Finally, we use the <b>quit<\/b> method to exit the Excel application. This last step is very important. If you do not call the <b>quit<\/b> method, you will end up with a new instance of the Excel process running on your computer each time you run the script. It would not take very many times before you consume all the resources on your computer and crash the thing! Also keep in mind that Microsoft Office products are not designed for <a href=\"http:\/\/support.microsoft.com\/kb\/257757\" target=\"_blank\">server side automation<\/a>. To call <b>quit<\/b>, you just type <b>quit<\/b> followed by empty parentheses as shown here:<\/p>\n<pre class=\"codeSample\">$excel.quit()<\/pre>\n<p>Well, PR, it is still cold and foggy outside. Vampire Weekend is gone, and now it is <a href=\"http:\/\/en.wikipedia.org\/wiki\/Fado\" target=\"_blank\">Fado<\/a>. I am listening to <a href=\"http:\/\/en.wikipedia.org\/wiki\/Mariza\" target=\"_blank\">Mariza<\/a>. I got addicted to Fado while I was in Lisbon for a month back in June. That is also where I got addicted to a cinnamon stick in my tea. See you tomorrow for Quick-Hits Friday. Paz!<\/p>\n<p><font class=\"Apple-style-span\" face=\"Verdana\" size=\"3\"><span class=\"Apple-style-span\"><b><b>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/b><\/b><\/span><\/font><\/p>\n<p><font class=\"Apple-style-span\" face=\"Verdana\" size=\"3\"><b><\/b><\/font><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I have a file that I would like to copy a number of times. This file is basically a template. I have an Excel spreadsheet that contains a list of names that I would like to use for the newly copied files. Can you help me? &#8211; PR Hi PR, Sorry, I\u2019m [&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,3,12,45],"class_list":["post-54703","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-files","tag-scripting-guy","tag-storage","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I have a file that I would like to copy a number of times. This file is basically a template. I have an Excel spreadsheet that contains a list of names that I would like to use for the newly copied files. Can you help me? &#8211; PR Hi PR, Sorry, I\u2019m [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/54703","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=54703"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/54703\/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=54703"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=54703"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=54703"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}