Skip to content

Instantly share code, notes, and snippets.

@djangofan
Created January 9, 2012 03:49
Show Gist options
  • Save djangofan/1580953 to your computer and use it in GitHub Desktop.
Save djangofan/1580953 to your computer and use it in GitHub Desktop.
DOS batch functions to handle editing property files.
@echo off &SETLOCAL ENABLEDELAYEDEXPANSION
:: Functions for updating property files
ECHO Args to script: %1 %2 %3
::---------------------------------------------------
:: Create test file if it doesn't exist
::---------------------------------------------------
IF NOT EXIST test.properties (
ECHO Key1=Value1>test.properties
ECHO #Key2=Value2>>test.properties
ECHO.>>test.properties
ECHO Key3=Value3>>test.properties
ECHO.>>test.properties
)
::---------------------------------------------------
:: Test Routine
::---------------------------------------------------
::CALL :PROPEDIT Key1 val1 test.properties
::CALL :PROPGET Key2 test.properties
::CALL :PROPADD Key3 ABCDEFGHIJ test.properties
::CALL :PROPADD Key5 Value5 test.properties
::pause
CALL :PROPEDIT # Key4 Value446 test.properties
::CALL :PROPGET Key1 test.properties
::pause
::---------------------------------------------------
:: Functions
::---------------------------------------------------
GOTO :END
:PROPGET PropKey File
IF "%~2"=="" (
ECHO PROPGET: Function requires exactly 2 args: PropKey File
pause
EXIT
) ELSE (
SET "_PROPKEY=%~1"
SET "_FILE=%~2"
)
ECHO PROPGET: Function args are '%_PROPKEY%' '%_FILE%'
COPY /Y "%_FILE%" "%_FILE%.bak">nul
FOR /F "tokens=1* delims==" %%A IN (%_FILE%) DO (
IF "%%A"=="%_PROPKEY%" SET "_RETURNVAL=%%B"
)
IF "%_RETURNVAL%"=="" SET "_RETURNVAL=NULL"
ECHO PROPGET: returned '%_RETURNVAL%'.
EXIT /B 0
:PROPADD PropKey PropVal File
IF "%~3"=="" (
ECHO PROPADD: Function requires exactly 3 args: PropKey PropVal File
PAUSE
EXIT
) ELSE (
SET "_PROPKEY=%~1"
SET "_PROPVAL=%~2"
SET "_FILE=%~3"
)
ECHO PROPEDIT: Function args are '%_PROPKEY%' '%_PROPVAL%' '%_FILE%'
FINDSTR.exe "%_PROPKEY%=" %_FILE%>nul
IF "%ERRORLEVEL%"=="0" (
ECHO Key '%_PROPKEY%' is already defined in '%_FILE%'. Cannot add.
) ELSE (
ECHO %_PROPKEY%=%_PROPVAL%>>%_FILE%
ECHO Added property '%_PROPKEY%=%_PROPVAL%' to file '%_FILE%'.
)
EXIT /B 0
:PROPEDIT [#] PropKey PropVal File
:: -------- NOTES -------------------------------------
:: -Leaves a blank line at bottom of edited files.
:: -If there are more than one match found for a property, then this
:: script will only operate on the last one it finds.
:: -Passing a first argument of "#" will disable the line while editing.
:: ----------------------------------------------------
IF "%~1"=="#" (
SET "_PREFIX=#"
SHIFT
)
IF NOT "%~4"=="" (
ECHO Too many arguments to PROPEDIT function.
EXIT /B 1
)
IF "%~3"=="" (
ECHO PROPEDIT: Function requires 3 args: [#] PropKey PropVal File
EXIT /B 1
) ELSE (
SET "_PROPKEY=%~1"
SET "_PROPVAL=%~2"
SET "_FILE=%~3"
)
MOVE /Y "%_FILE%" "%_FILE%.bak">nul
FOR /F "USEBACKQ tokens=*" %%A IN (`TYPE "%_FILE%.bak" ^|FINDSTR.exe /N /I "%_PROPKEY%="`) DO (
SET LINE=%%A
)
FOR /F "tokens=1,2* delims=:" %%S IN ("%LINE%") DO SET LINE=%%S
SET /A COUNT=1
FOR /F "USEBACKQ delims=" %%A IN (`TYPE "%_FILE%.bak" ^|FIND.exe /V /N ""`) DO (
SET "LN=%%A"
SET "LN=!LN:*]=!"
IF "!COUNT!" NEQ "%LINE%" (
ECHO(!LN!>>%_FILE%
) ELSE (
ECHO %_PREFIX%%_PROPKEY%=%_PROPVAL%>>%_FILE%
ECHO Updated '%_FILE%' with value '%_PREFIX%%_PROPKEY%=%_PROPVAL%'.
)
SET /A COUNT+=1
)
EXIT /B 0
:END
ECHO.
ECHO --- Finished Tests ---
pause
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment