ReadINI function
Posted: 15 Jul 2024 11:10
This is seperate post to my WriteIni function. The ReadIni function is in part inspired by another thread.
Code: Select all
:: Function to read an entry from an INI file
:READINI
REM ======================================================================
REM Function: READINI
REM Author: G.A. von Pickartz, built with ChatGPT 4.0
REM Version: 1.07.0715.2024
REM
REM Description:
REM This function reads an entry from an INI file. It takes the INI file,
REM section, and key as arguments and outputs the key-value pair or all
REM key-value pairs within the specified section. If no section and key are
REM specified, it outputs the entire INI file values.
REM
REM Version History:
REM 1.0 - Initial version
REM 1.02 - Added section handling
REM 1.03 - Improved key-value extraction
REM 1.05 - Enhanced logic for specific key search
REM 1.07.0715.2024 - Updated to capture entire INI file if no section/key provided
REM
REM Change Log:
REM - Added logic to capture entire INI file values if no section/key provided
REM - Improved section and key search handling
REM
REM Credits:
REM Inspired by solutions from Stack Overflow:
REM https://stackoverflow.com/questions/2866117/windows-batch-script-to-read-an-ini-file
REM
REM EXAMPLES:
REM 1. To capture the entire INI file values:
REM CALL :READINI "PACKAGE.INI"
REM
REM 2. To find a section and display all key-value pairs within it:
REM CALL :READINI "PACKAGE.INI" "CREDITS"
REM
REM 3. To find a specific key-value pair within a section:
REM CALL :READINI "PACKAGE.INI" "CREDITS" "LogPath"
REM
REM 4. To test with non-existing section and key:
REM CALL :READINI "PACKAGE.INI" "NotASection"
REM CALL :READINI "PACKAGE.INI" "CREDITS" "NotAKeyinCREDITS"
REM ======================================================================
REM Arguments: INI file, Section (optional), Key (optional)
SET "inifile=%~1"
SET "Section=%~2"
SET "Key=%~3"
REM Flags to track if the section and key were found
SET "Sectionfound=false"
SET "KeyFound=false"
REM Process the INI file
FOR /F "usebackq delims=" %%A IN ("%inifile%") DO (
SET "line=%%A"
REM Check if no section and key are specified
IF "%Section%"=="" (
REM Extract and display all key-value pairs in the INI file
FOR /F "tokens=1,* eol=; delims==" %%B IN ("!line!") DO (
IF "!line:~0,1!"=="[" (
REM ECHO %%B
) ELSE (
REM ECHO %%B=%%C
SET %%B=%%C
)
)
) ELSE (
REM Check if the line is the target section
IF /i "!line!"=="[%Section%]" (
SET "Sectionfound=true"
) ELSE IF "!Sectionfound!"=="true" (
IF /i "!line:~0,1!"=="[" (
REM Abort if another section was found and the key was not found
IF "%Key%" NEQ "" IF "%KeyFound%"=="false" (
ECHO Key not found: %Key%
)
GOTO :EOF
) ELSE (
REM Extract and compare the key from the line
FOR /F "tokens=1,* delims==" %%B IN ("!line!") DO (
IF "%Key%"=="" (
SET "KeyFound=true"
SET "%%B=%%C"
) ELSE IF /i "%%B"=="%Key%" (
SET "KeyFound=true"
SET "%%B=%%C"
GOTO :EOF
)
)
)
)
)
)
REM If the section or key was not found
IF "%Section%" NEQ "" IF "%Sectionfound%"=="false" (
ECHO Section not found: [%Section%]
) ELSE IF "%Key%" NEQ "" IF "%KeyFound%"=="false" (
ECHO The Key %Key% was not found.
)
ENDLOCAL
GOTO :EOF