Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for readreg.bat

(view source code of readreg.bat as plain text)

  1. @ECHO OFF
  2. :: Check Windows version
  3. IF NOT "%OS%"=="Windows_NT" GOTO Syntax
  4. :: Check command line arguments:
  5. :: 2 required plus 1 optional, "?" or "/?" for help screen
  6. ECHO.%* | FIND "?" >NUL
  7. IF NOT ERRORLEVEL 1 GOTO Syntax
  8. IF     "%~2"=="" GOTO Syntax
  9. IF NOT "%~4"=="" GOTO Syntax
  10. IF NOT "%~3"=="" IF /I NOT "%~3"=="/K" GOTO Syntax
  11. :: Check if either FINDSTR or EGREP is available
  12. SET FINDSTR=
  13. FINDSTR.EXE /? >NUL 2>&1
  14. IF ERRORLEVEL 1 (
  15. 	EGREP.EXE --help >NUL 2>&1
  16. 	IF ERRORLEVEL 1 GOTO Syntax
  17. 	SET FINDSTR=EGREP
  18. )
  19.  
  20. :: Keep variables local
  21. SETLOCAL
  22.  
  23. :: Read variables from command line
  24. SET RegSection=%~1
  25. SET RegKey=%~2
  26. SET RegVal=
  27. IF /I "%~3"=="/K" (SET VarName=_%RegKey%) ELSE (SET VarName=RegVal)
  28.  
  29. :: Delete temporary file if it already exists
  30. IF EXIST "%Temp%.\_readreg.dat" DEL "%Temp%.\_readreg.dat"
  31.  
  32. :: Store content of registry section in temporary file
  33. START /WAIT REGEDIT.EXE /E "%Temp%.\_readreg.dat" "%~1"
  34.  
  35. :: Abort with error message if the section wasn't found in the registry
  36. IF NOT EXIST "%Temp%.\_readreg.dat" (
  37. 	ECHO.
  38. 	ECHO ERROR:  [%RegSection%]  not found
  39. ) 1>&2
  40. IF NOT EXIST "%Temp%.\_readreg.dat" (
  41. 	ENDLOCAL
  42. 	GOTO:EOF
  43. )
  44.  
  45. :: Use either FINDSTR or EGREP to search requested value in temporary file.
  46. :: Note: TYPE is used here because the temporary file may be in Unicode.
  47. IF "%FINDSTR%"=="EGREP" (
  48. 	FOR /F "tokens=1* delims==" %%A IN ('TYPE "%Temp%.\_readreg.dat" 2^>NUL ^| EGREP.EXE -i "^^\"?%~2\"?="') DO (
  49. 		SET RegKey=%%~A
  50. 		SET RegVal=%%~B
  51. 	)
  52. ) ELSE (
  53. 	FOR /F "tokens=1* delims==" %%A IN ('TYPE "%Temp%.\_readreg.dat" 2^>NUL ^| FINDSTR.EXE /I /B /R /C:"\"%~2\"="') DO (
  54. 		SET RegKey=%%~A
  55. 		SET RegVal=%%~B
  56. 	)
  57. )
  58.  
  59. :: Format and display the result
  60. IF DEFINED RegVal (SET RegVal=%RegVal:\\=\%) ELSE (SET RegVal= - Undefined -)
  61. ECHO.
  62. ECHO [%RegSection%]
  63. ECHO %RegKey%=%RegVal%
  64.  
  65. :: Delete temporary file
  66. :: IF EXIST "%Temp%.\_readreg.dat" DEL "%Temp%.\_readreg.dat"
  67.  
  68. :: Pass result in variable to calling environment and end program
  69. ENDLOCAL & SET %VarName%=%RegVal%
  70. GOTO:EOF
  71.  
  72.  
  73. :Syntax
  74. ECHO.
  75. ECHO ReadReg.bat,  Version 2.10 for Windows NT 4 and later
  76. ECHO Read a value from the registry and store it in an environment variable
  77. ECHO.
  78. ECHO Usage:    READREG  "section"  "key"  [ /K ]
  79. ECHO.
  80. ECHO Where:             "section"  is the section name, without brackets
  81. ECHO                    "key"      is the key whose value must be read
  82. ECHO                    /K         uses "_key" for environment value name
  83. ECHO.
  84.  
  85. :: In case we use NT 4 we have multiple notes, otherwise one single note
  86. VER | FIND "Windows NT" >NUL
  87. IF     ERRORLEVEL 1 ECHO Note:     The result is stored in an environment variable %%RegVal%%,
  88. VER | FIND "Windows NT" >NUL
  89. IF NOT ERRORLEVEL 1 ECHO Notes:    The result is stored in an environment variable %%RegVal%%,
  90. ECHO           unless /K switch is used.
  91.  
  92. :: Message for NT 4 about FINDSTR and EGREP availability
  93. VER | FIND "Windows NT" >NUL
  94. IF ERRORLEVEL 1 GOTO Example
  95. ECHO           This batch file uses either FINDSTR or EGREP to search for the
  96. ECHO           the requested value.
  97. ECHO           FINDSTR is part of the Windows NT 4 Resource Kit, available at
  98. ECHO           http://www.microsoft.com/ntserver/nts/downloads/recommended/ntkit/
  99. ECHO           EGREP is available at http://unxutils.sourceforge.net/ and
  100. ECHO           several other sites.
  101.  
  102. :Example
  103. ECHO.
  104. ECHO Example:  READREG  "HKEY_CURRENT_USER\Environment"  "path"  /K
  105. ECHO           should store the user part of the PATH variable in _PATH variable
  106. ECHO.
  107. ECHO Written by Rob van der Woude
  108. ECHO http://www.robvanderwoude.com
  109.  

page last modified: 2024-04-16; loaded in 0.0107 seconds