Reputation: 1
I want to replace bad characters in filenames within all sub-directories. However due to limitation of rename-item with '[' (wild cards) I have to use the -LiteralPath in the command. This means I'm having issues with running this with sub-directories.
The code below works on current directory, but I cannot work out how to adapt this code to rename files within all sub-directories. Please help?
ls *.* -recurse | % { Move-Item -literalpath $_.fullname `
($_.name -replace "[()\[\]]|\.(?!\w{3}$)", " ") }
Upvotes: 0
Views: 130
Reputation:
Your RegEx will remove the dot's from extensions with not exactly 3 chars.
.h
.cs
.html
I suggest replacing in BaseName and append the extension and only if brackets/parentheses/dots chars are present.
Get-ChildItem *.* -Recurse -File|
Where-Object {$_.BaseName -match '[()\[\]\.]'}|
Rename-Item -NewName {($_.BaseName -replace '[()\[\]\.]',' ')+$_.Extension} -Whatif
Remove the -WhatIf
if the output looks OK.
Upvotes: 1