Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
692 views
in Technique[技术] by (71.8m points)

windows - cmd is somehow writing Chinese text as output

I have some trouble with cmd.exe. I use it sometimes to create files and write there the output. But if I try this:

wmic logicaldisk get name, freespace >> output.txt
echo %date% >> output.txt

And I start it two to three times, I get an output.txt like:

FreeSpace    Name  
17990881280  C:    
             D:    
????㈱??爀攀攀匀瀀愀挀攀????一愀洀攀???????? ????? ??????????????????????????????????金?′?

Well, the Chinese text there looks funny, but I would like to see the date. I think somehow the encoding is changed with date. Everything is fine and I get the date if I do echo %date% >> output.txt alone.

I would like to get the wmic output and the date.

What should I do?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The reason is that WMIC outputs to UNICODE. While the batch commands outputs to ANSI by default. Since the ANSI codepage is smaller than UNICODE and mapped differently, converting between them becomes a problem. There are several ways to solve this problem.

A. Start the command shell with the /U switch or if already in a command prompt, just type cmd /U.

Help from the "Help cmd" command: /U Causes the output of internal commands to a pipe or file to be Unicode

Thus, you will end up with a UNICODE text file and your original code needs no modification. However, you will need to remember to always use the /U switch. Also the correct way to do it is :

    wmic /OUTPUT:output.txt logicaldisk get name, freespace
    echo %date% >> output.txt

B. Convert the WMIC output to ANSI (Recommended. However depends on what you need. Just makes life easier when you decide to add to the text file. However, you will have to use 2 output files.).

   wmic /OUTPUT:output.tmp logicaldisk get name, freespace
   TYPE output.tmp > output.txt
   echo %date% >> output.txt

Hope this will help someone.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...