Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

WMIC in the for loop within my script doesn't work and I have no idea why!

536 views
Skip to first unread message

JoeBloggs

unread,
Feb 25, 2011, 8:02:02 AM2/25/11
to
Try this as a cmd batch file. I cannot get it to accept the WHERE
NAME="cmd" no matter how I escape the quotes or pre make WMIC command
line as a variable.
But the same WMIC line works on the command line.

Driving me nuts!

@echo off
setlocal enableextensions EnableDelayedExpansion


:ME
set ProcessName=cmd
set wmicmd=WMIC PATH Win32_PerfFormattedData_PerfProc_Process WHERE
NAME='%ProcessName%' GET idprocess

rem echo.%wmicmd%

for /F "tokens=1-2" %%A in ('"WMIC PATH
Win32_PerfFormattedData_PerfProc_Process WHERE NAME="cmd" Get
idProcess"') do set cPID=%%A & set cCPU=%%B
echo.!cPID!
echo.!cCPU!

REM SLEEP FOR ONE SEC
ping 1.1.1.1 -n 1 -w 1000 >NUL

goto ME
endlocal

foxidrive

unread,
Feb 25, 2011, 8:50:20 AM2/25/11
to
On 26/02/2011 00:02, JoeBloggs wrote:
> Try this as a cmd batch file. I cannot get it to accept the WHERE
> NAME="cmd" no matter how I escape the quotes or pre make WMIC command
> line as a variable.
> But the same WMIC line works on the command line.
>
> Driving me nuts!
>
> @echo off
> setlocal enableextensions EnableDelayedExpansion
>
>
> :ME
> set ProcessName=cmd
> set wmicmd=WMIC PATH Win32_PerfFormattedData_PerfProc_Process WHERE
> NAME='%ProcessName%' GET idprocess
>
> rem echo.%wmicmd%
>
> for /F "tokens=1-2" %%A in ('"WMIC PATH
> Win32_PerfFormattedData_PerfProc_Process WHERE NAME="cmd" Get
> idProcess"') do set cPID=%%A& set cCPU=%%B

> echo.!cPID!
> echo.!cCPU!
>
> REM SLEEP FOR ONE SEC
> ping 1.1.1.1 -n 1 -w 1000>NUL
>
> goto ME
> endlocal


This works to report all CMD PIDs but the variable will only be set to
the last one. What are you trying to do?

@echo off
for /F "skip=1" %%A in ('"WMIC PATH

Win32_PerfFormattedData_PerfProc_Process WHERE NAME="cmd" Get

idProcess"') do echo "%%A"& set cPID=%%A
pause


--
Regards,
Mic

Tom Lavedas

unread,
Feb 25, 2011, 10:18:24 AM2/25/11
to

Try adding the usebackq parameter to the FOR statement ...

for /F "usebackq tokens=1-2" %%A in (


'WMIC PATH Win32_PerfFormattedData_PerfProc_Process WHERE NAME="cmd"
Get idProcess'

) do set cPID=%%A & set cCPU=%%b

The FOR was having trouble interpreting the statement because of the
double quotes.

BTW, I assume you removed the cCPU part for the posting, as it is,
that part will always be empty. Actually, I can't figure out what
parameter you intend for that part, but that's not my problem, is
it ;-)
__________________
Tom Lavedas

01MDM

unread,
Feb 25, 2011, 10:20:10 AM2/25/11
to

> set ProcessName=cmd
> set wmicmd=WMIC PATH Win32_PerfFormattedData_PerfProc_Process WHERE NAME='%ProcessName%' GET idprocess

2 ways to escape errors in 'for /f circle':

1st: set "wmicmd=WMIC ... WHERE NAME^="%ProcessName%" ..."

and

2nd: set wmicmd="WMIC ... WHERE NAME="%ProcessName%" ..."

or

for /f ... ('"%wmicmd%"') do ...

01MDM

unread,
Feb 25, 2011, 10:35:57 AM2/25/11
to
And for wmic command it seems to be shorter:

wmic process where name="cmd.exe" get processid

0 new messages