Manipulating a variable

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

Manipulating a variable

#1 Post by SIMMS7400 » 23 Jan 2019 15:45

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!

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: Manipulating a variable

#2 Post by SIMMS7400 » 23 Jan 2019 16:05

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" 
Last edited by SIMMS7400 on 23 Jan 2019 19:21, edited 1 time in total.

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Manipulating a variable

#3 Post by Aacini » 23 Jan 2019 16:45

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

Post Reply