Question

I have this script and the script works fine. Except one part. I am trying to return the error level after each query I do to determine my error level. I know I have syntax that is wrong please help.

@echo off
    setlocal enabledelayedexpansion
set "ports=HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Ports"

rem get only the first token of each line of "reg query"
for /f %%I in (
    'reg query "%ports%"'
) do (
IF !errorlevel!==0  echo(%%I | findstr /i "c:\\convertdoc\\output\\silentprinttemp\\.*\.ps" >NUL && (
        rem (if "findstr" didn't exit with an abnormal error code)
        echo Deleting item %%I
        ECHO [PW-MOD] reg delete "%ports%" /v "%%I" /f
    )
)

[edited by Dk 68 61 63 6b in response to comments] [re-edited PW to join broken line and disable potentially damaging instruction 'reg delete']

Was it helpful?

Solution

Look up delayed expansion in help set. It explains your issue exactly.

To solve it use

setlocal enabledelayedexpansion

and then !errorlevel! instead of %errorlevel%.

Another option would be to use

if not errorlevel 1

(with slightly different semantics that are likely not relevant in your case).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top