Powershell replace characters in files within sub-directories using -LiteralPath - powershell

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}$)", " ") }

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.

Related

Powershell - Multiple file ext on rename doesnt work

I have a large amount of files in a large amount of folders that I want to set to all have the same file extension. Using this works for single file ext
Get-ChildItem -File -Recurse | ForEach-Object { Rename-Item -Path $_.PSPath -NewName $_.Name.replace(".webp",".jpg")}
But using this for multiple files ext does nothing
Get-ChildItem -File -Recurse | ForEach-Object { Rename-Item -Path $_.PSPath -NewName $_.Name.replace(".jpeg.png.webp",".jpg")}
I have tried .* for the file ext to cover all of them and adding spaces between the exts but this does nothing either.
Can someone point out where I am being an idiot?
To avoid renaming parts of the file name that are not the extension, I would advise against using -replace, unless you treat it as it should by escaping the characters that have special meaning for regex (the dot) and by anchoring the string to replace.
Better use a dedicated .Net function:
(Get-ChildItem -Filter '*.jpeg','*.png','*.webp' -File -Recurse) | Rename-Item -NewName {[System.IO.Path]::ChangeExtension($_.Name, ".jpg")}
or if you do want to use -replace:
(Get-ChildItem -Filter '*.jpeg','*.png','*.webp' -File -Recurse) | Rename-Item -NewName { $_.Name -replace '\.(jpeg|png|webp)$', '.jpg' }
Also, by specifying the extensions in the -Filter parameter of Get-ChildItem, you do not iterate all files, eventhough they can have extensions you do not want to rename, plus Get-ChidItem will do its job faster.
You can use | for OR operations.
Get-ChildItem -File -Recurse | Rename-Item -NewName { $_.Name -replace "\.jpeg$|\.png$|\.webp$",".jpg" }
the \. is needed to match a literal ., and the $ is needed to tell the command to only match those three strings when they occur at the end of the filename.

Renaming all the folder names in the current dir that has files and sub directories inside using Powershell

I have a set of folders like below, which has files and subfolders inside them, i want to rename
all the smv2103* folders to smv2106* folders , keeping the files and subfolders inside them intact using Powershell
E:\online\smv2103pdf
E:\online\smv2103mac
E:\online\smv2103frp
E:\online\smv2103rep
E:\online\smv2103soc
E:\online\smv2103bid
E:\online\smv2103rem
E:\online\smv2103nop
E:\online\smv2103gac
E:\online\smv2103pam
Any help would be highly appreciated , as Rename-Item itself is not working because of the files and subfolders inside
Get-ChildItem "E:\online\*" -Directory|
Where{ ( $_.Name -like "*2103*") } |
ForEach-Object{
((Get-Content $_.FullName -Raw) -replace "2103","2106") | Set-Content -Path $_.FullName
}
So you just want to rename the folders? You use Get-Content to get the content from within files. To filter for a certain criteria, Get-ChildItem has a -Filter Parameter you can use.
Get-ChildItem -Filter "*2013*"
When filtering in powershell, its good practice filtering as far left as possible. Now, knowing your just looking to rename the folders themselves, we can remove Get-Content and pipe the info onto Rename-Item.
Get-ChildItem -Path "E:\online*" -Filter "*2013*" -Directory |
ForEach-Object {
Rename-Item -Path $_.FullName -NewName $_.Name.Replace('2013','2016')
}

Powershell Changing Bulk File Extensions All At Once

Long story short: need to change multiple file extensions that are . (in Windows, the file extension is just a .) to .csv. I am able to do this in command prompt with this:
ren *. *.csv
But when I try to rename in Powershell, it only changes the first file (result: .csv.csv.csv.csv.csv.csv.csv) while the rest of the files remain untouched. I used this command:
Dir | Rename-Item -NewName { $_.name -replace ".",".csv" }
Can someone explain what I'm doing wrong and how come the first file is being renamed as such? Don't understand why Powershell makes it more complicated since it is based off CMD.
-replace is using regular expression matching where . will match any character so every character is replaced with .csv. Try this to replace *.txt and *.file with *.csv
gci -File | Rename-Item -NewName { $_.name -replace "\.(txt|file)$", ".csv" }
The question has changed. If your files really end in . which I can't actually reproduce then -replace "\.$", ".csv" where \. matches a literal dot and $ matches the end of the line.
If your files have no extension at all then you could do -replace "$", ".csv"
Or you could filter for files with no extension and just add one
gci -File |? Extension -eq '' |% { Rename-Item $_.FullName -NewName "$($_).csv" }
Get-ChildItem -Path C:\ -File | ForEach {Rename-Item $_.FullName -NewName ($_.name).Replace(".txt",".csv")}
Using dot notation, it reads characters as such; meaning no need in escaping any special chars.

How to use powershell to remove spaces from the beginning of a filename

I've been using the below to replace spaces in all the filenames and folders under the current directory location with underscores.
dir -Force -Recurse | Rename-Item -NewName {$_.name -replace " ","_"}
How do I specify to only replace spaces if they are at the the beginning of the filename, eg the first character?
Get-ChildItem -Force -Recurse | Rename-Item -NewName ($_.Name -replace "^ ","_")
Your post title doesn't quite line up with what you're asking.
There's also a syntax error in your example code (you're missing the period when trying to access the Name property of the PSItem $_).
None the less I hope this is what you're looking for. You were mostly there. The first parameter of -replace interprets regex.

Renaming files in multiple subfolders

I have 100 folders that are incremental. E.g.
'20D, 0.5B001'...'20D, 0.5B002'
...all the way to
'20D, 0.5B100'
Each of those folders contains files that have the same incremental names. E.g. 'Test_C1S0002001'...'Test_C1S0002002' etc.
I want to rename every file in each of these folders to '002001' I.e. just get rid of 'Test_C1S0' in every one of these subfolders. How can I do this?
gci 'c:\path\' -File -Recurse | ren -NewName { $_ -replace 'Test_C1S0', '' }
(untested)
What TessellatingHeckler has should work perfectly fine. You don't need regex for this as you are removing a simple string from the beginning of the line. So using the same logic...
Get-ChildItem "C:\temp" -Recurse -File | Rename-Item {($_.Name).TrimStart("Test_C1S0")}
If you don't have PowerShell at least v3.0 then you would need to do this.
Get-ChildItem "C:\temp" -Recurse | Where-Object{!$_.PSIsContainer} | Rename-Item {($_.Name).TrimStart("Test_C1S0")}