Using Powershell to copy and rename files RRS feed

  • Question

  • I have about ten servers, ServerA, ServerB > etc

    Within each server, there is a folder name C:\Program Files\AppName\Files

    At the end of every day, a file is generated by the App into that directory. Sometimes, two files can be generated, as the application creates a second one if the initial file is larger than 10MB. After the file is generated, it is zipped up.

    Each file is named File-XXXX.log where XXXX is a random set of numbers.

    I also have a central reporting server, named Report.

    From Report, I need to:

    i. Pull the last 7 days of files generated by the app
    ii. Unzip them
    iii. Rename them from File-XXXX into File1.log, File2.log, File3.log etc

    Does anyone know if this is possible using Powershell?
    Sunday, August 12, 2012 6:41 PM


All replies

  • I think this works:

    $servers = @("serverA", "serverB", "serverC");
    foreach ($s in $servers)
        # This assumes the script is running as an admin for all servers and that
        # all servers have file and printer shaing enabled for the administrative
        # shares to exist.
        Copy-Item -Path "\\$s\C$\Program Files\AppName\Files\*" -Include *.zip -Destination C:\SomeDestination;
    # Now all files have been harvested.  Unzip using something.  Google it up.
    # I found http://serverfault.com/questions/18872/how-to-zip-unzip-files-in-powershell.
    # Now rename each item.  It depends on how you unzipped.  If you unzipped all to a folder,
    # then I guess you could just rename like this:
    $counter = 0;
    Get-ChildItem -Path C:\TheFolderWithUnzippedFiles\* -Include *.log `
        | % { $counter = $counter + 1; Rename-Item -Path $_ -NewName "File$counter.log"; }

    I tested some equivalent code in my PC and it looked promising.  I did not explore further the functions to unzip a zip file in PowerShell, but I guess Google or Bing can do the heavy lifting there.

    Assuming you have unzipped everything to a folder, you can use the renaming I show there.  It should work.  I tested against a folder of mine using PDF files and the -WhatIf switch and the new filename looks OK.

    Jose R. MCP
    Code Samples

    Sunday, August 12, 2012 11:19 PM
  • Thanks Jose.

    I'll give this a go - one question though, I need to grab only the last 7 days files, do you know how I'd do that?

    Best regards

    Monday, August 13, 2012 7:53 AM
  • Hi,

    Get-Childitem C:\TheFolderWithUnzippedFiles\* -Recurse |  
    where-object {!($_.psiscontainer)} | 
    where { $_.LastWriteTime -gt $seven_days_backdate } |  
    foreach { Write-Host "$($_.LastWriteTime) :: $($_.Fullname) " } 

    More details:

    Use PowerShell to Find Files Modified by Month and Year



    Yan Li

    Yan Li

    TechNet Community Support

    • Marked as answer by Yan Li_ Thursday, August 16, 2012 2:01 AM
    Monday, August 13, 2012 8:04 AM
  • Oops!  I forgot the 7-day part.  Yan Li shows how to do it.  Get today's date and add -7 days.  That's your reference date.  Then run Get-ChildItem, either for each of the zipped files or for each of the unzipped files.  Yan Li shows this for each of the unzipped files.  Then pipe the contents to a Where-Object and do the check Yan Li shows there against LastWriteTime.  This will automatically filter out all files that are older.  So there, just pipe the filtered results to the next cmdlet.  Yan Li's example simply consumes them in a ForEach-Object to demonstrate the filter's effectiveness.

    Jose R. MCP
    Code Samples

    Monday, August 13, 2012 1:32 PM
  • Thanks guys - but I don't see how we're copying just files with the name File-XXXX.log, this requirement is quite important.
    Thursday, August 16, 2012 9:47 PM