I'm trying to bulk rename a bunch of files with PowerShell, however, I run into an error which I don't know what to do with and searching for it didn't give me any solutions either.
Error message:
Rename-Item : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'NewName'.
For example: 2068227794 (0x8f860be3).bnk
to 0x8f860be3_0001.bnk
In this particular case $hex_name
is 0x8f860be3
.
$source = "K:\_test"
Get-ChildItem -Path $source -File -Recurse -Include "* (0x*).*" | ForEach-Object {
$prefix, $hex_name = ($_.BaseName).Split('()')
Rename-Item -Path $_ -NewName ($hex_name + "_0001" + $_.Extension)
}
As this question was answered in the comments, I'll answer my own question so it is marked as solved.
The error occurs as $hex_name
is an array which contains the hex string and the rest. There are 2 simple solutions:
Selecting the item directly:
$hex_name = ($_.BaseName).Split('()')[1]
Assigning the unwanted rest to a new variable:
$prefix, $hex_name, $rest = ($_.BaseName).Split('()')
All credit goes to EBGreen.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments