Batch file: check for (non-)existence of registry key
Posted by jpluimers on 2021/01/05
Small batch file that only deletes a registry key if it exists:
:DeleteKeyIfItExists reg query %1 >nul 2>&1 if %errorlevel% equ 0 reg delete %1 /f goto :eof
It is based on:
- redirecting both
stderrandstdouttonul(the>nul 2>&1bit) - checking
reg querywith the appropriateerrorlevelvalue for equality (equoperator) for0(existence); you can also use1for non-existence.
Based on:
- [WayBack] Checking if a key is present on the windows registry through batch file – Super User
- [WayBack] batch file – Check if registry key + value exists and if so, log it – Stack Overflow
- [WayBack] [SOLVED] Batch File. If Reg Key exist goto, if not exist goto – IT Programming – Spiceworks
- [WayBack] command line – Redirect Windows cmd stdout and stderr to a single file – Stack Overflow
You want:
dir > a.txt 2>&1The syntax
2>&1will redirect2(stderr) to1(stdout).You can also hide messages by redirecting to
NUL, more explanation and examples on MSDN.
–jeroen






Leave a comment