Copy File directly or rename dest file if exist and copy file


posted by Sylvain LESIRE
01-29-2009

Downloads: 870
File size: 561 B
Views: 11,188

Embed
Copy File directly or rename dest file if exist and copy file
  1. function CopyFileToFolder ([string]$Source,[string]$destination){ 
  2. # get filename 
  3. $filename = $source.substring($source.lastindexofany("\") +1 ,$source.length - ($source.lastindexofany("\")+1)) 
  4. # verify if file exists 
  5. if (Test-Path $destination$file) { 
  6.     $ext = Get-Date -format 'yyMMddhhmmss' 
  7.     Rename-Item $destination$filename $ext"."$filename 
  8. Copy-Item $source $destination 
  9.  
  10. $sourcefilepath = "P:\powershell Script\Orginal\test.txt" 
  11. $destinationpath = "P:\powershell Script\destinationDir\" 
  12. copyFiletoFolder $sourcefilepath $destinationpath 

This function permits to copy file to an other directory.

If a file exists with same filename, it will rename the orginal file by using date and copy file to destination.

 

Comments

Sylvain LESIRE wrote re: Copy File directly or rename dest file if exist and copy file
on 01-29-2009 5:17 PM

Error line5, please replace by

if (Test-Path $destination$filename) {  

Colin Smith wrote re: Copy File directly or rename dest file if exist and copy file
on 02-18-2009 4:07 PM

Nice. I just wrote a similar script. I had it rename the existing file at the destination with a datestamp and also since I was copying to multiple remote servers I implemented a ping test. File copy only happens if server is available.

Copyright 2012 PowerShell.com. All rights reserved.