I'm looking for a solution to rename all files with given name in the directory and subfolders.
Eg There is a directory called c:\\blah
and in this folder, there is one file called BaR.txt
and one folder called foo. In the foo
folder, there is another file called bAr.txt
. I want to find all files with name bar.txt
and change the name of each file to “neo.txt”. It must also be possible to rename the files to lower case as well eg bar.txt
. So far, I've tried to do this manually using the Windows Explorer in Windows10 but for some weird reason when I try to rename the bulk files there is extra “(1)” sign added to the end of each file name. I've tried to play with PowerShell and I created a command
$_.name -replace 'bar.txt','neo.txt'
But it didn't work for me.
To do this, you need 2 cmdlets: Get-ChildItem
and Rename-Item
.
This should work:
Get-ChildItem -Path 'C:\Blah' -Filter 'bar.txt' -Recurse | ForEach-Object {
Rename-Item -Path $_.FullName -NewName 'neo.txt'
}
However, if inside the foo
folder there are more subfolders, this code will rename all files called bar.txt
(case-insensitive) it finds in all of these subfolders.
If you do not want to go any deeper than the first subfolder in C:\\Blah
(the one called foo
), you can use the -Depth
parameter on the command like this:
Get-ChildItem -Path 'C:\Blah' -Filter 'bar.txt' -Recurse -Depth 1 | ForEach-Object {
Rename-Item -Path $_.FullName -NewName 'neo.txt'
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.