Page 1 of 1

SS64 Example for Variable Edit/Replace

Posted: 01 Jun 2017 23:34
by julesverne
Hi all,

I know some of you use and some even contribute to ss64 and stackoverflow and probably countless other forums. Well, there is one example in SS64 that kinda has always bugged me that I wonder if someone knows the answer to. The example below has comments explaining exactly what it does. -- Continue thought after code...

Code: Select all

:: 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%


Now it's a great example. However, this fails (as you may already know) if using delayed expansion. Does anyone know if this can be achieved with delayed expansion? To be clear that I'm not asking this question incorrectly I'll provide what I've tried.

Code: Select all

setlocal enabledelayedexpansion
:: 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.
for /l %%a in (1,1,2) do (
   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!
)


Note: Now I know you are first wondering about the for loop. You are correct it does nothing. I merely added it around the old code so that I could set up the environment where I would need "!" around the variables rather than "%" signs which I believe the difference is to evaluate the variables as the line is read rather than at the end of the loop.

My apologies in advance if I'm not being clear. I really just want to know if the code on SS64 can be used in a enabledelayedexpansion situation.

Re: SS64 Example for Variable Edit/Replace

Posted: 02 Jun 2017 02:55
by penpen
First you should use "[!_endbit!]" instead of "[%_endbit%]" within a code block.

Second you cannot escape an exclamation mark ('!') with "!!", you would have to use '^^^!' outside doublequotes, or '^!' inside doublequotes, but the "call" command doubles the circumflex accent character ('^'), so you cannot use this. Just use "call %%variable%%" instead - it still works within a code block.

There is a third issue related to successive "::" lines within code blocks; you could avoid this for example by using circumflex accent ('^') to merge these lines to one.

This might help you:

Code: Select all

setlocal enabledelayedexpansion
:: 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.
for /l %%a in (1,1,2) do (
   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!
)


penpen

Re: SS64 Example for Variable Edit/Replace

Posted: 07 Jun 2017 21:02
by julesverne
Thanks @penpen ! This works great! Also.. I had no idea about the "::" comments in code blocks. Very cool to know. I wonder if this should be considered as an example in SS64. I'll reach out to them and link them to this thread and see if they want to include it, and if so give you proper recognition. Thanks again!!

Re: SS64 Example for Variable Edit/Replace

Posted: 07 Jun 2017 21:54
by ShadowThief
I was under the impression that :: didn't work in a code block at all.

Re: SS64 Example for Variable Edit/Replace

Posted: 08 Jun 2017 06:39
by elzooilogico
I don't remember the post, but in this forum it was discussed. It does not work unless it is followed by another line beginning with :: But may I've forgotten something. The post was about comments or labels inside blocks.

Re: SS64 Example for Variable Edit/Replace

Posted: 12 Jun 2017 13:23
by catalinnc
or something like this...

Code: Select all

setlocal enabledelayedexpansion

for /l %%a in (1,1,1) do (

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

set _endbit=!_test:*brown=!

for %%b in ("!_endbit!") do set _startbit=!_test:%%~b=!

echo\!_startbit!

)

_