Mixed ascii and unicode output from script - how t

2020-05-01 04:38发布

问题:

I have a script which is run from a service which is built using C++ and is not built using unicode. This program runs the script below and strangely, the output from the script running the wmic qfe list line seems to output as unicode. Sample output below.

Here is the script:

@echo off
echo This text is output as standard - no spacing between characters >>C:\log.txt

REM this one is output with 1 space between characters
wmic qfe list >>C:\log.txt

echo This text is also output as standard - no spacing >>C:\log.txt

Sample output

This text is output as standard - no spacing between characters
C a p t i o n                                                                                 C S N a m e                 D e s c r i p t i o n             F i x C o m m e n t s     H o t F i x I D       I n s t a l l D a t e     I n s t a l l e d B y                                   I n s t a l l e d O n     N a m e     S e r v i c e P a c k I n E f f e c t     S t a t u s     

h t t p : / / s u p p o r t . m i c r o s o f t . c o m / ? k b i d = 4 0 1 2 2 1 2           M F M - 7 4 2 - 0 - 7 1     S e c u r i t y   U p d a t e                               K B 4 0 1 2 2 1 2                               N T   A U T H O R I T Y \ S Y S T E M                   5 / 1 8 / 2 0 1 7                   

This text is also output as standard - no spacing 

Actually, I just viewed the file using a hex editor and the characters between the letters are null characters. so presumably wmic qfe list must be outputting unicode.

So how do I either get it all output as ascii or all output as unicode. Everything output as ascii would be preferred.

回答1:

wmic qfe list | find /v "" >> c:\log.txt
wmic qfe list | findstr "^" >> c:\log.txt

Pipe the output of the wmic command to a find or findstr command (you can also use sort if you don't have problems with the order, or more if your output has less than 65535 lines) before redirecting the output to the file.