将`dir'输出重定向到文件后编码不正确 [英] Incorrect encoding after redirecting `dir` output to a file

查看:389
本文介绍了将`dir'输出重定向到文件后编码不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在欧洲的Windows cmd.exe上运行此代码,我在此使用本地设置作为我的语言。所以我在目录的名称中使用变音符号。

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.

我尝试列出目录的名称,并且它们正确显示。然后我将它们保存到文件中,但是当我在记事本中打开它时,变音符不可读:例如,而不是StředníČechy我有Stýedn

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


推荐答案

尝试使用/ u启动cmd.exe开关。这将导致cmd写入UTF-16。

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

还需要使用 chcp 1250切换到代码页1250(中欧的ANSI)

您可以在批处理脚本中执行此操作。我为你做了一个。结构是:

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

和脚本:

@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

此外,我建议您阅读 andrewdotn的好回答到相关问题。

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

这篇关于将`dir'输出重定向到文件后编码不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