Mark Towers
Mark Towers

Reputation: 3

Rename-Item - item does not exist (ANSI character issue in PowerShell)

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

Upvotes: 0

Views: 3312

Answers (3)

xz4m
xz4m

Reputation: 1

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!

Upvotes: 0

Adi Inbar
Adi Inbar

Reputation: 12321

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.

Upvotes: 1

Dirk
Dirk

Reputation: 676

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"

Upvotes: 0

Related Questions