Hi All
I am looking to grab a bunch of files and rename them.
In the middle of the file name there is a ‘time stamp’ string that i need to remove.by doing so I am creating duplicate file names and getting this error
“Rename-Item : Cannot create a file when that file already exists.”
I am looking for some logic that when this error occurs to randomly name the files something slightly different so both files can co-exist in the same directory.
heres an example of what i have so far
# File1 = c:\temp\11-28-2014-06-40-46-UPS--On-battery-power-in-response-to-an-input-power-problem.msg
# File2 = c:\temp\11-28-2014-06-40-52-UPS--No-longer-on-battery-power.msg
$Source = gci C:\temp | ? {$_.Extension -match '.msg'}
foreach ($file in $Source) {
$path = $file.FullName
$oldname = $file.name.Remove(10,9)
$newname = $oldname
Rename-Item -Path $path -NewName "$($newname)$($_.Extension)" -Force
}
Thank you
Hey fella,
You could try something like below. It does a check to see if the file exists already and if it does goes through a loop which gives it a _copyx where x incremements each time until a unique name is found.
# File1 = c:\temp\11-28-2014-06-40-46-UPS–On-battery-power-in-response-to-an-input-power-problem.msg
# File2 = c:\temp\11-28-2014-06-40-52-UPS–No-longer-on-battery-power.msg
Set-Location -Path C:\Temp
$Source = Get-ChildItem -Path C:\temp | Where-Object -FilterScript {
$_.Extension -match '.msg'
}
foreach ($file in $Source)
{
$path = $file.FullName
$oldname = $file.name.Remove(10,9)
$newname = $oldname
$newFileName = "$($newname)$($_.Extension)"
$fileAlreadyExists = Test-Path $newFileName
$index = 0
While ($fileAlreadyExists)
{
$index += 1
$newFileName = "$($newname)_copy$($index)$($_.Extension)"
$fileAlreadyExists = Test-Path $newFileName
}
Rename-Item -Path $path -NewName $newFilename -Force
}
HI
Thank you for the reply
the files that are copy1 the file name extension is actually becoming _copy and not .msg
its replacing the .msg with _copy1
“C:\temp\11-28-2014-UPS–On-battery-power-in-response-to-an-input-power-problem…msg_copy1”
oops, try this.
# File1 = c:\temp\11-28-2014-06-40-46-UPS–On-battery-power-in-response-to-an-input-power-problem.msg
# File2 = c:\temp\11-28-2014-06-40-52-UPS–No-longer-on-battery-power.msg
Set-Location -Path C:\Temp
$Source = Get-ChildItem -Path C:\temp | Where-Object -FilterScript {
$_.Extension -match '.msg'
}
foreach ($file in $Source)
{
$path = $file.FullName
$oldname = $file.name.Remove(10,9)
$extension = $file.extension
$newFilename = $oldname
$fileAlreadyExists = Test-Path $newFileName
$index = 0
$basename = $NewFileName.Substring(0,$newFilename.Length-4)
While ($fileAlreadyExists)
{
$index += 1
$newFileName = "$($basename)_copy$($index)$($Extension)"
$fileAlreadyExists = Test-Path $newFileName
}
Rename-Item -Path $path -NewName $newFileName -Force
}
Actually after running it against a big set of 2000k + files I am getting this error
Rename-Item : Cannot rename because item at
‘C:\Temp\Email\11-26-2014-10-45-01-[SUPPORT-#426760]–Rick’-Home-VPN.msg’ does not exist.
At line:26 char:3
but the file is there
Hmmm…It’s mentioning an ‘email’ folder, but the script only parses the top level Temp folder. Is it just with one file it’s doing or multiple? Also is it only with ones in a subfolder?
‘Mr Pringle’? That’s shocking! i feel old enough as it is, ‘Tim’ is much better for my emotional wellbeing. thnx. 
haha sorry about that Tim lol
even when i set the location to the ‘email’ folder which is the top level and no sub folders it still throws he error But it is renaming everything correctly so idk
it is working as intended but generating the error. If test with just 4 files no error, its when i run it against 2,000 files it happens
either way it is working so thank you for all your help