Search code examples
batch-filewmiwmic

WMIC getting the grand parent in batch


Have been able to put together the following which does work, but has a extra line at the end.

For /f "tokens=1,2 skip=1 delims=\" %%i IN (
  'WMIC service WHERE "name LIKE 'tomcat%%'" GET PathName'
) DO (
echo %%i\%%j
)
Pause

When run, we see:

  • D:\tomcat
  • D:\tomcat2
  • \

and \ is not correct.


Solution

  • Wrap around another for /F loop to get rid of the Unicode-to-ANSI text conversion artefacts (orphaned carriage-return characters) left by for /F:

    for /F "skip=1 delims=" %%a in ('
        wmic Service where "Name LIKE 'tomcat%%'" get PathName
    ') do (
        for /F "tokens=1,2 delims=\" %%i in ("%%a") do (
            echo %%i\%%j
        )
    )
    

    The outer loop reads the full lines except the one you want to skip; the inner one parses the text.