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.

user826323
  • 2,248
  • 6
  • 43
  • 70
  • The question is unclear, Please show the desired result. – jfatal Mar 08 '17 at 15:51
  • I would like to extract only 'C:\Works\download' which is the value of '-Dhello.alt.dir' from ENV_STRING. – user826323 Mar 08 '17 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](http://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script/10167990#10167990) that explains this management, although the topic is different. – Aacini Mar 08 '17 at 16:12
  • I will take a look at other resource. Thanks. – user826323 Mar 09 '17 at 19:34

2 Answers2

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.

npocmaka
  • 55,367
  • 18
  • 148
  • 187
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.

Mofi
  • 46,139
  • 17
  • 80
  • 143