![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
![]() |
| 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 | |||