将`dir'输出重定向到文件后编码不正确

编程入门 行业动态 更新时间:2024-10-10 06:20:17
本文介绍了将`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'输出重定向到文件后编码不正确

本文发布于:2023-11-02 07:15:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1551720.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不正确   重定向   文件   dir

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!