String Manipulation - how to use two variables?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
SIMMS7400
Posts: 539
Joined: 07 Jan 2016 07:47

String Manipulation - how to use two variables?

#1 Post by SIMMS7400 » 29 Apr 2020 06:18

Hi Folks -

I have a situation where I need to do some string manipulation. Here it my code:

Code: Select all

SET "CLOUD_URL=https://planning2-z558000.pbcs.us2.oraclecloud.com"

ECHO("%CLOUD_URL:~16,1%"|FINDSTR "^[\"][-][1-9][0-9]*[\"]$ ^[\"][1-9][0-9]*[\"]$ ^[\"]0[\"]$">nul && SET "NUM=%CLOUD_URL:~16,1%"||SET "NUM="

IF "%CLOUD_URL:-test=%" EQU "%CLOUD_URL%" (
   SET "CLOUD_URL=%CLOUD_URL:planning%NUM%=planning%NUM%-test%"
) ELSE ( 
    SET "CLOUD_URL=%CLOUD_URL:-test=%"
)
echo %CLOUD_URL%
pause
Based on what environment I'm logging into, I need to add -test or remove it. However, the tricky part is sometimes the url is planning2 or planning3 or just planning. Therefore, I'm trying to capture that value and use a variable in the string replacement. But it's not working. How would I get around this?

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: String Manipulation - how to use two variables?

#2 Post by Squashman » 29 Apr 2020 08:03

Either use CALL

Code: Select all

CALL SET "CLOUD_URL=%%CLOUD_URL:planning%NUM%=planning%NUM%-test%%"
Or Delayed Expansion

Code: Select all

SET "CLOUD_URL=!CLOUD_URL:planning%NUM%=planning%NUM%-test!"

Post Reply