#1 17 Aug 2017 22:01

julesverne
New Member
Registered: 17 Aug 2017
Posts: 2

Adding another example to Syntax-Replace in CMD

Hi!

Long time user, first time poster.  I wanted to suggest an example to add to Syntax Replace in CMD section, or add to one.

Currently there is an example on the page that looks like this:

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

Which is useful but on a few occasions I've needed to use delayed expansion inside a for loop and that example won't work and the answer wasn't immediately clear to me how to change that code to work.  I wasn't aware of the SS64 forums and so I posted the question in dostips.com and got a solution. 
The solution:

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 The

for

loop in the solution is only used for display purposes of the delayed expansion and not doing anything. The contributor @penpen also mentions in that link that consecutive '

::

' comments will error inside of a for loop unless adding a caret symbol '

^

'.

I was wondering if that solution (or something similar) would be beneficial to users if added to the page.

The main differences between the two

Original ex

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

to

delayed expansion

SET _endbit=!_test:*brown=!
CALL SET _result=%%_test:!_endbit!=%%

Also, kudos for this awesome site.

Thanks!

Jules

Offline

#2 19 Aug 2017 15:07

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 902
Website

Re: Adding another example to Syntax-Replace in CMD

A principle I try to follow when writing these examples is to try and focus on only one or two items of syntax at a time, so that it is terse and to the point.
Having an example which is demonstrating variable replacement AND Delayed Expansion AND a FOR loop, AND explaining about different comments is going to be too complex for most people to follow, they do all get a mention but on separate pages.

That said I have added a line linking to the DelayedExpansion page for people who are using that.

Offline

#3 21 Aug 2017 08:01

julesverne
New Member
Registered: 17 Aug 2017
Posts: 2

Re: Adding another example to Syntax-Replace in CMD

Hi Simon,

That makes sense. I agree it would be confusing and might muddy up the point of the page itself.  I just wanted to pass on that information in case it was helpful to the site since I had a hard time figuring out the solution.  Keep up the great site!!

Offline

Board footer

Powered by FluxBB