Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Copy/Move file and rename if destination exist

Reply
 
Old 01-29-2009   #1 (permalink)
Personne


 
 

Copy/Move file and rename if destination exist

Hi,

I'm trying to get better with Powershell, and I just want your
comments on this little function I wrote, to automatically rename a
files (with increment) if the destination file already exists when I
try to copy or move a file.

I'm just wondering, if there is a faster/cleaner way to write this
function

Example: I want to copy a file from "c:\source\test.txt" to "C:
\test", so I'm checking if "c:\dest\test.txt" doesn't exist, and it it
does the source will will be renamed to test[1].txt

function rename_exist_file([string]$source, [string]$dest) {
cls
$fullname=[io.path]::GetFileName($source)
$name=[io.path]::GetFileNameWithoutExtension($source)
$ext=[io.path]::GetExtension($source)
$dir=[io.path]::GetDirectoryName($source)
$newfile="$dest\$fullname"

$i=1
while ((test-path -literalpath $newfile)) {
#write-host -back blue -for white $newfile already exists
$newfile = $dest + '\'+ $name +'['+ ($i++) +']' + $ext
}

$newfile
}

$source= "c:\source\test.txt"
$dest="C:\dest"

Copy-Item -LiteralPath $source -destination (rename_exist_file $source
$dest)


My System SpecsSystem Spec
Old 01-29-2009   #2 (permalink)
S.Lesire


 
 

RE: Copy/Move file and rename if destination exist

Hi Personne,

I will purpose a solution more easy, by using CMDLET directly....


function CopyFileToFolder ([string]$Source,[string]$destination){

$filename = $source.substring($source.lastindexofany("\") +1 ,$source.length
- ($source.lastindexofany("\")+1))
if (Test-Path $destination$file) {
$ext = Get-Date -format 'yyMMddhhmmss'
Rename-Item $destination$filename $ext"."$filename
}
Copy-Item $source $destination
}

$sourcefilepath = "P:\SNCF powershell\temp\test.txt"
$destinationpath = "P:\SNCF powershell\temp\test\"
copyFiletoFolder $sourcefilepath $destinationpath

I wish it will help you

Sylvain Lesire

"Personne" wrote:
Quote:

> Hi,
>
> I'm trying to get better with Powershell, and I just want your
> comments on this little function I wrote, to automatically rename a
> files (with increment) if the destination file already exists when I
> try to copy or move a file.
>
> I'm just wondering, if there is a faster/cleaner way to write this
> function
>
> Example: I want to copy a file from "c:\source\test.txt" to "C:
> \test", so I'm checking if "c:\dest\test.txt" doesn't exist, and it it
> does the source will will be renamed to test[1].txt
>
> function rename_exist_file([string]$source, [string]$dest) {
> cls
> $fullname=[io.path]::GetFileName($source)
> $name=[io.path]::GetFileNameWithoutExtension($source)
> $ext=[io.path]::GetExtension($source)
> $dir=[io.path]::GetDirectoryName($source)
> $newfile="$dest\$fullname"
>
> $i=1
> while ((test-path -literalpath $newfile)) {
> #write-host -back blue -for white $newfile already exists
> $newfile = $dest + '\'+ $name +'['+ ($i++) +']' + $ext
> }
>
> $newfile
> }
>
> $source= "c:\source\test.txt"
> $dest="C:\dest"
>
> Copy-Item -LiteralPath $source -destination (rename_exist_file $source
> $dest)
>
>
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Cannot Rename or Move a File or Folder in Vista Fix Tutorials
In Vista Home Premium - Can not copy, move, rename or delete a fil Vista file management
Cut & Paste, Move and Rename folder or file in the network Vista networking & sharing
Can't open, rename, delete, or move a file.... Vista General
Can't Delete/rename/move a file.. Vista security


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46