Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't replace file names with PowerShell 2.0

I have files like this.

[mix]aaaa.flv
[mix]aaaa.mpv
[mix]aaaa.ogv
[mix]aaaa.webm
[mix]bb.flv
[mix]bb.mpv
[mix]bb.ogv
[mix]bb.webm
...

I just need to remove "[mix]" from the file names.

I use this command, but failed

Dir | Rename-Item –NewName { $_.name –replace "[mix]", "" }

Error says

Rename-Item : 'Microsoft.PowerShell.Core\FileSystem::C:\Users\Desktop\[mix]aaaa.mp4'에 항목이 없으므로 이름을 바꿀 수 없습니다.
위치 줄:1 문자:18
+ Dir | Rename-Item <<<<  –NewName { $_.name –replace “[mix]“,”” }
    + CategoryInfo          : InvalidOperation: (:) [Rename-Item], PSInvalidOperationException
    + FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.RenameItemCommand

There's korean in error code, it might say : Can't change name, there is no item at 'Microsoft.PowerShell.Core\FileSystem::C:\Users\Desktop\[mix]aaaa.mp4'

And I don't understand what I'm doing wrong. I used to change file names with this command.

like image 947
Deckard Avatar asked May 18 '26 10:05

Deckard


2 Answers

I think this is a known bug with Rename-Item and powershell 2.0

What I did was to use Move-Item instead, you should be able to use the following (tested on powershell 2.0):

Dir | Move-Item -Destination {$_.Name -replace "\[mix\]", ""}
like image 115
David Abrahamsson Avatar answered May 21 '26 00:05

David Abrahamsson


I was able to make this work. Remember that [ and ] are regex metacharacters and need to be escaped:

Dir | Rename-Item -NewName {$_.Name -replace "\[mix\]",""}
like image 37
Keith Hill Avatar answered May 21 '26 00:05

Keith Hill