Renaming files within a ton of subfolders - powershell

I have the following code but it is renaming folders and files. When i use basename it says the file i get an error that says the Source and destination path must be different.
I want to rename sd^fks.pdf to sd_fks.pdf
Get-ChildItem $dstpath -Recurse | `
Where-Object {$_.Name -match '^'} | `
Rename-Item -NewName { $_.Name -replace '^','_' }

Escape the circumflex character. Stick to the Name attribute. You might want to add -File to Get-ChildItem to only get files:
Get-ChildItem $dstpath -File -Recurse | Where-Object {$_.Name -match '\^'} | Rename-Item -NewName {$_.Name -replace '\^','_'}

You don't need the Where-Object clause for this. Instead use Filter which makes the code run faster aswell:
Get-ChildItem -Path $dstpath -Filter '*^*' -Recurse -File | Rename-Item -NewName { $_.Name -replace '\^','_' }

When in doubt, regex escape:
[regex]::escape('^')
\^
'^' means 'the beginning of the line in regex:
'hi' -replace '^','prefix-'
prefix-hi
Or plug the expression in at https://regex101.com and see what it says.

Related

Powershell to remove all file extensions in sharepoint online library through mapped network drive

Hello and thanks in advance for the help. I am attempting to remove an extra extension that was added to documents in a library due to a security breach. When i manually remove the extension ".easy2lock" the file becomes available. However, when i try to do this programmatically via powershell it works, but not for folders with long paths. Does anyone know how to get around this in powershell?
I've tried tried updating my registry to allow long paths, however, i don't believe that has any affect on
here is the script and error i recieve:
[string]$DeleteBadExtension = ".easy2lock_read_me"
[string]$BadExtension = ".easy2lock"
Get-ChildItem -Filter "*$BadExtension" -Recurse | Rename-Item -NewName {$_.Name -replace $BadExtension, [string]::Empty}
Get-ChildItem *$DeleteBadExtension -Recurse | foreach { Remove-Item -Path $_.FullName }
I'll have to take your word for it that the files are usable after changing the name, that hasn't been my experience. To be able to access long paths in powershell you'll need to use the -LiteralPath parameter. Unfortunately the -Path parameter will be the default used when piping. -LiteralPath does take pipeline input by propertyname so you can either manipulate the object removing path and adding literal path like this
Get-ChildItem -Filter "*$BadExtension" -Recurse |
Select-Object Name,Basename,Extension,#{n='LiteralPath';e={"\\?\$($_.fullname)"}} |
Rename-Item -NewName {$_.Name -replace $BadExtension}
Or you can use a Foreach-Object or foreach loop and use the required syntax \\?\ for the path.
Get-ChildItem -Filter "*$BadExtension" -Recurse | Foreach-Object {
Rename-Item -LiteralPath "\\?\$($_.FullName)" -NewName ($_.Name -replace ‘$BadExtension’)
}
Edit
Since the basename is the desired filename (just without the unwanted extension) you can use the basename as the newname. All four examples have been tested and confirmed to work. If it's not working for you then there is something else going on.
Get-ChildItem -Filter *$BadExtension -Recurse | ForEach-Object {
Rename-Item -LiteralPath "\\?\$($_.FullName)" -NewName ($_.name -replace $BadExtension)
}
Get-ChildItem -Filter *$BadExtension -Recurse | ForEach-Object {
Rename-Item -LiteralPath "\\?\$($_.FullName)" -NewName $_.basename
}
Get-ChildItem -Filter "*$BadExtension" -Recurse |
Select-Object Name,#{n='LiteralPath';e={"\\?\$($_.fullname)"}} |
Rename-Item -NewName {$_.Name -replace $BadExtension}
Get-ChildItem -Filter "*$BadExtension" -Recurse |
Select-Object Basename,#{n='LiteralPath';e={"\\?\$($_.fullname)"}} |
Rename-Item -NewName {$_.baseName}

I want to remove recursively square brackets from file names

