How to rename multiple files in powershell from parent folder? - powershell

Hello I would like to know is there a way to replace multiple filenames from parent folder?
I was able to rename multiple files using command below, but I have to access each folder first to rename multiple files.
dir .\* -include ('*.mp4','*.srt') | Rename-Item -NewName { $_.Name -replace '\[','' -replace '\]','' }
I was trying to replace dir .\* to dir .\**\* to select from parent folder but didn't work.
What am I missing?

You can do that by adding the -Recurse switch to Get-ChildItem (dir is alias to Get-ChildItem).
When searching for files that contain square brackets, you need to use the -LiteralPath parameter.
Also, be aware that there is a snag if you pipe the results from that directly to Rename-Item..
When doing so, Get-ChildItem may pick up the already processed files again, wasting time of course, so to prevent that you can either do:
$rootFolder = 'X:\WhereTheFilesAre'
(Get-ChildItem -LiteralPath $rootFolder -Include '*.mp4','*.srt' -File -Recurse) |
Rename-Item -NewName { $_.Name -replace '\[|]' }
or
$rootFolder = 'X:\WhereTheFilesAre'
$files = Get-ChildItem -LiteralPath $rootFolder -Include '*.mp4','*.srt' -File -Recurse
foreach ($file in $files) {
$file | Rename-Item -NewName { $file.Name -replace '\[|]' }
}

In additional to marsze's concise answer and realizing this isn't really a performance question:
-Include is much slower than -Filter because it filters after retrieving from the file system. Whereas, -Filter has the file system do the heavy lifting. In essence -Filter is a nuanced version of moving filtering operations left in the command/pipeline to improve performance. However, -Filter doesn't take multiple values! That said, you may still be able to exploit this characteristic using a loop, something like:
'*.mp4','*.srt' |
ForEach-Object{
Get-ChildItem .\ -Filter $_ -File
} |
Rename-Item -NewName { $_.Name -replace '\[|\]' }
-File works even though you are using -Recurse and may carry a modest performance improvement. I also shortened the -replace to 1 operation which can only help further.

What you probably want is the -Recurse switch:
dir "the folder" -Recurse -Include ('*.mp4','*.srt')
Note that this will recurse all levels of subdirectories.

I just found out I could use dir . to select all files (with specific file types), including files in subdirectories.
dir . -Recurse -Include ('*.srt', '*.mp4') | Rename-Item -NewName { $_.Name -replace '\[|\]','' }

Related

Windows 10, Rename all *.jpg files in all subdirectories, with number starting from 1

I want a solution for the same problem, but in Windows 10.
Recursively rename .jpg files in all subdirectories
I tried with following powershell command,
Get-ChildItem -Recurse -Include *.jpg | % { Rename-Item $_ -NewName ('{0:D1}.jpg' -f $i++)}
but it renames the files in sequential order without resetting the index to 1 in every sub folder.
I think you need two separate Get-ChildItem cmdlets for this. The first will gather all subdirectories and when looping though that, the second will gather the files in each directory:
Get-ChildItem -Path 'X:\RootFolder\where\the\files\are' -Recurse -Directory | ForEach-Object {
$count = 1 # reset the counter for this subdir to 1
Get-ChildItem -Path $_.FullName -Filter '*.jpg' -File | ForEach-Object {
$_ | Rename-Item -NewName ('{0:D1}.jpg' -f $count++) -WhatIf
}
}
Remove the -WhatIf if you are satisfied with the results shown in the console.
P.S. the title says *.png, but your code deals with *.jpg. Doesn't matter, as long as you set your filter to the correct extension and adjust the new name in the code accordingly
As of my knowledge you have to do use it as a nested foreach:
Foreach ($directory in (Get-ChildItem -Directory)){
$i = 1
Get-ChildItem $directory.Fullname -Recurse -Include *.jpg | % { Rename-Item $_ -NewName ('{0:D1}.jpg' -f $i++)}
}
I tested it and it worked :)
If it worked for you, please mark it as the accepted answer.

Rename all files with no extension in a given directory and sub-folders using PowerShell

I need to write a script in PowerShell which renames all the files with no extension under a given directory and all subfolders. By renaming I mean to add an extension e.g. ".html" to the file which doesn't have it.
So far I've tried to build something like this:
Get-ChildItem -Path 'C:\dev\blah' -Filter *. -Recurse | ForEach-Object {
Rename-Item -Path $_.FullName -NewName ($_.FullName + ".html")
}
You need to exclude folders from the enumerated items. Use the switch -File if you're running PowerShell v3 or newer, otherwise add a Where-Object filter. Also, you don't need ForEach-Object since Rename-Item accepts pipeline input.
Get-ChildItem -Path 'C:\dev\blah' -Filter *. -Recurse | Where-Object {
-not $_.PSIsContainer
} | Rename-Item -NewName {$_.Name + ".html"}

