Forum Discussion
Remove characters from a file name
- Nov 08, 2024
Hi manny213 ,
A simple one-liner can handle this.
Get-ChildItem -Path "D:\Data\Temp\Forum" | ForEach-Object { Rename-Item -Path $_.FullName -NewName "$($_.Name.Replace("'", ''))"; }
Example
Cheers,
Lain
Hi manny213 ,
A simple one-liner can handle this.
Get-ChildItem -Path "D:\Data\Temp\Forum" | ForEach-Object { Rename-Item -Path $_.FullName -NewName "$($_.Name.Replace("'", ''))"; }
Example
Cheers,
Lain
- manny213Nov 09, 2024Brass Contributor
Hi LainRobertson. Thanks for the prompt reply. I am getting an error. Do you know how to fix this?
Rename-Item : Cannot create a file when that file already exists. At C:\Users\Me\Documents\Trading\SSIS\Powershell\RemoveApostropheInFilenamesScript.ps1:1 char:104 + ... ch-Object { Rename-Item -Path $_.FullName -NewName "$($_.Name.Replace ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : WriteError: (C:\Users\Me...Ord Shs (D).csv:String) [Rename-Item], IOException + FullyQualifiedErrorId : RenameItemIOError,Microsoft.PowerShell.Commands.RenameItemCommand
- LainRobertsonNov 09, 2024Silver Contributor
Hi manny213 ,
The error's fairly straightforward.
During a Rename-Item action, a file with the proposed "new name" already exists, meaning the existing file that Rename-Item is working on cannot be renamed.
Here's an example where the file named "forum'.ps1" cannot be renamed as another file named "forum.ps1" already exists.
Cheers,
Lain- manny213Nov 09, 2024Brass Contributor
I deleted all the source files and started over. Looks like the script ran b/c I can see the apostrophes gone. Now, I am getting a new error:
Rename-Item : The process cannot access the file because it is being used by another process. At C:\Users\Me\Documents\Trading\SSIS\Powershell\RemoveApostropheInFilenamesScript.ps1:1 char:104 + ... ch-Object { Rename-Item -Path $_.FullName -NewName "$($_.Name.Replace ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : WriteError: (C:\Users\Me...eries A (D).csv:String) [Rename-Item], IOException + FullyQualifiedErrorId : RenameItemIOError,Microsoft.PowerShell.Commands.RenameItemCommand
Do you know how to fix this one?
Thank you