You are not logged in.
The only way that I've found is with (any kind of) FOR:
@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!
endlocalhttp://www.facebook.com/npocmaka
Offline
There is no need for CALL with your solution. I would probably use a single FOR /F.
@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!
endlocalThere 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%
endlocalbegin and end are expanded before the CALL, and string is expanded as a substring after the CALL
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)
Offline
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.
Offline
There is no need for CALL with your solution. I would probably use a single FOR /F.
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 :-).
http://www.facebook.com/npocmaka
Offline
@Ocalabob - yep, I did intend to use percents. I edited the post. Thanks.
Offline
There's no need for a "for" if you're using delayed expansion:
@echo off
setlocal enabledelayedexpansion
set begin=2
set end=2
set string=12345
set "string2=!string:~%begin%,%end%!"
echo %string2%
endlocalLast edited by bluesxman (28 Feb 2013 11:53)
CMD | *sh | KiX | AutoIT3 | PowerShell
Offline
There's no need for a "for" if you're using delayed expansion
Doh! Of course you are right. I'm not sure where my head was.
But the FOR loop does help if begin and end are defined in the same code block that does the substring. For example:
@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!
)
endlocalDave Benham
Last edited by dbenham (28 Feb 2013 12:16)
Offline
Ah good point. I think I'd typically call a sub-proc in those cases... that's a very worthy alternative, thanks!
CMD | *sh | KiX | AutoIT3 | PowerShell
Offline
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)
http://www.facebook.com/npocmaka
Offline
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
Offline