Home > other >  filter out text inside loop in batch file
filter out text inside loop in batch file

Time:10-09

I am trying to process the output of wmic command in for loop:

@echo off

FOR /F "tokens=*" %%a in ('wmic process where "commandline like '%%w3wp.exe%%SharePoint%%-h%%config%%'" get commandline /VALUE /format:value') do (
    echo %%a
)

It prints the following:

ECHO is off.
ECHO is off.
CommandLine=c:\windows\system32\inetsrv\w3wp.exe -ap "SharePoint Central Administration v4" -v "v4.0" -l "webengine4.dll" -a \\.\pipe\iisipm8d1b7d9b-4aee-49b5-87f0-1f5e78228544 -h "C:\inetpub\temp\apppools\SharePoint Central Administration v4\SharePoint Central Administration v4.config" -w "" -m 0



ECHO is off.
ECHO is off.
CommandLine=C:\Windows\system32\cmd.exe /c wmic process where "commandline like '%w3wp.exe%SharePoint%-h%config%'" get commandline /VALUE /format:value

ECHO is off.
ECHO is off.
CommandLine=wmic  process where "commandline like '%w3wp.exe%SharePoint%-h%config%'" get commandline /VALUE /format:value

ECHO is off.
ECHO is off.
ECHO is off.

What I am trying to do is to process only:

CommandLine=c:\windows\system32\inetsrv\w3wp.exe -ap "SharePoint Central Administration v4" -v "v4.0" -l "webengine4.dll" -a \\.\pipe\iisipm8d1b7d9b-4aee-49b5-87f0-1f5e78228544 -h "C:\inetpub\temp\apppools\SharePoint Central Administration v4\SharePoint Central Administration v4.config" -w "" -m 0

It seems that the following works:

@echo off

FOR /F "tokens=* skip=2" %%a in ('wmic process where "commandline like '%%w3wp.exe%%SharePoint%%-h%%config%%'" get commandline /VALUE /format:value') do (
    SET OUTPUT=%%a & goto :next
)
:next
echo %OUTPUT%
#rem CommandLine=c:\windows\system32\inetsrv\w3wp.exe -ap "SharePoint Central Administration v4" -v "v4.0" -l "webengine4.dll" -a \\.\pipe\iisipm8d1b7d9b-4aee-49b5-87f0-1f5e78228544 -h "C:\inetpub\temp\apppools\SharePoint Central Administration v4\SharePoint Central Administration v4.config" -w "" -m 0 

But it seems that skip=2 part depends on the wmic output format.

Is it possible to get the desired string without the skip=2 part?

I tried this, but this doesn't work(not sure I understand why):

@echo off

setlocal EnableDelayedExpansion
set "substring=SharePoint"
FOR /F "tokens=*" %%a in ('wmic process where "commandline like '%%w3wp.exe%%SharePoint%%-h%%config%%'" get commandline /VALUE /format:value') do (
    set "string=%%a"
    if "!string:%substring%=!"=="!string!" (
        echo(!string!
        goto :next
    )
)
:next

endlocal

CodePudding user response:

In your particular example above, you'd need ensure that you isolate only the process you're after, and not capture also the command lines you're using in the batch file too.

You could do that, by including a NOT LIKE filter:

@Echo Off
SetLocal EnableExtensions

For /F "Tokens=1,* Delims==" %%G In ('
 %SystemRoot%\System32\wbem\WMIC.exe Process Where
 "CommandLine Like '%%w3wp.exe%%SharePoint%%-h%%config%%' And Not CommandLine Like '%%WMIC.exe%%'"
 Get CommandLine /Format:List 2^>NUL') Do For /F "Tokens=*" %%I In ("%%H"
) Do Echo %%I

Pause

Alternatively, (and IMO better based upon your provided output), filter first by the known NAME of the executable:

@Echo Off
SetLocal EnableExtensions

For /F "Tokens=1,* Delims==" %%G In ('
 %SystemRoot%\System32\wbem\WMIC.exe Process Where
 "Name = 'w3wp.exe' And CommandLine Like '%%w3wp.exe%%SharePoint%%-h%%config%%'"
 Get CommandLine /Format:List 2^>NUL') Do For /F "Tokens=*" %%I In ("%%H"
) Do Echo %%I

Pause

CodePudding user response:

WMIC is outputting extra sets of CRLF. That is why you see the ECHO is OFF error. So just pipe the WMIC command to findstr to get just the output you want.

FOR /F "tokens=*" %%a in ('wmic process where "commandline like '%%w3wp.exe%%SharePoint%%-h%%config%%'" get commandline /VALUE /format:value ^|findstr /B /I "CommandLine=') do (
    echo %%a
)
  • Related