SS64 Example for Variable Edit/Replace

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
julesverne
Posts: 81
Joined: 19 Nov 2013 00:41

SS64 Example for Variable Edit/Replace

#1 Post by julesverne » 01 Jun 2017 23:34

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.

penpen
Expert
Posts: 1839
Joined: 23 Jun 2013 06:15
Location: Germany

Re: SS64 Example for Variable Edit/Replace

#2 Post by penpen » 02 Jun 2017 02:55

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

julesverne
Posts: 81
Joined: 19 Nov 2013 00:41

Re: SS64 Example for Variable Edit/Replace

#3 Post by julesverne » 07 Jun 2017 21:02

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!!

ShadowThief
Expert
Posts: 964
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: SS64 Example for Variable Edit/Replace

#4 Post by ShadowThief » 07 Jun 2017 21:54

I was under the impression that :: didn't work in a code block at all.

elzooilogico
Posts: 119
Joined: 23 May 2016 15:39
Location: Spain

Re: SS64 Example for Variable Edit/Replace

#5 Post by elzooilogico » 08 Jun 2017 06:39

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.

catalinnc
Posts: 39
Joined: 12 Jan 2015 11:56

Re: SS64 Example for Variable Edit/Replace

#6 Post by catalinnc » 12 Jun 2017 13:23

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!

)

_

Post Reply