i have a few hundred files that i wish to rename
All have random strings at the start of the file name, but all have a constant in this example the word "File"
So how would i get from this
huhukifauduigui9983hodhohhh File 01 [145dde].ext
ljdwidnjncjbbbjbk File 55 [wniw].ext
wdwjbnkjbkjbiuiuiubi File 24 [wdwxxx].ext
plxwjijiwi File 14 [wddbb].ext
to this
File 01 [145dde].ext
File 55 [wniw].ext
File 24 [wdwxxx].ext
File 14 [wddbb].ext
Thank you in advance
The only thing i can think of is a powershell command but only really know how to replace specific strings
Use -replace
, PowerShell's regex-based string-replacement operator:
Get-ChildItem -Filter *.ext |
Rename-Item -NewName { $_.Name -replace '^.*\b(?=File)' } -WhatIf
Note: The -WhatIf
common parameter in the command above previews the operation. Remove -WhatIf
and re-execute once you're sure the operation will do what you want.
Regex ^.*\b(?=File)
matches any number (*
) of characters (.
) (possibly none) at the start of the file name (^
) up to, but not including (due to use of a lookahead assertion, (?=…)
) the word File
, which must occur at a word boundary (\b
).
For a more detailed explanation of the regex and the ability to experiment with it, see this regex101.com page.
Since no replacement operand is given, the empty string is implied resulting in the effective removal of the matching part.
Note that if a given file's name doesn't match the pattern, its name will be kept as-is.
Also note the use of a delay-bind script block with Rename-Item
's -NewName
parameter in order to dynamically determine the new file name based on each file-info object emitted by Get-ChildItem