Home > front end >  Bile file output to text file not structured
Bile file output to text file not structured

Time:05-01

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

CodePudding user response:

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.

CodePudding user response:

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

CodePudding user response:

I would use a differnt command completely, net.exe is not really needed for the results you need.

@%SystemRoot%\System32\wbem\WMIC.exe /Output:"out.txt" UserAccount Get Name
@%SystemRoot%\System32\wbem\WMIC.exe /Append:"out.txt" QFE List Full 1>NUL
  • Related