• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1924
  • Last Modified:

Using Powershell to copy and rename files

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?
0
richlionel
Asked:
richlionel
  • 5
  • 4
1 Solution
 
Manpreet SIngh KhatraSolutions Architect, Project LeadCommented:
Can I Copy and Rename a file in a single step?
http://ask-leo.com/can_i_copy_and_rename_a_file_in_a_single_step.html

Renaming multiple files
http://msdn.microsoft.com/en-us/library/cc294618.aspx

How to Copy & Rename a File With Date Stamp in DOS
http://www.ehow.com/how_5993334_copy-file-date-stamp-dos.html

How to copy and rename files using the directory name using the command prompt (in Windows XP)?
http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_22699586.html

Check the above article should help you concerns :)

- Rancy
0
 
QlemoC++ DeveloperCommented:
ยป i. Pull the last 7 days of files generated by the app
I reckon you mean you want to move those files to Report server? Else it will get more difficult - we would have to keep a track of files already transferred for the next run of the script, since the numbers are random.

Anyway, yes, you can do all that with PowerShell. ZIP files can be accessed like folders (since XP), and file move/copy/rename based on creation or modification date is no big deal.
0
 
richlionelAuthor Commented:
Hello QLemo

Yes, I'd like to MOVE those files to the report server.  So to clarify, there are files named

File-23123, File-2343, File-346546 etc in that directory that I am interested in. There are also other files with names like Data-343, Data-345, Info-3432 etc that I am not interested in. The key here is that the files I am interested in begin with the word "File-".

I need to:

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

I would be running this from the Report server
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
QlemoC++ DeveloperCommented:
Assumptions:
(1) Each ZIP is named like the logfile.
(2) Each ZIP contains exactly one logfile.
(3) The order of logfiles shall be maintained in the target (that is, oldest log has the lowest number)
(4) There might be logfiles in the target already, and we need to start with a higher number then.
(5) The source is local and target remote, since the script runs from Report server.
0
 
richlionelAuthor Commented:
Correct, thanks Qlemo
0
 
QlemoC++ DeveloperCommented:
Taking the liberty to ignore my prior post ;-), here is the code:
$src = "C:\temp\ee\tst\src"
$dst = "C:\temp\ee\tst\dst"
$srcmask = "file-"
$dstmask = "file"

$app=new-object -com Shell.Application

gci "$src\$srcmask*.zip" | ? {!$_.PSIsContainer} |
  ? {$_.LastWriteTime -gt (Get-Date).AddDays(-7)} |
    % {	
      $app.namespace($src).MoveHere($app.namespace($_.Fullname).items())
      Remove-Item $_.FullName 
    }	

$num = (gci "$dst\$dstmask*")
if ($num) {
  $num = ($num | % { $_.Name.Replace($dstmask,'').Replace('.log','')} | measure-object -max).Maximum+1
} else {
  $num = 1
}
gci "$src\$srcmask*.log" | % {
  Move-Item $_.FullName "$dst\$dstmask$num.log"
  $num++
}

Open in new window

It does not guarantee the proper sequence of files (order by creation date), but that way it is much easier and more straight-forward.
0
 
richlionelAuthor Commented:
Thanks Qlemo, would it be too much to explain how it works to me? I'm keen to learn :)

Also, not sure where the unzipping takes place?
0
 
QlemoC++ DeveloperCommented:
The first 4 lines should be obvious: They define the folders used, and how the files are/will be named.

In general I've used the shortest forms of the Cmdlets, as long as they are obvious; like ? instead of where-object, % for foreach-object, gci for get-childitem (for the latter I could also have used dir).

Lines 6, 11 and 12 perform the unzipping part.
The Shell.Application object (created in line 6) allows for both file copy/move/rename aso. operations and (basic) operations on ZIP files, the same way you can do them in Explorer.
Line 11 does two things - gets the content of a ZIP file ( $app.namespace($_.Fullname).items() ), and moves it into the $src folder ( ($app.namespace($src).MoveHere(...) ). I could also have used CopyHere to extract without removing the files from the ZIP.
Line 12 then deletes the (now empty) ZIP file.

Lines 15-20 are calculating the highest used file number in the target folder, and add one to it.

Lines 21ff are then going thru the extracted files, and rename them using the $dstmask plus the incrementing number, so we get unique file names.
0
 
richlionelAuthor Commented:
Excellent, thanks!

The one last thing I am not sure about is "$num++" - what does that mean? I've googled and there are a couple of explanations, but I wanted to be sure.
0
 
QlemoC++ DeveloperCommented:
$num++  is a combined operator which increments the value of the var after delivering the info to the surrounding expression. ++$num would increment, then put that value into the pipeline. When there is no expression evaluation, the value is dismissed, and ++$num is the same as $num++ and the same as $num = $num  + 1.
Example:
$num = 1
write-host ($num++)
write-host (++$num)

Open in new window

will report 1 and 3.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now