使用批处理,如何将unicode写入文件? [英] Using batch, how to write unicode into a file?

查看:124
本文介绍了使用批处理,如何将unicode写入文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要拖放文件夹/文件以递归方式将所有目录/文件及其子文件夹的文件打印到文件中.

I want to drag and drop folders/files print out all dirs/files and files of its subfolder recursively into a file.

@echo off
REM chcp 1250
REM chcp 65001

if [%1]==[] goto :eof
:loop
  echo %1 >> aText.txt
  for /f "tokens=* delims=" %%a in ('dir %1  /s /b') do (
    echo %%a >> aText.txt
  )
shift
if not [%1]==[] goto loop

aText.txt

@pause

这很好用,但是它不支持Unicode文件名.如果我将蝙蝠文件本身保存为UTF-8或Unicode,则它也不起作用. 我已经看过了: Windows命令行中的Unicode字符-怎样?

And that works fine, but it doesn't support Unicode filenames. It also doesn't work, if I save the bat file itself under UTF-8 or Unicode. I have looked at this: Unicode characters in Windows command line - how?

但这无法使其正常工作.我的猜测是,chcp使得可以在批处理文件中写入unicode,而不是在其创建的文件中写入unicode. 如何将unicode文件名写入其创建的文件中?

But this doesn't make it work. My guess is, the chcp makes it possible to write unicode in the batch file, and not unicode in the file it creates. How do I get the unicode filenames written into the file it creates?

编辑:

更准确地重新表述我的问题.我想编写此unicode,以便浏览器可以读取(例如,大多数是Chrome) 我现在所拥有的是这样:

To re-phrase my question more precisely. I want to write this unicode to be readable by the browser (e.g. Chrome mostly) What I have now is this:

@echo off
chcp 65001

if [%1]==[] goto :eof
:loop
  echo %1 > aText.txt
  for /f "tokens=* delims=" %%a in ('dir %1  /s /b') do (
echo   ^<br^>^<img src='%%a'^> >> aText.txt
REM    echo %%a >> aText.txt
  )
shift
if not [%1]==[] goto loop

aText.txt

@pause

所以我在记事本中打开它,它显示了unicode,一切都很好. (正如MC ND在回答中所述) 这给了我

So I open this in notepad, it shows the unicode, all fine. (Just as MC ND describes in the answer) This gives me:

D:\Downloads\unicodes 
  <br><img src='D:\Downloads\unicodes\sdsdsd.html'> 
  <br><img src='D:\Downloads\unicodes\ŽŽŽŽŽ.png'> 
  <br><img src='D:\Downloads\unicodes\中文.png'> 
  <br><img src='D:\Downloads\unicodes\文言.png'> 
  <br><img src='D:\Downloads\unicodes\日本語.png'> 
  <br><img src='D:\Downloads\unicodes\日本語.txt'> 
  <br><img src='D:\Downloads\unicodes\粵語.png'> 
  <br><img src='D:\Downloads\unicodes\한국어.png'> 

但是,当我用Chrome打开它时,会得到:

However, when I open this with Chrome it gets:

D:\Downloads\unicodes 
  <br><img src='D:\Downloads\unicodes\sdsdsd - Kopie.txt'> 
  <br><img src='D:\Downloads\unicodes\sdsdsd.html'> 
  <br><img src='D:\Downloads\unicodes\ŽŽŽŽŽ.png'> 
  <br><img src='D:\Downloads\unicodes\中文.png'> 
  <br><img src='D:\Downloads\unicodes\文言.png'> 
  <br><img src='D:\Downloads\unicodes\日本語.png'> 
  <br><img src='D:\Downloads\unicodes\日本語.txt'> 
  <br><img src='D:\Downloads\unicodes\粵語.png'> 
  <br><img src='D:\Downloads\unicodes\한국어.png'> 

很明显,当我将txt文件重命名为html文件时,即使是png文件,也只有一堆损坏的图像.

obviously, when I rename the txt file to an html file, there is just a bunch of broken images even for the png files.

