SS64 Example for Variable Edit/Replace
Posted: 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...
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.
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.
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.