1

I am trying to parse the output of a WMIC command, but I noticed, when I redirect the output to a file, the string goes in with one readable character and one NUL character (0x00 according hex editor), making the output unusable for my purpose.

I want to parse this WMIC output by reading it one character at a time and discard the non-readable characters, at the end reconstructing the string as a continuous characters without weird spaces in between them.

How can I accomplish this in DOS (actually CMD prompt under win-7) batch file ?

My command

wmic process where processID=12345 get commandLine

Output directed to a file looks like this

 _C o m m a n d L i n e
 n o t e p a d     q . b a t

And I want it to look like

 _Command Line
 notepad    q.bat

I don't know how I can accomplish this, so I am not even going to feign what I am doing. My batch programming skills nearly non-existent.

Hackoo
  • 18,337
  • 3
  • 40
  • 70
MelBurslan
  • 2,383
  • 4
  • 18
  • 26
  • 1
    `wmic process where processID=12345 get commandLine | more` – Squashman Feb 22 '21 at 23:00
  • 2
    Does this answer your question? [Text garble in batch script for wmic command](https://stackoverflow.com/questions/25599445/text-garble-in-batch-script-for-wmic-command). Couple other questions I could link to as well if you searach the [tag:batch-file] tag for WMIC. – Squashman Feb 22 '21 at 23:05
  • If I let it go to the CMD prompt window, I don't see the 0x00 characters and it displays normally. I need to assign this output to variable and the only way I know how to do it in the simplest form, is by writing it to a file and reading it back using 'read /p variable= – MelBurslan Feb 22 '21 at 23:07

2 Answers2

0

You can pipe to the MORE command to output the file. That will get rid of the Unicode format and change it to ANSI.

But it would be just as easy to use a FOR /F command to capture the output of the WMIC output.

@echo off
for /F "usebackq tokens=1,2 delims==" %%G IN (`"wmic process where processID=16832 get commandLine /format:value |Findstr ."`) do set "%%~G=%%~H"
echo Command Line:%commandLine%
pause
Squashman
  • 13,649
  • 5
  • 27
  • 36
0

While searching more questions alike, I accidentally stumbled upon an answer while trying to modify these answers to serve my purpose. This might not hold true for everyone but instead of using

wmic process where processID=12345 get commandLine > out.txt

using

wmic /OUTPUT:"out.txt" process where processID=12345 get commandLine

You don't get the Uniocde characters and end up with ANSI output, which is what I needed.

MelBurslan
  • 2,383
  • 4
  • 18
  • 26