Anand
Anand

Reputation: 47

windows batch to split string on first occurrence of a delimiter

I have a parameter string read from a properties file. One of the properties is as below:

"CUSTOM_JAVA_OPTIONS=-Dhttp.proxyHost=webcache.example.com -Dhttp.proxyPort=8080"

I need to split this string on the first occurrence of "=" and set a parameter with the value:

-Dhttp.proxyHost=webcache.example.com -Dhttp.proxyPort=8080

I am trying to split the string first on the = token and then remove the fist sub-string token from the original string. In the below code %%G will be set to "CUSTOM_JAVA_OPTIONS" and I am trying to remove this from the original string "TESTSTR"

@echo off

set "TESTSTR=CUSTOM_JAVA_OPTIONS=-Dhttp.proxyHost=webcache.example.com 
-Dhttp.proxyPort=8080"

SETLOCAL ENABLEDELAYEDEXPANSION

FOR /F "tokens=1,2 delims==" %%G IN ("%TESTSTR%") DO (

  echo Name=%%G
  echo Value=%%H
  set removestr=%%G

  echo TESTSTR=!TESTSTR!
  echo removestr=!removestr!

  set "str=!TESTSTR:%removestr%=!"
  echo str=!str!
  )
pause

The above does not seem to work, it produces:

Name=CUSTOM_JAVA_OPTIONS
Value=-Dhttp.proxyHost
TESTSTR=CUSTOM_JAVA_OPTIONS=-Dhttp.proxyHost=webcache.example.com -Dhttp.proxyPort=8080
removestr=CUSTOM_JAVA_OPTIONS
str=TESTSTR:=

Expected result needs to be:

 str=-Dhttp.proxyHost=webcache.example.com -Dhttp.proxyPort=8080

Upvotes: 1

Views: 4350

Answers (3)

dbenham
dbenham

Reputation: 130919

Your code fails entirely because %removestr% is expanded when the command is initially parsed, and your entire loop (code block) is parsed all at once. So %removestr% expands to the value that existed before your loop was entered. In your case, the variable is undefined. So !TESTSTR:%removestr%=! becomes !TESTSTR:=!, which finally becomes TESTSTR:=.

You get closer if you use %%G directly, instead of assigning an environment variable.
set str=!TESTSTR:%%G=! yields =-Dhttp.proxyHost=webcache.example.com -Dhttp.proxyPort=8080

You can then use set str=!str:~1!" to remove the leading =.

set str=!TESTSTR:%%G==! will not work because the search strings stops at the first occurrence of =, so the result is ==-Dhttp.proxyHost=webcache.example.com -Dhttp.proxyPort=8080

The RGuggisberg answer is the most convenient method to get your desired result. (You may want both %%G and %%H).

However, it technically does not break at the first =. It actually breaks at the first string of contiguous = because FOR /F does not parse empty tokens.

So for /f "tokens=1* delims==" %%G in ("A==B==C") yields A for %%G (correct), and B==C (incorrect) for %%H. The correct value should be =B==C.

Upvotes: 2

Compo
Compo

Reputation: 38719

If the first character after the = character is always a -, then the following method may also work for you:

@Echo Off
Set "TESTSTR=CUSTOM_JAVA_OPTIONS=-Dhttp.proxyHost=webcache.example.com -Dhttp.proxyPort=8080"
Set "REST=-%TESTSTR:*-=%"
Set "FIRST=%TESTSTR:-="&:"%"
Set "FIRST=%FIRST:~,-1%"
Echo [%FIRST%] [%REST%] & Pause

The bottom line is simply to show you the information.

Upvotes: 0

RGuggisberg
RGuggisberg

Reputation: 4750

This could be simplified to:

@echo off

set "TESTSTR=CUSTOM_JAVA_OPTIONS=-Dhttp.proxyHost=webcache.example.com -Dhttp.proxyPort=8080"

FOR /F "tokens=1* delims==" %%G IN ("%TESTSTR%") DO set "str=%%H"
echo TESTSTR=%TESTSTR%
echo.str=%str%
pause

There are 2 tokens:

1. Text up to 1st delimiter
2. Everything else after first delimiter (*)

Note that by echoing the variables outside of the FOR loop you don't need to enable delayed expansion.

Upvotes: 4

Related Questions