rename files in powershell - 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.'}

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}

Renaming files within a ton of subfolders

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.

Powershell rename multiple files

Example "rename multiple files" gives us this:
Get-ChildItem *.txt | Rename-Item -NewName { $_.Name -replace '.txt','.log' }
But I need something like this:
Get-ChildItem *.txt | Rename-Item -NewName { $_.Name -replace '.txt','someArray[$i]' }
How can I do that ?
Simple. Take the quotes off and add a $:
EDIT: Ok, here's my guess. I don't know what $textfile is, or what result you want exactly. [^.jpg] just means any character except those 4.
get-childitem *.jpg |
Foreach {$i=0} {Rename-Item $_ -NewName ($_.name -replace 'jpg', $textfile[$i++]) -whatif}

Remove numbers from all files in a folder

I have a folder with a load of files with a random number in the middle. they are in this format:
Input-4535344534x-results.xml
Input-4535344534y-log.xml
Input-4535344534z-main.xml
Is there any way to remove the numbers through a .bat file or something I can execute as part of an ssis package so that I am left with:
Input-x-results.xml
Input-y-log.xml
Input-z-main.xml
I tried running this in powershell but it does not seem to work
Get-ChildItem -Filter *.xml | Rename-Item -NewName {$_.Name -replace '^[0-9_]+'}
Thanks
Something like this should work:
Get-ChildItem -Filter *.xml |
Foreach-Object {
$newName = $_.Name -replace "(\w+-)(\d+)(\w+)",'$1$3'
Rename-Item -Path $_.FullName -NewName $newName
}
UPDATE: Simplifying
Get-ChildItem -Filter *.xml | Rename-Item -NewName {$_.Name -replace "(\w+-)(\d+)(\w+)",'$1$3'}
Let me know if this helps.
ls Input-*.xml -n | % { mv $_ ($_ -replace '\d+') }

Powershell - Delete all non mp3 files

I´m totally new to Powershell and wanted to write a script that deletes all non-mp3 files in a directory.
My solution:
get-childitem -Recurse |
Where-Object {!($_.PSIsContainer)} |
Where {$_.Extension -ne ".mp3"} |
remove-item
What can be improved in this statement or could be written in another way.
Are there any problems with this statement?
Thank you.
I would use just one Where-Object command:
Get-childitem -Recurse |
Where-Object {!$_.PSIsContainer -AND $_.Extension -ne '.mp3'} |
Remove-Item -whatIf
If you're certain that no directories have 'mp3' extension :
Get-childitem -Recurse | Where-Object {$_.Extension -ne '.mp3'} |
Remove-Item -whatIf
Remove -whatIf to delete the files.