-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.

6
  • You need to show what you have tried and to describe what exactly you have trouble with! Commented Nov 7, 2016 at 7:45
  • 2
    Possible duplicate of batch script replace variable string within string Commented Nov 7, 2016 at 8:06
  • I have updated the question with whatever I have tried. Commented Nov 7, 2016 at 8:57
  • 1
    Don't take your guns to town.....It's not a trivial substitution since it involves strings containing ":" Commented Nov 7, 2016 at 9:31
  • @Magoo, the : can be used in standard substring replacement syntax; problematic are: =, * and % (or ! in case of delayed expansion)... Commented Nov 7, 2016 at 12:03

1 Answer 1

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="
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.