当我在记事本中手动打开txt并以其他名称重新保存txt文件时,甚至不更改任何设置的编码(UTF-8)时,一切都可以按我的要求正常工作,但是我需要摆脱这种手动保存.

When I manually open the txt in notepad and re-save the txt file under a diffrent name, not even changing any of the set encodings (UTF-8), all works fine, as I want it, but I need to get rid of this manual saving.

使用npocmaka的CM \ u解决方案,我得到了每个字符之间都留有空格的内容,不幸的是,在无用的尝试之后,我突然似乎无法重现此内容,而是现在使用了它:

With npocmaka's CM \u solution I was getting something with spaces inbetween each character, unfortunately I suddenly don't seem to be able to reproduce this after trying around uselessly, and instead with this now:

@echo off
chcp 65001

cmd /u /c for /f "tokens=* delims=" %%a in ('dir %1 /s /b') do ( echo %%a >> aText.txt )

aText.txt

我知道

D:\Downloads>(echo D:\Downloads\unicodes\sdsdsd.html   ) 
D:\Downloads\unicodes\sdsdsd.html  

D:\Downloads>(echo D:\Downloads\unicodes\ŽŽŽŽŽ.png   ) 
D:\Downloads\unicodes\ŽŽŽŽŽ.png  

D:\Downloads>(echo D:\Downloads\unicodes\中文.png   ) 
D:\Downloads\unicodes\中文.png  

D:\Downloads>(echo D:\Downloads\unicodes\文言.png   ) 
D:\Downloads\unicodes\文言.png  

D:\Downloads>(echo D:\Downloads\unicodes\日本語.png   ) 
D:\Downloads\unicodes\日本語.png  

D:\Downloads>(echo D:\Downloads\unicodes\日本語.txt   ) 
D:\Downloads\unicodes\日本語.txt  

D:\Downloads>(echo D:\Downloads\unicodes\粵語.png   ) 
D:\Downloads\unicodes\粵語.png  

D:\Downloads>(echo D:\Downloads\unicodes\한국어.png   ) 
D:\Downloads\unicodes\한국어.png  

尽管回声本身的双行输出对我来说很奇怪,但是无论如何,在记事本中显示了unicode文件名,但是chrome甚至不想打开txt,并且将扩展名重命名为html ,它显示垃圾",如下所示:

whose double line output despite the echo off in itself is weird for me, but at any rate, in notepad the unicode filesnames are shown, but chrome would not want even want to open the txt, and upon renaming the extention to html, it shows "garbage" as follows:

D:\Downloads>(echo D:\Downloads\unicodes\sdsdsd.html ) D:\Downloads\unicodes\sdsdsd.html D:\Downloads>(echo D:\Downloads\unicodes\}}}}}.png ) D:\Downloads\unicodes\}}}}}.png D:\Downloads>(echo D:\Downloads\unicodes\-N‡e.png ) D:\Downloads\unicodes\-N‡e.png D:\Downloads>(echo D:\Downloads\unicodes\‡eŠ.png ) D:\Downloads\unicodes\‡eŠ.png D:\Downloads>(echo D:\Downloads\unicodes\åe,gžŠ.png ) D:\Downloads\unicodes\åe,gžŠ.png D:\Downloads>(echo D:\Downloads\unicodes\åe,gžŠ.txt ) D:\Downloads\unicodes\åe,gžŠ.txt D:\Downloads>(echo D:\Downloads\unicodes\µ|žŠ.png ) D:\Downloads\unicodes\µ|žŠ.png D:\Downloads>(echo D:\Downloads\unicodes\\Õm­´Å.png ) D:\Downloads\unicodes\\Õm­´Å.png

这不是我所需要的...

which is not what I need...

推荐答案

某些wmic命令要以unicode字符的形式写入文件时遇到了这个问题.这是我解决问题的方法:

I was having this problem with certain wmic commands want to write as unicode characters to the file. Here is how I resolved the problem:

echo %%a |more>> aText.txt

对于那些有兴趣的人,这也适用于WinPE.

This also works on WinPE, for those who may be interested.

这篇关于使用批处理,如何将unicode写入文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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