4
I am writing a batch script to check if a registry key value exists and I am having some issues.
When I directly specify a key to look for, the %ERRORLEVEL%
updates appropriately. The example below echos the value 1 as expected.
REG QUERY HKLM /v NONEXISTENT_KEY
ECHO %ERRORLEVEL%
However I am checking the existence of a bunch of keys in a file so I am looping over it with FOR
. The following echos 0 for some reason that I do not understand.
FOR /F "tokens=1-2 delims=," %%A IN (myFile.txt) DO (
REG QUERY "%%A" /v "%%B"
ECHO %ERRORLEVEL%
Note: the structure of the file I am looping over is demonstrated in the following example:
HKEY_LOCAL_MACHINE\PATH\TO\KEY,SOME VALUE
Have you tried using Setlocal EnableDelayedExpansion together with
– DavidPostill – 2017-08-09T15:05:20.463ECHO !ERRORLEVEL!
?Just tried this, the error level is still returned as 0 despite the key value not existing. – Alex – 2017-08-09T15:10:54.740
EDIT Sorry I forgot to change my '%'s to '!'s. It works after doing that. – Alex – 2017-08-09T15:16:14.973
Yes, just confirmed by testing. Writing an answer. – DavidPostill – 2017-08-09T15:38:44.253