|Hey guys, this ones a little specific, but I wrote up a script yesterday I seem to be having trouble with. I'm reasonably well experienced with batch and believe I've identified the problem but am unsure of a reasonable workaround.|
The aim of the script is essentially:
- Read in a list (file) of registry paths, keys and their values (comma delimited)
- Query the keys in the list for their values and, if it exists, write this information to a custom .reg file containing the keys and their current values (saving the current values in a correctly formatted .reg to re-import later if desired)
- If a key in the list does NOT currently exist, take note of this key by outputting it's path and key name to another file that can be read from later (this is to keep track of NEW keys that my script may add so that if I wish to revert to an original state, I know which new keys were added so I may remove them). THIS IS WHERE MY PROBLEM LIES.
- If desired, there are options for a user to change the values of the listed keys to the values contained within the list file (third comma delimited token), or add the new key with this value if it doesnt exist.
- Can delete any new keys added.
Right now, everything works as desired EXCEPT for my ability to accurately determine the ERRORLEVEL after REG QUERY is run in a FOR loop. It should be simply -- if the key is there, write its value to a custom .reg file. This works fine. If the key does NOT exist, add its path and name to another file ("new_keys.txt") so I can keep track of newly added keys for later removal.
Ive tried to explain this as best I can -- Here are the two files I'm working with -- the batch script and its dependent list file:
REM zRegMod.bat - A simple utility to aid in the backing-up
REM and modification of select registry keys/values per
REM Ziftens needs for customer demos.
IF /I "%1"=="" (
ECHO zRegMod - A simple utility to aid in the backing-up and
ECHO modification of select registry keys/values per our needs
ECHO for setting up optimized environments. See README.TXT
ECHO for additional information.
ECHO Usage: %~nx0 [-b/-m/-d]
ECHO -b : Backup keys and values defined in %KEYFILE%.
ECHO These keys/values can be restored to their
ECHO backed-up values ^(or removed entirely if it
ECHO is a newly created key^) by executing "%REGFILE%"
ECHO at any time.
ECHO -m : Modifies the value of keys defined in %KEYFILE% to
ECHO reflect the new value defined at the end of each line.
ECHO -d : Deletes any keys/values defined within %KEYFILE% that
ECHO were NOT present on the system before zRegMod was used
ECHO to add them.
ECHO NOTE: To generate a list of keys newly added by
ECHO zRegMod ^(for use with `-d'^), you must first use `-b'.
ECHO This list can be found in %NEW_KEYS%.
IF /I "%1"=="-b" (
ECHO [+] Backup up current registry values to %REGFILE%...
ECHO Windows Registry Editor Version 5.00>%REGFILE%
FOR /F "usebackq tokens=1-2 delims=, eol=;" %%A IN (%KEYFILE%) DO (
FOR /F "skip=4 tokens=1-3 delims= " %%C IN ('REG QUERY "%%A" /v %%B') DO (
IF ERRORLEVEL 0 (
IF /I "%%D"=="REG_DWORD" (
) ELSE (
IF NOT ERRORLEVEL 0 ECHO %%A,%%B>>%NEW_KEYS%
ECHO [-] Old registry have been saved to %REGFILE%
IF /I "%1"=="-m" (
SET /P APPLY_TWEAKS=[+] Apply registry changes now? (y/n):
IF /I "%APPLY_TWEAKS%"=="n" GOTO :EOF
FOR /F "usebackq tokens=1-3 delims=, eol=;" %%A IN (%KEYFILE%) DO (
ECHO [+] Setting %%B=%%C...
REG ADD "%%A" /v %%B /d %%C /f 1>NUL
IF /I "%1"=="-d" (
FOR /F "usebackq tokens=1-2 delims=," %%A IN (%NEW_KEYS%) DO (
ECHO [+] Removing newly added key `%%B'...
REG DELETE "%%A" /v %%B
The line that I'm having issues with is:
IF NOT ERRORLEVEL 0 ECHO %%A,%%B>>%NEW_KEYS%
The list file:
I've tried several different things trying to catch the correct errorlevel at the right time. I've used ERRORLEVEL, %ERRORLEVEL%, !ERRORLEVEL!, moved its position in the loop around, even using double-pipes within the actual REG QUERY to pipe the current ERRORLEVEL into a variable or file that can be read from to get the right errorlevel in real time...
I know Windows is tricky in the way it processes Batch code and has its own less than ideal way of handling things. I assumed this probably had something to do with the ERRORLEVEL being handled by REG.EXE itself and maybe it was still returning 0 in MY code because the code itself was executing without error?...or...something... I don't know.
I just need this thing to spit the keys out to a new file if reg query fails to find it. I hope I haven't been too lengthy with this question... ANY HELP IS APPRECIATED!!!! Thanks :)