0

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.

John_S
  • 53
  • 2
  • 8
  • Quick suggestion to try out, have you tried escaping the square bracket characters? `$fyl = One Vision \[From the Motion Picture Iron Eagle\].mp3` – Gnarlywhale Jan 11 '16 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. – Nick Young Jan 11 '16 at 20:53
  • 1
    Possible duplicate of [How to access file paths in Powershell containing special characters?](http://stackoverflow.com/questions/12090312/how-to-access-file-paths-in-powershell-containing-special-characters) – Matt Jan 11 '16 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 – John_S Jan 11 '16 at 21:57
  • your code does not show $fyl being a collection. That would change how you need to use $fyl. – Kory Gill Jan 12 '16 at 00:25

4 Answers4

9

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

Rename-Item -LiteralPath C:\[filename].txt -NewName filename.txt
Oz Bar-Shalom
  • 1,747
  • 1
  • 18
  • 33
  • 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 – John_S Jan 11 '16 at 21:27
  • Can you install powershell v4.0? That should fix this – Oz Bar-Shalom Jan 12 '16 at 15:10
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
Kory Gill
  • 6,993
  • 1
  • 25
  • 33
  • 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? – John_S Jan 12 '16 at 05:26
  • What is the output from `$PSVersionTable`? – Kory Gill Jan 12 '16 at 05:28
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?

Manuel Batsching
  • 3,406
  • 14
  • 20
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.

John_S
  • 53
  • 2
  • 8