Working with directory with special characters

Asked

Viewed 999 times

3

I have the following script:

$ExistPath = Test-Path -PathType container  C:\Publicação\SQL-Release\Services 


if ($ExistPath)
 {
     Write-Host "Removendo diretorio!"
     Remove-Item -Path C:\Publicação\SQL-Release\Services -Recurse -Force
 }

 Write-Host "Criando diretorio!"
 New-Item -ItemType Directory -Force -Path C:\Publicação\SQL-Release\Services

 Copy-Item .\Services\MyServices\bin\Release C:\Publicação\SQL-Release\Services\Nalin -Recurse

But when I run it creates a directory with very different characters than expected. In this case:

C:\Publicação

How to solve?

Thank you.

1 answer

3


You need to change the encoding (encoding) script through the command chcp.

In short, the command changes the encoding in which your script is interpreted by powershell. The default code page for the Portuguese language is 860, so using the following command before the rest of the script should solve the problem:

chcp 860

Note that calling the argument-free command causes Powershell to print the code page currently in use on the screen.

Link to command documentation, if you are curious, or need to do more things related to it in the future.

p.s.: the documentation does not mention, but there are pages of code with much higher numbers. Undocumented pages may represent, for example, UNICODE. I believe, however, that page 860 is already enough to solve the problem of the question.

  • I ran the script here on my computer and it worked, created the directory correctly [C: SQL-Release Services publication], it is normal to have this divergence of encoding?

  • I put the command as the first line of my script and it did not work as expected. But when I changed the way the script file from UTF-8 to UTF-8 with BOM worked.

  • 1

    @Mathias as far as I know the Powershell console already opens with a selected default encoding. I don’t know if this behavior is repeated in all versions of the console, or even Windows (because this may be the system and not Powershell itself).

  • @Rodolphosa o BOM alters the encoding of the file, so that must be it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.