I have a lot of files in a directory containing square brackets for example:
Filename 1 [12454365].txt
I tried the following script but it's giving me the an error.
get-childitem -recurse | foreach { move-item -literalpath $_.name ($_.name -replace '\[.*\]', '')}
Error message
move-item : A device attached to the system is not functioning.
Only want to remove square brackets not everything in between!
If you specify -Recurse, you will need to specify the file with FullName because it will be targeted other than the current directory.
(Get-ChildItem -File -Recurse) | foreach {
$dest = Join-Path $_.DirectoryName ($_.Name -replace "[\[\]]")
Move-Item -LiteralPath $_.FullName $dest
}
Also, it is better to use Rename-Item for file renaming.
(Get-ChildItem -File -Recurse) | Rename-Item -NewName { $_.Name -replace "[\[\]]" }
I believe the issue is that you are replacing the brackets and everything in between.
Get-ChildItem * -Filter "*`[*`]*" | Rename-Item -NewName { $_.name -replace '\[','' -replace '\]','' }

How to write a regular expression in PowerShell - find specific string in filename and remove everything what comes after it

I want to write a powershell script which should be able to remove searched string and everything after the searched string from a filename.
My code looks like this:
$path = "U:\PowerShell_Scripts\Test files"
$text1 = "_FULL.001_020_"
$text2 = "_HIGH.001_020_"
Get-ChildItem -Path $path -Filter "*$text1*" -Recurse |
Rename-Item -NewName {$_.Name -replace $text1, ''}
Get-ChildItem -Path $path -Filter "*$text2*" -Recurse |
Rename-Item -NewName {$_.Name -replace $text2, ''}
but it is not doing what I need of course. I have been looking the whole day via google, but this particular method is not possible to find, or I am using probably wrong search criteria.
Filenames which names should be manipulated:
SVT_ALL_HU__MGU_01__FULL.001_020_040.xml
SVT_ALL_HU__MGU_01__FULL.001_020_080.xml
SVT_ALL_HU__MGU_01__HIGH.001_020_249.xml
This is not a duplicate of "How to remove part of a string in powershell using Regular Expressions", because there is no multiple filename manipulation mentioned with starting fixed string.
This should do what you're requesting:
$path = "U:\PowerShell_Scripts\Test files"
$text1 = "_FULL.001_020_"
$text2 = "_HIGH.001_020_"
Get-ChildItem -Path $path -Filter "*$text1*" -Recurse |
Rename-Item -NewName ($_.Name -replace '__F.*\.','.')
Get-ChildItem -Path $path -Filter "*$text2*" -Recurse |
Rename-Item -NewName ($_.Name -replace '__H.*\.','.')
It will take SVT_ALL_HU__MGU_01__FULL.001_020_080.xml and make it SVT_ALL_HU__MGU_01.xml

Powershell bulk rename file name including ()

Files are named like 20160825().xlsx
Have tried both of this without any changes to the file names
Get-ChildItem "C:\Users\..." -Filter “*()*” -Recurse | Rename-Item NewName {$_.name -replace ‘()’,’’ }
Get-ChildItem "C:\Users\..." -Filter “*()*” -Recurse | Rename-Item -LiteralPath -NewName {$_.name -replace ‘()’,’’ }
-replace uses regular expressions (regex), which is a different language with different rules, and parentheses are special characters for grouping, and they need to be escaped to match literally. Try:
$_.name -replace '\(\)'
You may let PowerShell/.net do the escaping:
$RE = [RegEx]::escape("()")
Get-ChildItem "C:\Users\..." -Filter "*()*" -Recurse |
Rename-Item NewName {$_.Name -Replace $RE}
BTW I'd avoid using an editor which replaces quotes with typographic ones.

rename files in powershell

I would like to ask for help in renaming files in given folder.
I would like to change characters "vol._" to "vol."
thanks for help
gci c:\folder_path | ? {$_.name -match 'vol._'} |
rename-item -newname {$_.name -replace 'vol._','vol.'} -whatif
Take a look at the output and if everything works fine remove whatif switch
edit. If you need to rename files even in subfolders you have to apply a little change
gci c:\folder_path -rec | ? {!$_.psiscontainer -and $_.name -match 'vol._'} |
rename-item -newname {$_.name -replace 'vol._','vol.'} -whatif
What about:
gci c:\folderpath -include vol._* | rename-item -newname {$_.name -replace 'vol._', 'vol.'}