How can I remove empty lines from wmic output?

10,030

Solution 1

Like this:

for /f "skip=1 delims=" %A in (
  'wmic path win32_service where "name like 'TeamViewer%'" get pathname ^| findstr /r /v "^$"'
) do set POSITION=%A

The findstr /r /v "^$" removes empty lines from the output.

Solution 2

wmic blah /value | find "=" >> wherever

output will be

field=value

no extra lines

tokenize from there, delim =

Share:
10,030
Admin
Author by

Admin

Updated on June 15, 2022

Comments

  • Admin
    Admin almost 2 years

    This is my string to obtain the position of TeamViewer (any version) service executable:

    for /f "skip=1 delims=" %A in ('wmic path win32_service where "name like 'TeamViewer%'" get pathname') do set POSITION=%A
    

    The problem is caused by wmic because it includes an empty line at the end of result (on Windows 7 command) and this is the output:

    C:\Users\giovanni>for /f "skip=1 delims=" %A in ('wmic path win32_service where "name like 'TeamViewer%
    '" get pathname') do set POSITION=%A
    
     :\Users\giovanni>set POSITION="C:\Program Files\TeamViewer\Version8\TeamViewer_Service.exe"
    
     :\Users\giovanni>set POSITION=
    
    C:\Users\giovanni>echo %position%
    ECHO enabled.
    

    How I can get only the second line of the output with the correct position of the executable? (or skip the latest line, of course).

    Thanks all in advance and have a nice day. Giovanni.

    This is checktv.bat:

    for /f "skip=1 delims=" %%A in ('wmic path win32_service where "name like 'TeamViewer%'" get pathname ^| findstr /r /v "^$"') do set POSITION=%%A
    echo %POSITION%
    
  • Admin
    Admin about 11 years
    And -I add to the request- can I include the complete string in a batch file? I need to specify something before?
  • Ansgar Wiechers
    Ansgar Wiechers about 11 years
    If you want to use this in a batch file you have to change %A to %%A.
  • Admin
    Admin about 11 years
    If i change %A in %AA the batch doesn't produce anything: for /f "skip=1 delims=" %%A in ('wmic path win32_service where "name like 'TeamViewer%'" get pathname ^| findstr /r /v "^$"') do set POSITION=%%A C:\>checkteamviewer.bat C:\>
  • Ansgar Wiechers
    Ansgar Wiechers about 11 years
    Add echo %POSITION% at the end of the batch file.
  • Admin
    Admin about 11 years
    Sorry Ansgar, I have edited my question and I have included "checktv.bat" code.
  • Ansgar Wiechers
    Ansgar Wiechers about 11 years
    Ah, I see. You have to double the % in the WMI query as well: "name like 'TeamViewer%%'".
  • Admin
    Admin about 11 years
    Yes, You got it! Thanks a lot :-)
  • Davor Josipovic
    Davor Josipovic over 10 years
    Nice point. The /VALUE switch doesn't seem to be documented when running wmic /?.