Batch code snippet to query, then update dynamic values in registry

By | September 21, 2016
Share
FOR /F "tokens=1,2*" %%a in ('reg.exe query "HKLM\SYSTEM\CurrentControlSet\Control\Class"') DO call :FindAndReplace %%a

goto cleanup
:FindAndReplace
reg.exe query "%1\0000" /f "COM3" /d > nul
if "%ErrorLevel%"=="0" reg.exe add "%1\0000" /v "AttachedTo" /t REG_SZ /d "COM1" /f
exit /b

:cleanup
echo "This is a test"
pause

The snippet pulls sub-keys from the “HKLM\SYSTEM\CurrentControlSet\Control\Class” and passing each to a call to a “:FindAndReplace” section with the full key as an argument.  The reason I call a section from the Do, is because %ErrorLevel% was not populating correctly inside of a for loop.

/BG

 

 

Leave a Reply

Your email address will not be published.

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.