Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for readini.bat

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

  1. @ECHO OFF
  2. :: Check Windows version
  3. IF NOT "%OS%"=="Windows_NT" GOTO Syntax
  4. :: Check command line
  5. ECHO.%1 | FIND "?" >NUL
  6. IF NOT ERRORLEVEL 1 GOTO Syntax
  7. IF [%3]==[] GOTO Syntax
  8. :: Check if INI file exists
  9. IF NOT EXIST "%~f1" GOTO Syntax
  10.  
  11. :: Keep variables local and enable delayed variable expansion
  12. SETLOCAL ENABLEDELAYEDEXPANSION
  13.  
  14. :: Read variables from command line
  15. SET INIFile="%~f1"
  16. SET INISection=%~2
  17. SET INIKey=%~3
  18. SET INIValue=
  19.  
  20. :: Reset temporary variables
  21. SET SectOK=0
  22. SET SectFound=0
  23. SET KeyFound=0
  24.  
  25. :: Search the INI file line by line
  26. FOR /F "tokens=* delims=" %%A IN ('TYPE %INIFile%') DO CALL :ParseINI "%%A"
  27.  
  28. :: Display the result
  29. ECHO.
  30. :: EXIT /B return codes (errorlevels) added by Van Woods,
  31. :: US Army Corps of Engineers, Seattle District
  32. IF NOT %SectFound%==1 (
  33.     ECHO INI section not found
  34.     EXIT /B 1
  35. ) ELSE (
  36.     IF NOT %KeyFound%==1 (
  37.         ECHO INI key not found
  38.         EXIT /B 2
  39.     ) ELSE (
  40.         IF DEFINED INIValue (
  41.             ECHO.%INIFile%
  42.             ECHO [%INISection%]
  43.             ECHO %INIKey%=%INIValue%
  44.         ) ELSE (
  45.             ECHO Value not defined
  46.             EXIT /B 3
  47.         )
  48.     )
  49. )
  50.  
  51. :: Corrected environment variable by Van Woods,
  52. :: US Army Corps of Engineers, Seattle District
  53. ENDLOCAL & SET %INIKey%=%INIValue%
  54. GOTO:EOF
  55.  
  56.  
  57. :ParseINI
  58. :: Skip rest of file after key has been found;
  59. :: speed improvement by Jeroen Verschuuren
  60. IF "%SectFound%"=="1" IF "%KeyFound%"=="1" GOTO:EOF
  61. :: Store quoted line in variable
  62. SET Line="%~1"
  63.  
  64. :: Check if this line is the required section heading
  65. ECHO.%Line%| FIND /I "[%INISection%]" >NUL
  66. IF NOT ERRORLEVEL 1 (
  67.     SET SectOK=1
  68.     SET SectFound=1
  69.     GOTO:EOF
  70. )
  71. :: Check if this line is a different section header
  72. IF "%Line:~1,1%"=="[" SET SectOK=0
  73. IF %SectOK%==0 GOTO:EOF
  74.  
  75. :: Parse any "key=value" line
  76. FOR /F "tokens=1* delims==" %%a IN ('ECHO.%Line%') DO (
  77.     SET Key=%%a^"
  78.     SET Value=^"%%b
  79. )
  80.  
  81. :: Strip quotes, tabs, and surrounding spaces from key and value
  82. :: Modifications added by Van Woods,
  83. :: US Army Corps of Engineers, Seattle District
  84. SET Value=%Value:"=%
  85. :: Remove quotes
  86. SET Key=%Key:"=%
  87. :: Remove tabs
  88. SET Value=%Value:	=%
  89. SET Key=%Key:	=%
  90. :: Remove leading spaces
  91. FOR /F "tokens=* delims= " %%A IN ("%Key%")   DO SET Key=%%A
  92. FOR /F "tokens=* delims= " %%A IN ("%Value%") DO SET Value=%%A
  93. :: Remove trailing spaces
  94. FOR /L %%A in (1,1,32) do if "!Key:~-1!"==" " set Key=!Key:~0,-1!
  95. FOR /L %%A in (1,1,32) do if "!Value:~-1!"==" " set Value=!Value:~0,-1!
  96.  
  97. :: Now check if the key matches the required key
  98. IF /I "%Key%"=="%INIKey%" (
  99.     SET INIValue=%Value%
  100.     SET KeyFound=1
  101. )
  102.  
  103. :: End of ParseINI subroutine
  104. GOTO:EOF
  105.  
  106.  
  107.  
  108. :Syntax
  109. ECHO.
  110. ECHO ReadINI.bat,  Version 1.30 for Windows NT 4 / 2000 / XP / Server 2003
  111. ECHO Read a value from the specified INI file
  112. ECHO.
  113. ECHO Usage:  READINI  "ini_file"  "section"  "key"
  114. ECHO Where:           "ini_file" is the file name of the INI file to be read
  115. ECHO                  "section"  is the section name, without the brackets
  116. ECHO                  "key"      is the key whose value must be read
  117. ECHO.
  118. ECHO Example: if MYPROG.INI looks like this:
  119. ECHO     [Section 1]
  120. ECHO     Key1=Value 1
  121. ECHO     Key2=Value 2
  122. ECHO     [Section 2]
  123. ECHO     Key2=Value 4
  124. ECHO Then the command:  READINI "MYPROG.INI" "section 1" "key2"
  125. ECHO will return:       Key2=Value 2
  126. ECHO.
  127. ECHO Written by Rob van der Woude
  128. ECHO http://www.robvanderwoude.com
  129. ECHO Speed improvement by Jeroen Verschuuren
  130. ECHO Return codes, whitespace removal and corrected environment variable
  131. ECHO by Van Woods, US Army Corps of Engineers, Seattle District
  132.  

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