Cmd somehow writes Chinese text as output

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

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

And I start it 2-3 times, I get output.txt as:

 FreeSpace Name 17990881280 C: D:ใ ฑใ€ฎโธดใ€ฒใˆฑเด ไ˜Š็ˆ€ๆ”€ๆ”€ๅŒ€็€€ๆ„€ๆŒ€ๆ”€    ไธ€ๆ„€ๆด€ๆ”€  เด€เจ€ใ„€ใœ€ใค€ใค€ ใ €ใ €ใ„€ใˆ€ใ €   ไŒ€ใจ€    เด€เจ€             ไ€ใจ€    เด€เจ€ใ„€โธธใฐใˆฎใ„ฐโ€ฒเจ 

Well, this Chinese text looks funny, but I would like to see the date. I think somehow the encoding changes with the date. Because if I do echo% date% โ†’ output.txt, everything is fine, I get the date. What should I do, I would like to get wmic output and date. Any ideas? Thanks you

+4
source share
3 answers

WMIC has some weird results. I saw a discussion where it was said that WMIC uses unicode, but I think the situation is more complicated. If I take WMIC output to a file and use the hex editor, I see an extra carriage return at the end of each line. I absolutely do not understand how the contents of the date are converted to gibberish when output.txt is typed. (encoding problem, but how?) On my machine I get question marks where the date should be.

I was able to solve the problem using

 wmic logicaldisk get name, freespace | more >>output.txt echo %date%>>output.txt 
+7
source

The reason is the WMIC results for UNICODE. While command commands exit to ANSI by default. Since the ANSI code page is smaller than UNICODE and displays differently, converting between them becomes a problem. There are several ways to solve this problem.

a. Launch a command shell using the / U switch or, if already on the command line, just type cmd / U.

Help from the Help cmd command: / U Invokes output of internal commands to a channel or file in Unicode

Thus, you will receive a UNICODE text file, and your source code does not need to be modified. However, you need to remember that always use the / U switch. Also the right way to do this:

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

B. Converting WMIC output to ANSI (recommended. However, it depends on what you need. It just makes life easier when you decide to add to a 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 helps someone.

+6
source

I found that the script package used to create the txt output file using stdout "โ†’" to create the txt file is compatible with the powershell ascii, utf8 and utf7 output formats, all other output formats lead to additional spaces in the file.

If the output file is created using powershells stdout "โ†’", then any addition of stdout to the file from the script package will be displayed as Chinese characters.

The workaround in my case was to re-create the output file with the script package, and it would be in the correct format, and subsequent additions to powershell should use "| out-file -encoding ascii / utf8 / utf7"

0
source

All Articles