(view source code of readini.bat as plain text)
@ECHO OFF
:: Check Windows versionIF NOT "%OS%"=="Windows_NT" GOTO Syntax
:: Check command lineECHO.%1 | FIND "?" >NUL
IF NOT ERRORLEVEL 1 GOTO Syntax
IF [%3]==[] GOTO Syntax
:: Check if INI file existsIF NOT EXIST "%~f1" GOTO Syntax
:: Keep variables local and enable delayed variable expansionSETLOCAL ENABLEDELAYEDEXPANSION
:: Read variables from command lineSET INIFile="%~f1"
SET INISection=%~2
SET INIKey=%~3
SET INIValue=
:: Reset temporary variablesSET SectOK=0
SET SectFound=0
SET KeyFound=0
:: Search the INI file line by lineFOR /F "tokens=* delims=" %%A IN ('TYPE %INIFile%') DO CALL :ParseINI "%%A"
:: Display the resultECHO.
:: EXIT /B return codes (errorlevels) added by Van Woods,:: US Army Corps of Engineers, Seattle DistrictIF NOT %SectFound%==1 (
ECHO INI section not found
EXIT /B 1
) ELSE (
IF NOT %KeyFound%==1 (
ECHO INI key not found
EXIT /B 2
) ELSE (
IF DEFINED INIValue (
ECHO.%INIFile%
ECHO [%INISection%]
ECHO %INIKey%=%INIValue%
) ELSE (
ECHO Value not defined
EXIT /B 3
) )):: Corrected environment variable by Van Woods,:: US Army Corps of Engineers, Seattle DistrictENDLOCAL & SET %INIKey%=%INIValue%
GOTO:EOF
:ParseINI:: Skip rest of file after key has been found;:: speed improvement by Jeroen VerschuurenIF "%SectFound%"=="1" IF "%KeyFound%"=="1" GOTO:EOF
:: Store quoted line in variableSET Line="%~1"
:: Check if this line is the required section headingECHO.%Line%| FIND /I "[%INISection%]" >NUL
IF NOT ERRORLEVEL 1 (
SET SectOK=1
SET SectFound=1
GOTO:EOF
):: Check if this line is a different section headerIF "%Line:~1,1%"=="[" SET SectOK=0
IF %SectOK%==0 GOTO:EOF
:: Parse any "key=value" lineFOR /F "tokens=1* delims==" %%a IN ('ECHO.%Line%') DO (
SET Key=%%a^"
SET Value=^"%%b
):: Strip quotes, tabs, and surrounding spaces from key and value:: Modifications added by Van Woods,:: US Army Corps of Engineers, Seattle DistrictSET Value=%Value:"=%
:: Remove quotesSET Key=%Key:"=%
:: Remove tabsSET Value=%Value: =%
SET Key=%Key: =%
:: Remove leading spacesFOR /F "tokens=* delims= " %%A IN ("%Key%") DO SET Key=%%A
FOR /F "tokens=* delims= " %%A IN ("%Value%") DO SET Value=%%A
:: Remove trailing spacesFOR /L %%A in (1,1,32) do if "!Key:~-1!"==" " set Key=!Key:~0,-1!
FOR /L %%A in (1,1,32) do if "!Value:~-1!"==" " set Value=!Value:~0,-1!
:: Now check if the key matches the required keyIF /I "%Key%"=="%INIKey%" (
SET INIValue=%Value%
SET KeyFound=1
):: End of ParseINI subroutineGOTO:EOF
:SyntaxECHO.
ECHO ReadINI.bat, Version 1.30 for Windows NT 4 / 2000 / XP / Server 2003
ECHO Read a value from the specified INI file
ECHO.
ECHO Usage: READINI "ini_file" "section" "key"
ECHO Where: "ini_file" is the file name of the INI file to be read
ECHO "section" is the section name, without the brackets
ECHO "key" is the key whose value must be read
ECHO.
ECHO Example: if MYPROG.INI looks like this:
ECHO [Section 1]
ECHO Key1=Value 1
ECHO Key2=Value 2
ECHO [Section 2]
ECHO Key2=Value 4
ECHO Then the command: READINI "MYPROG.INI" "section 1" "key2"
ECHO will return: Key2=Value 2
ECHO.
ECHO Written by Rob van der Woude
ECHO http://www.robvanderwoude.com
ECHO Speed improvement by Jeroen Verschuuren
ECHO Return codes, whitespace removal and corrected environment variable
ECHO by Van Woods, US Army Corps of Engineers, Seattle District
page last modified: 2025-10-11; loaded in 0.0074 seconds