Can't replace PowerShell 2.0 filenames

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 filenames.

I am using this command but failed

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

The 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 is an error code in the code, it can 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 am doing wrong. I used to change filenames with this command.

+3


source to share


2 answers


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

I had to use Move-Item instead, you should use the following (tested on powershell 2.0):



Dir | Move-Item -Destination {$_.Name -replace "\[mix\]", ""}

      

+4


source


I was able to get the job done. Remember that [

both ]

are regex metacharacters and must be escaped:



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

      

+2


source







All Articles