#1 28 Apr 2019 02:41

Registered: 28 Apr 2019
Posts: 2

Help with substring replacement inside for loop

My question relates to:

https: // ss64.com /nt/syntax-replace.html

:: To remove characters from the right hand side of a string is
:: a two step process and requires the use of a CALL statement
:: e.g.

   SET _test=The quick brown fox jumps over the lazy dog

   :: To delete everything after the string 'brown' 
   :: first delete 'brown' and everything before it
   SET _endbit=%_test:*brown=%
   Echo We dont want: [%_endbit%]

   ::Now remove this from the original string
   CALL SET _result=%%_test:%_endbit%=%%
   echo %_result%

So, inside a for loop, using delayedexpansion, when _test and _endbit would be normally used as !_test! and !_endbit! - how should I use the CALL?

CALL SET _result=%%_test:%_endbit%=%%

I tried with all permutations I could imagine of changing % by !, doubling and triping the ! to no aval.

Thanks in advance!


#2 28 Apr 2019 22:21

From: UK
Registered: 29 Dec 2006
Posts: 1,126

Re: Help with substring replacement inside for loop

If you've got the whole thing in a loop, including the setting of %_endbit%, then you're going to run up against expansion issues again (in the absence of your actual code I'm going to assume that's what's going on).

I think it will work if you double up the % and also the call to match.


call call set _result=%%%%_test:%%_endbit%%=%%%%

Last edited by bluesxman (28 Apr 2019 22:22)

cmd | *sh | ruby | chef


Board footer

Powered by FluxBB