Rename all sub elements with incremental number

I'm new in powershell, I have to execute the following statement recursively:
Get-ChildItem | ForEach-Object {Rename-Item $_ -NewName ("new_filename{0}.smali" -f $nr++)}
In other words I have to rename all the .smali files in the subdirectories with any other name different from the current one (keeping the .smali extension).
Get-ChildItem -Path FolderPath -Filter *.smali -Recurse | ForEach-Object{$_ | Rename-Item -NewName ($.BaseName + StringToAdd + $.Extension) -WhatIf}
Copy the above line as is... Replace the values for FolderPath and StringToAdd and give it a try. If everything looks fine then execute it by removing the -Whatif switch.
dir is an alias for Get-ChildItem which has a -Recursive parameter.
$nr = 0; Get-ChildItem -Recurse -Filter "*.smali" | Rename-Item -NewName "new_filename$($nr++).smali"
Things to note:
DIR is an alias for Get-ChildItem. Get-ChildItem is more Powershell-y
Your command only looked at the top level directory, so I added the -recurse
Your command would rename all files, not just the "*.smali", so I added the filter.

Rename-item in multiple subfolders

I have a piece of software which looks for a files named "report.txt". However, the text files aren't all named report.txt and I have hundreds of sub folders to go through.
Scenario:
J:\Logs
26-09-16\log.txt
27-09-16\report270916.txt
28-09-16\report902916.txt
I want to search through all the sub folders for the files *.txt in J:\logs and rename them to report.txt.
I tried this but it complained about the path:
Get-ChildItem * |
Where-Object { !$_.PSIsContainer } |
Rename-Item -NewName { $_.name -replace '_$.txt ','report.txt' }
Get-ChildItem * will get your current path, so in instead let's use the define the path you want Get-ChildItem -Path "J:\Logs" and add recurse because we want the files in all the subfolders.
Then let's add use the include and file parameter of Get-ChildItem rather than Where-Object
Then if we pipe that to ForEach, we can use the Rename-Item on each object, where the object to rename will be $_ and the NewName will be report.txt.
Get-ChildItem -Path "J:\Logs" -include "*.txt" -file -recurse | ForEach {Rename-Item -Path $_ -NewName "report.txt"}
We can trim this down a bit in one-liner fashion with a couple aliases and rely on position rather than listing each parameter
gci "J:\Logs" -include "*.txt" -file -recurse | % {ren $_ "report.txt"}
To replace certin word in all files in all sub folders
Get-ChildItem C:\Path -Recurse -Filter *OldWord* | Rename-Item -NewName { $_.name -replace 'OldWord', 'NewWord'} -verbose

error from Powershell when try to rename with sequential prefix *recursively*

I have no problem adding sequential prefixes to filenames. The following works great on the top directory in question.
$path="E:\path\newtest1"
$count=4000
Get-ChildItem $path -recurse | Where-Object {!$_.PSIsContainer -and $_.Name -NotMatch '^\d{4}\s+'} | ForEach -Process {Rename-Item $_ -NewName ("$count " + $_.name -f $count++) -whatif}
BUT if there are files in subfolders within the top directory, these are all completely missed. Whatif reports that for any deeper file it "does not exist".
I have tried the following, based on looking at some pages on other recursion problems, but as you can probably guess I have no clue what it is doing. Whatif shows that it does at least pickup and rename all the files. But the following does it too much and makes multiple copies of each file with each number:
$path="E:\path\newtest1"
$count=4000
Get-ChildItem -recurse | ForEach-Object { Get-ChildItem $path | Rename-item -NewName ("$count " + $_.Basename -f $count++) -whatif}
Really keen to get some guidance on how to get the first of these two snippets to work to find all files in all subdirectories and rename them with sequential number prepended.
Try it like so:
Get-ChildItem $path -recurse -file | Where Name -NotMatch '^\d{4}\s+' |
Rename-Item -NewName {"{0} $($_.name)" -f $count++} -whatif
When you supply $_ as an argument (not a pipeline object), that gets assigned to the Path parameter which is of type string. PowerShell tries to convert that FileInfo object to a string but unfortunately the "ToString()" representation of files in nested folders is just the filename and not the full path. You can see this by executing:
Get-ChildItem $path -recurse -file | Where Name -NotMatch '^\d{4}\s+' | ForEach {"$_"}
The solution is either to A) pipe the object into Rename-Item or B) use the FullName property e.g. Rename-Item -LiteralPath $_.FullName ....