Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incorrect encoding after redirecting `dir` output to a file

I run this code on Windows cmd.exe in Europe and I use local settings here, for my language. So I use diacritics in names of the directories.

I try to list names of the directories and they are displayed correctly. Then I save them into file, but when I open it in notepad, the diacritics is not readable: for example, instead of Střední Čechy I have Stýednˇ ¬echy.

What did I do wrong and how can I correct it?

@echo off
del directories.conf
FOR /F "delims=!" %%R IN ('dir * /b /a:d /o:n') DO (

 IF EXIST "%%R\scenery" ( 
  echo %%R
  echo %%R >> directories.conf
 ) ELSE (ECHO NOT INCLUDED %%R)

)
Echo Directory list created...
pause
like image 213
John Boe Avatar asked Mar 28 '12 10:03

John Boe


2 Answers

Try starting cmd.exe with /u switch. That will cause cmd to write in UTF-16.

Also you need to switch to code page 1250 (ANSI for Central Europe) using chcp 1250.

You can do it inside your batch script. I made one for you. The structure is:

.\Jižní Morava
.\Jižní Morava\scenery
.\Pelhřimov
.\Pelhřimov\scenery
.\Nic moc výlet
.\Střední Čechy
.\Střední Čechy\scenery

And the script:

@echo off

if _%1_==_main_ (
    call :main
) else (
    cmd /u /c "%0 main"
)
goto :eof

:main
    chcp 1250
    del directories.conf
    for /F "delims=!" %%R in ('dir * /b /a:d /o:n') do (
        if exist %%R\scenery (
            echo %%R
            echo %%R >> directories.conf
        ) else (
            echo not included: %%R
        )
    )
    echo Directory list created...
    pause
goto :eof

Also I recommend you to read andrewdotn's great answer to a related question.

like image 188
Alois Mahdal Avatar answered Nov 10 '22 21:11

Alois Mahdal


As an alternative solution (if the file is already generated) you can just re-encode your file.

Notepad++ has this feature:

  • Go to Encoding > Character sets
  • Select the appropriate character set that has a graceful render
  • Go back to Encoding > Character sets
  • Select Convert to UTF-8
  • Save your file
like image 1
n0p Avatar answered Nov 10 '22 21:11

n0p