0

I need to find a way to automate the settings of a configuration file. The file basically looks like this:

value1=hello
value2=world
value3=cookies
value4=cream

Ideally I would like to run a .bat file, with something like the following parameters:

setconfiguration.bat filename -value3 test -value4 success

This would then change the file data to the following:

value1=hello
value2=world
value3=test
value4=success

Is there any easy way of doing this without having to install any new tools? Honestly I have little experience with scripting (.bat) and Google has not been very helpful so far. :(

EDIT: I found a script called repl.bat on this answer; https://stackoverflow.com/a/16735079/4121213 Using this I can do something like which replaces a single value. This should be able to help me out further.

@echo off
setlocal
cd /d %~dp0
Set "OldString=^value3=\S{1,100}"
Set "NewString=value3=test"
set file="test.txt"
for %%F in (%file%) do set outFile="%%~nFCleaned%%~xF"
pause
call repl OldString NewString e <%file% > %outfile%

Thanks,

Community
  • 1
  • 1
Joachim Seminck
  • 721
  • 1
  • 8
  • 10

1 Answers1

1

Save this text as a bat file, then call like: setconfiguration.bat filename value3 test value4 success You can put any number of value pairs.

@echo off
setlocal

set tmpfile="%temp%\Alterconfiguration.txt"
:loop
if x%3 equ x goto done
if exist %tmpfile% del /f %tmpfile%
set key=%2
set value=%3
rem remove any quotes from strings with spaces
set key=%key:"=%
set value=%value:"=%
for /f "tokens=1,* delims==" %%a in (%1) do (
    if /i "%%a" equ "%key%" (
        echo %key%=%value%>> %tmpfile%
    ) else (
        echo %%a=%%b>> %tmpfile%
    )
)
copy %tmpfile% %1 /y
shift /2
goto loop

:done
endlocal

If you need to handle spaces, which I suspect you will, then you need to include the parameters in quotes. e.g. setconfiguration.bat filename "value 3" "test a full string" value4 success

Scott C
  • 1,660
  • 1
  • 11
  • 18