Batch script ignores %ERRORLEVEL% or using previously set one

54 views Asked by At

I wrote a batch script to find out if a specific process runs on a computer:

   

    for /f %%a in (computers.txt) do (
        PsList.exe \\%%a -e "process" > result.txt
        Find /i "found" < result.txt
        IF "%ERRORLEVEL%" == "0" echo %%a >> Computers_without_process.csv
)

I did Find /i "found" < result.txt because if the process is not found it returns: "process ... was not found on computername"

if the process is found it returns it's information. and the string "found" isnt there.

I tried just about anything.

Thanks for your help!

1

There are 1 answers

0
npocmaka On BEST ANSWER

You need delayed expansion

setlocal enableDelayedExpansion    
for /f %%a in (computers.txt) do (
        PsList.exe \\%%a -e "process" > result.txt
        Find /i "found" < result.txt
        IF "!ERRORLEVEL!" == "0" echo %%a >> Computers_without_process.csv
)

or you can use conditional execution (and pipes):

setlocal enableDelayedExpansion    
for /f %%a in (computers.txt) do (
        PsList.exe \\%%a -e "process" | Find /i "found" >nul 2>nul && (
           echo %%a >> Computers_without_process.csv
         )
)