Reputation: 705
In windows powershell ISE i'm trying to replace a string in a lot of files name and folders name example i have files and folders like
foo_file1.txt
file2_foo.php
file3_foo_file.js
foo/folder1/file.ext
folder2/file4_foo.doc
foo_folder/file5.ppt
folder/foo/file_foo.jpg
I want to change all "foo" to "bar" for example. I used windows powershell command
$dir = "the Path of folder Parent"
CD $dir
Get-Childitem -recurse |
Where-Object {$_.Name -match "foo"}
rename-item -NewName { $_.Name -replace "foo", "bar" }
but this command is not working
Upvotes: 1
Views: 196
Reputation: 9183
You can do like this also:
$dir = "C:\folderpath"
(Get-Childitem $dir -recurse |Where-Object {$_.Name -match "foo"}) |
rename-item -NewName { $_.BaseName -replace "foo", "bar" }
Upvotes: 1
Reputation: 11304
Get-Childitem -recurse | Where-Object {$_.Name -match "foo"} | % { Rename-Item -NewName ( $_.Name -replace "foo", "bar" ) -Path $_.FullName }
Seems that you forgot the -Path
parameter of Rename-ITem
.
Hope that helps.
Upvotes: 1