-1

I am new to Batch scripting and have searched all the material available but I did not get a satisfactory answer.

I have a string called find_str which has the following content

baseUrl:{development:"https://myserver.example:1234"}

I want to replace the url given in development with http://localhost:8084

So the output should look like

baseUrl:{development:"http://localhost:8084"}

I have a variable called

find_string which has https://myserver.example:1234 and a variable called

replacestr which has http://localhost:8084

I have referred to this site Replace a substring using string substitution

But there the string to be replaced is hardcoded , I want the string to be replaced to come from the variables given above and want to store the entire result in a third variable.

I have tried the following:

setlocal ENABLEDELAYEDEXPANSION
For /F "tokens=1* delims==" %%A IN (environment.properties) DO (
    IF "%%A"=="url" set replacestr=%%B
)

set /p find_str=<%~dp0application\app\common\common.config.js

set cmd="echo %find_str:~92,37%"

FOR /F "tokens=*" %%i IN (' %cmd% ') DO SET find_string=%%i

echo !find_string!

echo !replacestr!

break>%~dp0portfolio-analyzer\app\common\common.config.js
set _result=%find_str:=!replacestr!%

>> %~dp0portfolio-analyzer\app\common\common.config.js echo %_result%

pause

But it is not replacing the find_str with its value.

Thanks in advance.

Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
Shripad Ashtekar
  • 201
  • 3
  • 17

1 Answers1

0
@ECHO OFF
SETLOCAL
SET "find_str=baseUrl:{development:"https://myserver.example:1234"}"
SET "replacestr=http://localhost:8084"

SET "critical_str=%find_str:*"=%"
:: don't know whether we need to simply remove the last 2 chars
:: or replace both `"` and `}` with nothing
SET "critical_str=%critical_str:"=%"
SET "critical_str=%critical_str:}=%"
SET "original_str=%find_str%"
:: replace colons
SET "critical_str=%critical_str::=`%"
SET "original_str=%original_str::=`%"

CALL set "result_str=%%original_str:%critical_str%=%replacestr%%%"
:: substitute-back
SET "result_str=%result_str:`=:%"
set|FIND /i "str="

GOTO :EOF

This is not a simple problem. It involves gymnastics with : to prevent : being interpreted inappropriately.


Now I've had a few seconds to think...

:: Using delayedexpansion
SETLOCAL ENABLEDELAYEDEXPANSION

SET "critical_str=%find_str:*"=%"
SET "critical_str=%critical_str:~0,-2%"
SET "result_str=!find_str:%critical_str%=%replacestr%!"

set|FIND /i "str="
Magoo
  • 77,302
  • 8
  • 62
  • 84