[Batch] Save and read vars to config on disk

Discussion in 'Scripting' started by hearywarlot, Aug 15, 2015.

  1. hearywarlot

    hearywarlot MDL Member

    Jul 31, 2015
    112
    153
    10
    #1 hearywarlot, Aug 15, 2015
    Last edited by a moderator: Apr 20, 2017
    This is a batch script which can be used to save or read variables to/from a config file.
    This is useful incase you need to save userinput on a removeable disk or need to save environmental vars but can not use setx, for example XP.

    Usage:
    You can read variables by calling the script with parameters
    The syntax would be: confread.cmd [LABEL] [VAR] [FILE]

    Example reading variables:
    Code:
    SET "FILE=%~dp0\batchconfig.conf"
    
    CALL confread username USR "%FILE%"
    CALL confread password PSW "%FILE%"
    CALL confread additional-data ADD "%FILE%"
    
    IF defined USR (
        ECHO Username=!USR!
    )
    IF defined PSW (
        ECHO Password=!PSW!
    )
    IF defined ADD (
        ECHO Additional data=!ADD!
    )
    Saving is the same beast.
    The Syntax would be: confsave.cmd [LABEL] [VAR] [FILE]

    Example saving variables:
    Code:
    SET "FILE=%~dp0\batchconfig.conf"
    
    SET /p "USR=Username:"
    SET /p "PSW=Password:"
    SET /p "ADD=Additional data:"
    
    CALL confsave username USR "%FILE%"
    CALL confsave password PSW "%FILE%"
    CALL confsave additional-data ADD "%FILE%"

    Notes:
    • You can not save the character '!' because 'ENABLEDELAYEDEXPANSION' will cause the script to use that character for vars.
    • Using the '=' Character breaks the config file. The vars can still be read and saved normally, but you will get errors.
    • If you noticed, you should give the variables names themself instead of the contents.
    • This script can save any data, but is still has adhere to the limits of CMD/Batch.

    Code:
    Filename: confread.cmd
    Code:
    @ECHO OFF
    :CONFR
    SETLOCAL ENABLEDELAYEDEXPANSION
    IF not "%~3"=="" (
        SET "FILE=%~3"
    )
    IF exist "!FILE!" (
        FOR %%A in ("!FILE!") do (
            IF not %%~zF equ 0 (
                FOR /F "delims=" %%B in ('findstr /p "%~1" "!FILE!"') do (
                    SET "LNA=%%B"
                    SET "LNB=!LNA:%~1=!"
                    SET "LNC=!LNB:~3!"
                )
            )
        )
    )
    ( ENDLOCAL
        IF not "%~2"=="" (
            SET "%~2=%LNC%"
        )
    )
    EXIT /B 0
    Filename: confsave.cmd
    Code:
    @ECHO OFF
    :CONFS
    SETLOCAL ENABLEDELAYEDEXPANSION
    IF not "%~3"=="" (
        SET "FILE=%~3"
    )
    IF exist "!FILE!" (
        (    FOR /F "delims= usebackq" %%A in ("!FILE!") do (
                SET "LNA=%%A"
                SET "LNB=!LNA:* =!"
                IF not "!LNB!"=="= " (
                    SET "LNC=!LNB:* =!"
                    CALL SET "LND=%%LNA:!LNC!=%%"
                    IF "!LND!"=="%~1 = " (
                        SET "LNS=1"
                        IF not "!%~2!"=="" (
                            ECHO(%~1 = !%~2!
                        )
                    ) else (
                        ECHO(%%A
                    )
                )
            )
            IF not !LNS! equ 1 (
                IF not "!%~2!"=="" (
                    ECHO(%~1 = !%~2!
                )
            )
        ) > "!FILE!.new"
    ) else (
        IF not "!%~2!"=="" (
            ECHO(%~1 = !%~2!
        ) > "!FILE!.new"
    )
    MOVE /y "!FILE!.new" "!FILE!" >NUL 2>&1
    ENDLOCAL
    EXIT /B 0

    I hope anyone might find it useful, i created it when i was bored :>
    If possible, you might be better off using a VBS or better solution to save vars to disk.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...