Page 1 of 1

Manipulating a variable

Posted: 23 Jan 2019 15:45
by SIMMS7400
Hi Folks -

I have a variable defined as such:

Code: Select all

SET "CLOUD_URL=https://planning-z552xxx1.pbcs.us2.oraclecloud.com"
What I need to do is if the variable above does not have the string "test" in it, I need to add it. Like such:

Code: Select all

SET "CLOUD_URL=https://planning-test-z552xxx1.pbcs.us2.oraclecloud.com"
And then the inverse, if the URL has "test" in it, I need to remove it. What is the easiest way to go about this?

Thank you!

Re: Manipulating a variable

Posted: 23 Jan 2019 16:05
by SIMMS7400
I played around and got this to work, but not sure if it's the most efficient?

Code: Select all

SET "CLOUD_URL=https://planning-z552xxx1.pbcs.us2.oraclecloud.com"
SET "SEARCH=test"

ECHO "%CLOUD_URL%" | FINDSTR /C:"%SEARCH%" >nul 2>&1 && ( SET "FLAG=T" )

IF DEFINED FLAG FOR /F "tokens=1,3 delims=-" %%A IN ("%CLOUD_URL%") DO SET "CLOUD_URL=%%~A-%%~B"
IF NOT DEFINED FLAG FOR /F "tokens=1-2 delims=-" %%A IN ("%CLOUD_URL%") DO SET "CLOUD_URL=%%~A-test-%%~B" 

Re: Manipulating a variable

Posted: 23 Jan 2019 16:45
by Aacini
I would do it this way:

Code: Select all

@echo off

SET "CLOUD_URL=https://planning-z552xxx1.pbcs.us2.oraclecloud.com"

if "%CLOUD_URL:-test=%" equ "%CLOUD_URL%" (
   set "CLOUD_URL=%CLOUD_URL:planning=planning-test%"
) else (
   set "CLOUD_URL=%CLOUD_URL:-test=%"
)

echo %CLOUD_URL%
If you need the "test" string to be in a variable, use DelayedExpansion in the replacements and %var% in the "test" variable.

Antonio