Reputation: 21
Very first powershell experience and want to rename files and directories that starts with 50 to 40 inside of my rank folder
Initially I tried this
foreach($Directory in Get-ChildItem 'D:\Issues\' -Recurse -Directory)
{
Rename-Item $Directory -NewName $($Directory.Name -replace "50","40")
}
But it also tries with the folders and even tries with the directories that doesn't have a 50 in it
second time i tried with
Get-ChildItem -Recurse | Rename-Item {$_.FullName -replace '50','40'}
but this error comes in
Rename-Item : A positional parameter cannot be found that accepts argument '$_.FullName -replace '70','80''. At line:1 char:26
What Am I actually missing??
I have tried with 2 more Commands and it actually works but brings errors in red
For Files
foreach($File in Get-ChildItem 'D:\Issues\' -Recurse -file)
{
Rename-Item $file -NewName $($_.Name -replace "50","40")
}
Errors
Rename-Item : Cannot bind argument to parameter 'NewName' because it is an empty string. At line:3 char:32
For Directories
foreach($Directory in Get-ChildItem 'D:\Issues\' -Recurse -Directory)
{
Rename-Item $Directory -NewName $($Directory.Name -replace "50","40")
}
Upvotes: 0
Views: 308
Reputation: 13227
You need to filter Get-ChildItem
to return only the items you want to rename. This can be done using the Include
param:
Get-ChildItem D:\Issues -Include 50* -Recurse | Rename-Item -NewName {$_.Name -replace '50','40'}
Upvotes: 1