2

Not trying to do anything drastic, but I'm renaming my music files from the mp3 title tags. Came across a file this problem:-

$fyl = One Vision [From the Motion Picture Iron Eagle].mp3

$tmp = "tmp.mp3"

$track_title = One vision [From the motion picture Iron Eagle]

rename-item "$mp3path$fyl" -newname "$mp3path$tmp"

rename-item "$mp3path$tmp" -newname "$mp3path$track_title.mp3"

I get the error:- Rename-Item : Cannot rename because item at 'F:\Music\Queen\Live Magic\One Vision [From the Motion Picture Iron Eagle].mp3' does not exist.

The file exists!

I go through a tmp.mp3 in case the files are the same if looked at without case sensitivity.

The code works for files without square brackets, so obviously Powershell is taking them literally.

I did try replacing Rename-Item with Move-Item, but I get similar errors.

Any help is appreciated.

5
  • Quick suggestion to try out, have you tried escaping the square bracket characters? $fyl = One Vision \[From the Motion Picture Iron Eagle\].mp3 Commented Jan 11, 2016 at 20:47
  • 1
    I am noticing no quotes on the strings, normally they should be quoted (Single quotes if you don't want to escape. Commented Jan 11, 2016 at 20:53
  • 1
    Possible duplicate of How to access file paths in Powershell containing special characters? Commented Jan 11, 2016 at 20:56
  • $fyl is derived from Foreach ( $fyl in Get-Childitem $mp3path -filter "*.mp3" ) where $mp3path is "F:\My Music\Queen\Live Magic" and $track_title is derived from using taglib-shell.dll to get the albumartists tag from the mp3 file Commented Jan 11, 2016 at 21:57
  • your code does not show $fyl being a collection. That would change how you need to use $fyl. Commented Jan 12, 2016 at 0:25

4 Answers 4

11

The -LiteralPath knows how to handle special characters try this:

Rename-Item -LiteralPath C:\[filename].txt -NewName filename.txt
Sign up to request clarification or add additional context in comments.

2 Comments

Just used the -LiteralPath parameter, but it still fails to rename the file. It produced the following:- Rename-Item : A parameter cannot be found that matches parameter name 'LiteralPath' Powershell is Major 2, minor 0, build -1, revision -1
Can you install powershell v4.0? That should fix this
0

I am able to new, rename, remove, and get such files.

This script should work successfully for you as well, and maybe you can use it to debug your script.

Note, that I use Join-Path out of habit and best practice to ensure paths are valid.

Test Code:

$mp3dir = "d:\test"
$fyl = "One Vision [From the Motion Picture Iron Eagle].mp3"
$tmp = "tmp.mp3"
$track_title = "One vision [From the motion picture Iron Eagle]"
New-Item -Path $mp3dir -Name $fyl -ItemType file -Force | Out-Null
$found = Get-ChildItem -Path $mp3dir -Filter *.mp3
"Found1:"
$found | % {$_.FullName}
Remove-Item $(Join-Path $mp3dir $tmp) -ErrorAction SilentlyContinue
Rename-Item -LiteralPath $(Join-Path $mp3dir $fyl) -NewName $tmp -Force
$found = Get-ChildItem -Path $mp3dir -Filter *.mp3
"Found2:"
$found | % {$_.FullName}
$newName = $(Join-Path $mp3dir $($track_title + ".mp3"))
"New name:`n$newName"
Remove-Item $(Join-Path $mp3dir $newName) -ErrorAction SilentlyContinue
Rename-Item -LiteralPath $(Join-Path $mp3dir $tmp) -NewName $newName -Force
$found = Get-ChildItem -Path $mp3dir -Filter *.mp3
"Found3:"
$found | % {$_.FullName}

Test Output

Found1:
D:\test\One Vision [From the motion picture Iron Eagle].mp3
Found2:
D:\test\tmp.mp3
New name:
d:\test\One vision [From the motion picture Iron Eagle].mp3
Found3:
D:\test\One vision [From the motion picture Iron Eagle].mp3

2 Comments

Tried your script, but I'm getting error similar to above where it complains about Rename-Item : A parameter cannot be found that matches parameter name 'LiteralPath' I will keep trying to get this to work. Perhaps I need to update my Powershell?
What is the output from $PSVersionTable?
0

From your comments to other answers I gather, that you are using PowerShell v2.

Now, the Rename-Item cmdlet in PowerShell v2 does not seem to have the -LiteralPath parameter, which you need in order to correctly interpret a path that contains special characters such as square brackets.

An alternative cmdlet to Rename-Item is Move-Item which (curiously) does support the -LiteralPath parameter even in PowerShell v2.

Instead of:

rename-item "$mp3path$fyl" -newname "$mp3path$tmp"

try:

Move-Item -LiteralPath "$mp3path$fyl" -Destination "$mp3path$tmp"

In your question you said, that you already tried Move-Item instead of Rename-Item. But did you try Move-Item in combination with -LiteralPath?

Comments

0

The move-item -literalpath $var1 -destination $var2 method worked :-) I also found that by using [System.IO.File]::Move( "$mp3path$fyl" , "$mp3path$track_title.mp3") I was able to rename the file as I wanted without having to go through a tmp file. Later testing proved that the move-item -literalpath method didn't need a temporary file either.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.