Reputation: 718
I am getting 3 Missing operator.
lines as output from the following batch script
SET w3wp2ID=0
SET w3wp1ID=0
SET w3wpID=0
SET ValuesAreSet=FALSE
for /f "tokens=2,3,4 delims=," %%A in ('typeperf -sc 1 -y "\Process(w3wp*)\ID Process" ^| find /V "\\" ^| find /V "please wait..."') do (
if "%ValuesAreSet%"=="FALSE" (
SET /A w3wp2ID=%%~A+0
SET /A w3wp1ID=%%~B+0
SET /A w3wpID=%%~C+0
)
SET ValuesAreSet=TRUE
)
At first I thought the output was coming from the 3 Set
statements in the forloop, but upon investigation I no longer think this is the issue. Leading me to believe it has something to do with the for
statement itself, but I cannot figure out what exactly is wrong.
I need to suppress these warnings since the output of this script will be logged.
Any help is appreciated, thanks, T.
EDIT: I should add that the 3 variables are getting the correct values set.
Upvotes: 0
Views: 786
Reputation: 34939
Although for this specific case the method explained by Compo is probably the best solution for the problem at hand, I still want to contribute another approach: letting the set /A
command do the variable expansion, because this does not rise errors if non-numeric values are stored; instead, everything up to the first non-numeric character is converted to a signed 32-bit integer, omitting leading white-spaces, regarding signs and coercing it to the 32-bit bounds. For example:
set "VAR= -1.5 mV" & rem // (the first non-numeric char. is `.`, so the integer is `-1`)
set /A "VAR=VAR" & rem // (`VAR` is read by `set /A`, because no `%%` are surrounded)
echo %VAR% (-1 expected, no error message arises)
set /A "VAR=%VAR%" & rem /* (`VAR` becomes set to `-1` here, but an error message appears,
rem because `set /A` actually receives the string ` -1.5 mV`) */
echo %VAR% (-1 expected, an error message arises)
Here this functionality is implemented into your code; the values are first stored unaltered with a standard set
command, then converted to integers by a set /A
command (note that VAR=VAR
has been replaced by VAR+=0
, just not to have to state each variable name twice per statement):
set "ValuesAreSet="
for /F "tokens=2,3,4 delims=," %%A in ('
typeperf -sc 1 -y "\Process(w3wp*)\ID Process" ^
^| find /V "\\" ^| find /V "please wait..."
') do if not defined ValuesAreSet (
set "w3wp2ID=%%~A" & set /A "w3wp2ID+=0"
set "w3wp1ID=%%~B" & set /A "w3wp1ID+=0"
set "w3wpID=%%~C" & set /A "w3wpID+=0"
set "ValuesAreSet=#"
)
In addition, I changed the flag-style variable ValuesAreSet
to indicate the FALSE
state by being empty and the TRUE
state by being non-empty, so if defined ValuesAreSet
can be used.
Upvotes: 1
Reputation: 38643
If you only want the values before the decimal point just use the %~nI
expansion method which would treat the numbers after the decimal point as an extension thus removing them. (technically rounding it down).
SET "ValuesAreSet=FALSE"
For /F "Tokens=2-4 Delims=," %%A In (
'typeperf -sc 1 -y "\Process(w3wp*)\ID Process" ^| find /V "\\" ^| find /V "please wait..."'
) Do If "%ValuesAreSet%"=="FALSE" (SET "w3wp2ID=%%~nA"
SET "w3wp1ID=%%~nB"
SET "w3wpID=%%~nC"
SET "ValuesAreSet=TRUE"
)
Upvotes: 4
Reputation:
I think if suppressing the error messages is ok for you then stay with it, parsing the values to split at the dot is IMO overkill, but nevertheless:
SET w3wp2ID=0
SET w3wp1ID=0
SET w3wpID=0
SET ValuesAreSet=FALSE
for /f "tokens=2,3,4 delims=," %%A in ('typeperf -sc 1 -y "\Process(w3wp*)\ID Process" ^| find /V "\\" ^| find /V "please wait..."') do (
if "%ValuesAreSet%"=="FALSE" (
for /f "delims=." %%x in ("%%~A") do SET /A w3wp2ID+=%%x
for /f "delims=." %%x in ("%%~B") do SET /A w3wp1ID+=%%x
for /f "delims=." %%x in ("%%~C") do SET /A w3wpID+=%%x
)
SET ValuesAreSet=TRUE
)
Upvotes: 1