npocmaka
The only way that I've found is with (any kind of) FOR:
Code: Select all
@echo off
setlocal enabledelayedexpansion
set begin=2
set end=2
set string=12345
for %%A in (!begin!) do (
for %%B in (!end!) do (
call set string2=!string:~%%A,%%B!
)
)
echo !string2!
endlocal
#2 27 Feb 2013 03:57
dbenham
There is no need for CALL with your solution. I would probably use a single FOR /F.
Code: Select all
@echo off
setlocal enabledelayedexpansion
set begin=2
set end=2
set string=12345
for /f "tokens=1,2" %%A in ("!begin! !end!") do set "string2=!string:~%%A,%%B!"
echo !string2!
endlocal
There is another way using CALL and normal expansion, but it is much slower. The performance difference is not obvious unless you execute the code many times in a loop.
@echo off
setlocal
set begin=2
set end=2
set string=12345
call set "string2=%%string:~%begin%,%end%%%"
echo %string2%
endlocal
I prefer the delayed expansion method with the FOR statement, even though it is more verbose.
Dave Benham
Last edited by dbenham (28 Feb 2013 03:06)
----------------------------
#3 27 Feb 2013 15:22
Ocalabob
Hi Dave,
I think there is a typo in your second example. Did mean "echo %string2%" instead of "echo !string2!" ?
Never the less, interesting work by you and npocmaka.
Later.
----------------------------
#4 27 Feb 2013 20:29
npocmaka
Once I knew that there's no need of CALL , but I'm really absent-mind person.Thanks for pointing.And it's really more neat with a single FOR .dbenham wrote:
There is no need for CALL with your solution. I would probably use a single FOR /F.
----------------------------
#5 28 Feb 2013 03:07
dbenham
@Ocalabob - yep, I did intend to use percents. I edited the post. Thanks.
----------------------------
#6 28 Feb 2013 11:52
bluesxman
There's no need for a "for" if you're using delayed expansion:
Code: Select all
@echo off
setlocal enabledelayedexpansion
set begin=2
set end=2
set string=12345
set "string2=!string:~%begin%,%end%!"
echo %string2%
endlocal
cmd | *sh | ruby | chef
----------------------------
#7 28 Feb 2013 12:15
dbenham
Doh! Of course you are right. I'm not sure where my head was.bluesxman wrote:
There's no need for a "for" if you're using delayed expansion
But the FOR loop does help if begin and end are defined in the same code block that does the substring. For example:
Code: Select all
@echo off
setlocal enabledelayedexpansion
if ... (
set begin=2
set end=2
set string=12345
for /f "tokens=1,2" %%A in ("!begin! !end!") do set "string2=!string:~%%A,%%B!"
echo !string2!
)
endlocal
Dave Benham
Last edited by dbenham (28 Feb 2013 12:16)
----------------------------
#8 28 Feb 2013 13:52
bluesxman
Ah good point. I think I'd typically call a sub-proc in those cases... that's a very worthy alternative, thanks!
cmd | *sh | ruby | chef
----------------------------
#9 28 Feb 2013 23:42
npocmaka
Aghhmm..Yes .I first hit this within parentheses block and delayed expansion , but I thought that only the delayed expansion was the reason for my troubles. Good that this was clarified.
Last edited by npocmaka (01 Mar 2013 12:35)
----------------------------
#10 02 Mar 2013 15:06
dbenham
bluesxman wrote:
Ah good point. I think I'd typically call a sub-proc in those cases... that's a very worthy alternative, thanks!
More than just a worthy alternative if used within a large FOR loop. The CALL method is certainly simple, but it is slow. The FOR method is much faster. It probably doesn't matter for a few iterations, but the difference is striking once you have hundreds of iterations.
Dave Benham
original thread: https://ss64.org/oldforum/viewtopic.php?id=1669