Handling property files in a DOS batch script.
Posted: 09 Jan 2012 12:38
I created this test script to handle property files in DOS batch scripts. The scripts works fairly well as-is but I am hoping for any tips that anyone might have for improving it?
NOTE: On the PROPEDIT function, there is an error that I am unable to figure out :
Anyway, thanks to anyone who might want to contribute some advice on this:
NOTE: On the PROPEDIT function, there is an error that I am unable to figure out :
The system cannot find the file specified.
Error occurred while processing: .exe.
Anyway, thanks to anyone who might want to contribute some advice on this:
Code: Select all
@echo off
CALL :PROPEDIT # Key4 Value446 test.properties
GOTO :END
:PROPEDIT [#] PropKey PropVal File
IF "%~1"=="#" (
:: Passing a first argument of "#" will disable the line while editing
SET "_PREFIX=#"
SHIFT
)
IF "%~3"=="" (
ECHO PROPEDIT: Function requires 3 args: [#] PropKey PropVal File
PAUSE
GOTO :END
) ELSE (
SET "_PROPKEY=%~1"
SET "_PROPVAL=%~2"
SET "_FILE=%~3"
)
MOVE /Y "%_FILE%" "%_FILE%.bak">nul
FOR /F "USEBACKQ tokens=*" %%A IN (`TYPE.exe "%_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.exe "%_FILE%.bak" ^|FIND.exe /V /N ""`) DO (
SET "LN=%%A"
SETLOCAL ENABLEDELAYEDEXPANSION
SET "LN=!LN:*]=!"
IF "!COUNT!" NEQ "%LINE%" (
ECHO(!LN!>>%_FILE%
) ELSE (
ECHO %_PREFIX%%_PROPKEY%=%_PROPVAL%>>%_FILE%
ECHO Updated '%_FILE%' with value '%_PREFIX%%_PROPKEY%=%_PROPVAL%'.
)
ENDLOCAL
SET /A COUNT+=1
)
EXIT /B 0
:END
ECHO --- Finished Test ---
pause