Reputation: 125
I have a simple question. I have a config.json file with the below content
"DefaultAdminUsername": "[email protected]",
"DefaultAdminPassword": "Admin!",
"DefaultNoOfUsers": "100",
I want to modify the value of "DefaultNoOfUsers" from a batch file for example : Change from 100 to 1000
A pseudo code could be
for /f %%A in (config.json) do (
SET key = value
)
echo %VERSION%
echo %TEST%
Upvotes: 0
Views: 5421
Reputation: 49084
Here is a little commented batch file for making this replacement in file config.json
without checking for input mistakes.
@echo off
rem Exit this batch file if file config.json does not exist in current directory.
if not exist config.json goto :EOF
rem Make sure that the temporary file used does not exist already.
del "%TEMP%\config.json" 2>nul
rem Define a default value for number of users.
set "DefaultNoOfUsers=100"
rem Ask user of batch file for the number of users.
set /P "DefaultNoOfUsers=Number of users (%DefaultNoOfUsers%): "
rem Copy each line from file config.json into temporary file
rem with replacing on line with "DefaultNoOfUsers" the number.
for /F "tokens=1* delims=:" %%A in (config.json) do (
if /I %%A == "DefaultNoOfUsers" (
echo %%A: "%DefaultNoOfUsers%",>>"%TEMP%\config.json"
) else (
echo %%A:%%B>>"%TEMP%\config.json"
)
)
rem Move the temporary file over file config.json in current directory.
move /Y "%TEMP%\config.json" config.json >nul
rem Delete the used environment variable.
set "DefaultNoOfUsers="
Run in a command prompt window for /?
and read entire help output in window to understand how the for loop works.
Upvotes: 2