Page 1 of 1

WriteINI function.

Posted: 15 Jul 2024 11:07
by Gustaaf
I have created a dos function called :WriteIni.

I thought i would share for comment and improvement. Perhaps it can make its way to the dostips functions list?

Code: Select all

:: Function to write or update an entry in an INI file
:WRITEINI
REM ======================================================================
REM Function: WRITEINI
REM Author: G.A. von Pickartz, built by ChatGPT 4.0
REM Version: 1.27.0715.2024
REM
REM Description:
REM This function writes or updates an entry in an INI file. It takes the
REM INI file, section, key, and value as arguments and ensures the INI file
REM is updated accordingly. If the section or key is not found, it will add
REM the new entry.
REM
REM Version History:
REM 1.0 - Initial version
REM 1.10 - Added handling for missing sections
REM 1.20 - Improved key-value pair extraction
REM 1.25 - Enhanced section and key search logic
REM 1.27.0715.2024 - Updated to align with READINI function argument logic
REM
REM Change Log:
REM - Updated function to accept INI file as the first parameter
REM - Improved handling of section and key updates
REM - Added timestamp and user details when adding new sections
REM
REM Caution:
REM This function will replace the original INI file with the updated content.
REM Ensure you have a backup of the INI file before running this function.
REM
REM EXAMPLES:
REM 1. To call this function and update the INI file with a new entry, use:
REM CALL :WRITEINI "PACKAGE.INI" "CREDITS" "Developer" "Gustaaf"
REM
REM 2. To create a new entry:
REM CALL :WRITEINI "PACKAGE.INI" "NewSection" "NewKey" "ChatGPT 4.0"

REM ======================================================================

REM Arguments: INI file, Section, Key, Value
SET "inifile=%~1"
SET "Section=%~2"
SET "Key=%~3"
SET "Value=%~4"

REM Temporary file to store the updated INI content
SET "tempfile=%temp%\tempfile.ini"

REM Flags to track if the section and key were found
SET "Sectionfound=false"
SET "Keyupdated=false"

REM Create the temporary file by processing the INI file
(
    REM For loop ignores comments and empty lines by default. EOL= is mandatory to carry comments. Empty lines is artificially handled below.
    FOR /F "usebackq eol= delims=" %%A IN ("%inifile%") DO (
        SET "line=%%A"

        REM Add an empty line before each section for readability as FOR loop strips these.
        IF "!line:~0,1!"=="[" (
            echo.
        )

        REM Check if the line is the target section
        IF /i "!line!"=="[%Section%]" (
            SET "Sectionfound=true"
            SET "updated=true"
            echo !line!

        REM If within the target section, update or add the key-value pair
        ) ELSE IF "!Sectionfound!"=="true" (
            IF /i "!line:~0,1!"=="[" (
                IF "!Keyupdated!"=="false" (
                    echo %Key%=%Value%
                    SET "Keyupdated=true"
                )
                SET "Sectionfound=false"
                echo !line!
            ) ELSE (
                REM Preserve comments in the target section
                IF "!line:~0,1!"==";" (
                    echo !line!
                ) ELSE (
                    REM Extract and compare the key from the line
                    FOR /F "tokens=1,* delims==" %%B IN ("!line!") DO (
                        IF /i "%%B"=="%Key%" (
                            echo %Key%=%Value%
                            SET "Keyupdated=true"
                        ) ELSE (
                            echo !line!
                        )
                    )
                )
            )

        REM Echo all lines outside the target section
        ) ELSE (
            echo !line!
        )
    )

    REM If section was found but key was not updated, ensure key is added within the section
    IF "!Sectionfound!"=="true" IF "!Keyupdated!"=="false" (
        echo %Key%=%Value%
    )

    REM If section was not found at all, add it at the end with a timestamp and user details
    IF NOT DEFINED updated (
        echo.
        echo ; WriteIni added new section on %date% at %time% by %username%.
        echo [%Section%]
        echo ; Default value = %Key%=%Value%
        echo %Key%=%Value%
    )
) > "%tempfile%"

REM Replace the original INI file with the updated content
move /y "%tempfile%" "%inifile%" 1>nul

GOTO :EOF