Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bile file output to text file not structured

Tags:

batch-file

I am running these two commands net users >> out.txt and wmic qfe list full >>out.txt in a batch file. After doing so, the output of the wmic command displays null characters after every other character. Is there a way I can fix this? When i output those two commands info in separate text files, the output is perfectly fine. I am super confused about this as to why this is happening

like image 871
quarantine_code Avatar asked Dec 19 '25 02:12

quarantine_code


2 Answers

wmic uses little-endian UTF-16 to display its output, while net users uses regular ANSI. In UTF-16, each character is two bytes long, and the standard ASCII character set is its regular ASCII value followed by a null byte.

When you run net users >> out.txt first, you force the output file to not be a UTF-16-encoded file, so the output of the wmic command is not displayed correctly. (Incidentally, if you simply tried to swap the order of the commands, you'd find that the net users command would appear as Chinese characters.)

The only way to get wmic output into ANSI is to run it through multiple for /f loops:

net users >> out.txt
for /f "delims=" %A in ('wmic qfe list full') do for /f "delims=" %B in ("%~A") do echo %B >> out.txt

Note that if you're putting this in a script, you need to use %%A and %%B instead of %A and %B.

like image 190
SomethingDark Avatar answered Dec 21 '25 03:12

SomethingDark


wmic uses a different character set and has some additional CRLF in it's output. One way is to use a for loop:

from cmd

(net users && @for /F "delims=" %i in ('wmic qfe list full') do @echo(%i)>out.txt

In a batch-file

(net users && @for /F "delims=" %%i in ('wmic qfe list full') do @echo(%%i)>out.txt