2

I am trying to do some substring in windows batch programming, but it seems it's not as easy as linux scripting.

Here is my code snippet.

set ENV_STRING=-Dhello.env=hello -Dhello.alt.dir=C:\Works\download -Dhello.type=DDDDD
set START_INDEX=34
set END_INDEX=51
SET newStr=%ENV_STRING:~%START_INDEX%,%END_INDEX%%

START_INDEX and END_INDEX will change depending on the ENV_STRING which will be provided by a user. I am using static integers for START_INDEX and END_INDEX just to show that these variables are used. I just want to extract the value of hello.alt.dir from ENV_STRING.

When I print out newStr, it doesn't substring the string.

Could someone please point out which part is wrong?

Thanks.

4
  • The question is unclear, Please show the desired result. Commented Mar 8, 2017 at 15:51
  • I would like to extract only 'C:\Works\download' which is the value of '-Dhello.alt.dir' from ENV_STRING. Commented Mar 8, 2017 at 15:56
  • 1
    Use SET newStr=!ENV_STRING:~%START_INDEX%,%END_INDEX%! with Delayed Expansion, or CALL SET newStr=%%ENV_STRING:~%START_INDEX%,%END_INDEX%%%. I suggest you to read this answer that explains this management, although the topic is different. Commented Mar 8, 2017 at 16:12
  • I will take a look at other resource. Thanks. Commented Mar 9, 2017 at 19:34

2 Answers 2

2

You can use following code:

@echo off
set "ENV_STRING=-Dhello.env=hello -Dhello.alt.dir=C:\Works\download - Dhello.type=DDDDD"
set "START_INDEX=34"
set "END_INDEX=51"
setlocal EnableDelayedExpansion
set "newStr=!ENV_STRING:~%START_INDEX%,%END_INDEX%!"
endlocal & set "newStr=%newStr%"

It is not possible to reference the value of an environment variable like START_INDEX or END_INDEX within a variable reference. Windows command interpreter fails to interpret the variable references.

A solution is using delayed expansion for the variable on which the substitution is applied and immediate expansion for start and end index references as shown in the code above.

But command SETLOCAL creates a copy of current environment variables and therefore the environment variable newStr would not exist anymore after command ENDLOCAL discarding the current environment variables list and restoring the previous variables list. The solution is using ENDLOCAL and SET with immediate variable expansion on one line.

BTW: String splitting is usually done in a Windows batch script using for /F.

Sign up to request clarification or add additional context in comments.

Comments

2

try like this:

setlocal enableDelayedExpansion
set ENV_STRING=-Dhello.env=hello -Dhello.alt.dir=C:\Works\download -Dhello.type=DDDDD
set START_INDEX=34
set END_INDEX=51
SET newStr=!ENV_STRING:~%START_INDEX%,%END_INDEX%!
echo -%newStr%-

With delayed expansion and variables enclosed with ! the expression will be evaluated when it is executed (i.e. delayed). While the variables enclosed with % will be replaces immediately .Thus you can avoid the % collisions.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.