0

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.

Leptonator
  • 3,379
  • 2
  • 38
  • 51
  • I would recommend PowerShell rather than batch (one line): `gwmi Win32_Service -Filter "Name LIKE 'tomcat%'" | % { $_.PathName }` – Bill_Stewart Mar 02 '17 at 17:55

4 Answers4

1

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.

aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • I like this answer the best as it covers what is needed and if we were playing script golf, I think this is the winner! I appreciate all of the inputs for this question. – Leptonator Mar 02 '17 at 20:09
0

If you want to do it in batch you can use this syntax instead. This just gives you the parent and Grandparent folder. If you need the full path to these then add the dp modifiers to the FOR variable.

@echo off
For /f "skip=1 delims=" %%G IN ('WMIC service WHERE "name LIKE 'wdpostman'" GET PathName ^|findstr /r /V "^$"') DO (
    for %%H in ("%%~dpG\.") do set "parent=%%~nxH"
    for %%H in ("%%~dpG\..") do set "gparent=%%~nxH"
)
echo parent=%parent%
echo gparent=%gparent%

Just wanted to add this extra set of code to show you how you can break up the folder paths into variables.

@echo off
setlocal enabledelayedexpansion
For /f "skip=1 delims=" %%G IN ('WMIC service WHERE "name LIKE 'wdpostman'" GET PathName ^|findstr /r /V "^$"') DO (
    set "_fpath=%%~dpG"
)
set "_fpath=%_fpath:~0,-1%"
set i=1
set "_spath!i!=%_fpath:\=" & set /A i+=1 & set "_spath!i!=%"
echo There %i% Folder(s^)
set _spath

Pause
Squashman
  • 13,649
  • 5
  • 27
  • 36
  • I am getting the following: `The following usage of the path operator in batch-parameter substitution is invalid: %~dpG\..` – Leptonator Mar 02 '17 at 18:06
  • Sorry. I missed double up the %. – Squashman Mar 02 '17 at 18:12
  • Not quite.. I was just pointed to: http://stackoverflow.com/questions/15685816/how-can-i-remove-empty-lines-from-wmic-output from one of my co-workers.. Posting the solution now. – Leptonator Mar 02 '17 at 18:17
  • @Leptonator, Yes you are correct. I was just about to add that code. I always forget about WMIC outputting empty lines. – Squashman Mar 02 '17 at 18:30
  • @Leptonator, I added an extra set of code to show you how to put each folder into a variable as well. – Squashman Mar 02 '17 at 18:45
  • Here is why this will not work in this example, Tomcat is started with some switches. Running the WMIC command we see, for results: `D:\tomcat\bin\tomcat8.exe //RS//Tomcat8` and note: the `//RS//Tomcat8` portion. – Leptonator Mar 02 '17 at 20:17
  • @Leptonator, that is easily solved by using a space as a delimiter. – Squashman Mar 03 '17 at 05:18
  • Or a forward slash as the delimiter. – Squashman Mar 03 '17 at 06:46
0

One of my colleagues at my work pointed me to:

How can I remove empty lines from wmic output? - and more specifically: https://stackoverflow.com/a/15686239/175063

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

I know it is not as elegant as @Squashman's solution, but this is what I am looking for or at least similar to.

Community
  • 1
  • 1
Leptonator
  • 3,379
  • 2
  • 38
  • 51
0

It is my experience that PathName actually returns additional lines. When I say that I mean on top of the usual 'odd' output of WMIC commands. To be sure that your output isn't affected I would solve it using Call.

@Echo Off
For /F "Skip=1 Delims=" %%A In (
    '"WMIC Service Where (Name Like 'tomcat%%') Get PathName"'
) Do Call :Sub %%A
Timeout -1
GoTo :EOF

:Sub
If Not "%~1"=="" Echo(%*
Compo
  • 36,585
  • 5
  • 27
  • 39