0

I'm having problems renaming items that have ANSI characters in them using PowerShell.

Examples are characters like "é" in "\Michael Bublé" or "\Green Day\¡DOS!"

What I want to do is rename "\Michael Bublé" to "\Michael Buble".

I've done the code to map characters to a-z characters. The problems is that when I do either a Rename-Item operation or a delete operation the system reports the file doesn't exist.

I think this might be an encoding issue. I can't find any way around it.

ren : Cannot rename because item at 'Michael Bublé' does not exist.
At C:\...\Replace_non_Ascii_FileObjectName.ps1:24 char:5
+     ren $_ $NewName 
+     ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Rename-Item], PSInvalidOperationException
    + FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.RenameItemCommand
Adi Inbar
  • 12,097
  • 13
  • 56
  • 69
Mark Towers
  • 3
  • 1
  • 2

3 Answers3

1

Your question is a little skimpy on details, so it's hard to guess what's going on. It would help if you would provide the full commands you're using verbatim rather than describing them. Also, how are you entering the é into your command? Are you copying the filename from the directory listing, are you using a keyboard or keyboard emulator that has that character, are you typing it into a word processor and copying from there...? The more detailed and specific you are, the more likely it is that someone will be able to answer your question.

However, there are two suggestions I can make.

The first is that based on what you wrote I suspect that the special characters aren't even the problem. Is this only occurring with filenames that have non-ASCII characters? Is this the command you're using?

Rename-Item "\Michael Bublé" "\Michael Buble"

If so, that won't work unless Michael Bublé is in the root directory of your current working drive, and that's regardless of whether you have non-ASCII characters in the name. It should be

Rename-Item ".\Michael Bublé" ".\Michael Buble"

That's assuming that the file is in the current working directory. Otherwise, instead of ., provide the full path.

The second is that if you think encoding is the issue, you could try interpolating the characters into the string by casting the unicode numbers as [char]: "Michael Bubl$([char]0x00E9)" and "\Green Day\$([char]0x00A1)DOS!"

That's kind of a shot in the dark, though, because you shouldn't have to do that. If it displays correctly on your screen, it should be interpreted correctly by PowerShell. Especially if you're copying the name from the directory listing.

Adi Inbar
  • 12,097
  • 13
  • 56
  • 69
  • Thanks for the comments guys. Much appreciated. I think you are right Adi. I'm not referencing the full path with the rename-item command so it's just containing the artists name and not the full folder structure above it. I'll research it a bit more. New to powershell but really impressed so far! Thanks for the help :) – Mark Towers Oct 21 '13 at 20:17
  • It shouldn't take much research. Did you try invoking the command I suggested, with a dot in front of the backslash, from the directory in which the files reside? In fact, you don't even need the second `.\ `. This should work just as well: `Rename-Item '.\Michael Bublé' 'Michael Buble'`. You could also use tab completion. Type `rni Mic`, then hit the **[TAB]** key (possibly several times if you have other files beginning with "Mic"), and you'll see that it expands to `'.\Michael Bublé'`, i.e. the tab expansion function will put the `.\ ` in for you. – Adi Inbar Oct 21 '13 at 21:16
  • This was never resolved. I still have this same issue; with no workings solution. None of the Answers provide a working solution. – MKANET Apr 19 '14 at 23:16
0

One possible workaround would be to use a wildcard:

dir "Michael Bubl?" | ren -NewName "Michael Buble"

Or pipe it to where:

dir | where {$_.Name -eq "Michael Bublé"} | ren -NewName "Michael Buble"
Dirk
  • 666
  • 6
  • 5
0

A workaround is to use 'cmd /c rename ...' using relative paths. I did this by changing directory to the folder the file was in and then calling the cmd rename command like this:

$num=1
$files = gci -recurse
$files | ForEach-Object{
  $folderPath = split-path $_.Fullname -parent
  $originalFileName = split-path $_.Fullname -leaf
  **$newFileName = "Michael Buble" + $num**
  $num+=1
  cmd /c echo "$folderPath" "$originalFileName" "$newFileName"
  cd $folderPath
  cmd /c rename "$originalFileName" "$newFileName"
}

Obviously you can replace the newFileName part with your mapping. I hope this helps!

xz4m
  • 1