[Question] Bulk renaming files with parentheses and kanji in filenames.

Solved

Like in title, I have a bunch of files I want to quickly rename (delete part of name to be precise). Sounds simple. I'm using Powershell ISE 3.0.

I navigate to the folder with files to be renamed, and execute this command chain:

Get-ChildItem -rec | ForEach-Object -Process {$_.FullName} | move-item -literalpath $_ $_.Name.Replace("(生け花) ", "")

That returns error:

You cannot call a method on a null-valued expression.
At line:1 char:1
+ Get-ChildItem -rec | ForEach-Object -Process {$_.FullName} | move-item -literalp ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

I'm using Move-Item command, because Rename-Item wouldn't even properly execute with parentheses in filename.

This is literally my first time using Powershell, and I just bungled it up from random posts on the internet.

What exactly am I doing wrong?

Posted Dec 17, 2013, 2:45 PM UTC

36
pertymoose•
Get-ChildItem -Recurse | select FullName -ExpandProperty FullName | foreach { Rename-Item $_ ($_ -replace '\(生け花\)','') }
  • Get-ChildItem -Recurse > You now have a list of all items in folders and subfolders of whichever folder you're in.
  • Pass list to Select-Object where you expand the list and essentially create a new list containing only the FullName values
  • Pass the expanded list to Foreach-Object
  • Iterate the list and call Rename-Item to rename the current item $_ to ($_ -replace ...)
  • Make sure to escape parenthesies when using -replace as it implicitly uses regex to replace, compared to $string.Replace() which uses the .net string replace method
2
ginx2666OP•

I'm getting

Rename-Item : Cannot rename because item at 'D:\DOWNLOAD\Flowers\生け花\(生け花) 1000.zip' does not exist.
At line:1 char:79
+ Get-ChildItem -Recurse | select FullName -ExpandProperty FullName | foreach { Re ...
+                                                                               ~~
    + CategoryInfo          : InvalidOperation: (:) [Rename-Item], PSInvalidOperationException
    + FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.RenameItemCommand

for each file.

1
pertymoose•

Try running through it first without using one-liners to make sure you're getting the values you want

$items = Get-ChildItem -Recurse
$items

Then

$itemsWithPath = $items | select FullName -ExpandProperty FullName 
$itemsWithPath

And then

#$itemsWithPath | foreach { Rename-Item $_ ($_ -replace '\(生け花\)','') }
$itemsWithPath | foreach {
    $newName = ($_ -replace '\(生け花\)','')

    Write-Host "Old: $_"
    Write-Host "New: $newName"
    Write-Host ""
}
2
ginx2666OP•

It executes without errors, but doesn't change filenames. All paths are correct from what I can see.

PS D:\DOWNLOAD\Flowers\生け花> $items = Get-ChildItem -Recurse
$items


    Directory: D:\DOWNLOAD\Flowers\生け花


Mode                LastWriteTime     Length Name                                                                                                                                                               
----                -------------     ------ ----                                                                                                                                                               
-a---        2013-12-17     16:32  175183707 (生け花) 1000.zip                                                                                                                                                     
-a---        2013-05-27     20:49  199430038 (生け花) 1001.zip                                                                                                                                                     



PS D:\DOWNLOAD\Flowers\生け花> $itemsWithPath = $items | select FullName -ExpandProperty FullName 
$itemsWithPath
D:\DOWNLOAD\Flowers\生け花\(生け花) 1000.zip
D:\DOWNLOAD\Flowers\生け花\(生け花) 1001.zip

PS D:\DOWNLOAD\Flowers\生け花> #$itemsWithPath | foreach { Rename-Item $_ ($_ -replace '\(生け花\) ','') }
$itemsWithPath | foreach {
    $newName = ($_ -replace '\(生け花\) ','')

    Write-Host "Old: $_"
    Write-Host "New: $newName"
    Write-Host ""
}
Old: D:\DOWNLOAD\Flowers\生け花\(生け花) 1000.zip
New: D:\DOWNLOAD\Flowers\生け花\1000.zip

Old: D:\DOWNLOAD\Flowers\生け花\(生け花) 1001.zip
New: D:\DOWNLOAD\Flowers\生け花\1001.zip

I am using this temporary directory with only two files, so if something goes haywire, I won't lose all of them.

1
pertymoose•

Ah, well, I commented out the renaming part so that's not too surprising.

Try this:

$itemsWithPath | foreach {
    $newName = ($_ -replace '\(生け花\)','')

    Write-Host "Old: $_"
    Write-Host "New: $newName"
    Write-Host ""
    Rename-Item $_ $newName

}
2