0

I have a simple question. I have a config.json file with the below content

"DefaultAdminUsername": "Administrator@test.com",
"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%
UnknownOctopus
  • 2,057
  • 1
  • 15
  • 26
user336058
  • 115
  • 2
  • 10
  • 2
    What does this have to do with `bash`? – Etan Reisner Jul 27 '15 at 23:47
  • Ok, I need a way , to configure the number of users to be created by my .net application. for example when my application starts , it will create 100 user entries in the database..... this is something like creating a work load for my application. So for this I thought of adding "DefaultNoOfUser" in Config.json. And this value can be modified to create workloads. Example 10 users . 100 users or even 1000 users – user336058 Jul 28 '15 at 00:17
  • Unclear what you're asking. In case it helps, you could use JScript methods to read and manipulate a JSON string, then write the `obj.toString()` to a new (or overwrite the existing) JSON file. [More info](http://stackoverflow.com/a/27643386/1683264). – rojo Jul 28 '15 at 01:45

1 Answers1

2

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.

Mofi
  • 46,139
  • 17
  • 80
  • 143