#1 26 Feb 2013 19:44

npocmaka
Member
From: Bulgaria
Registered: 03 Dec 2009
Posts: 325

substring parametrization under enabled delayed expansion

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!

endlocal

http://www.facebook.com/npocmaka

Offline

#2 27 Feb 2013 03:57

dbenham
Member
From: U.S. east coast
Registered: 15 Apr 2012
Posts: 100

Re: substring parametrization under enabled delayed expansion

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!

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

begin 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

#3 27 Feb 2013 15:22

Ocalabob
Member
Registered: 21 Feb 2013
Posts: 7

Re: substring parametrization under enabled delayed expansion

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

#4 27 Feb 2013 20:29

npocmaka
Member
From: Bulgaria
Registered: 03 Dec 2009
Posts: 325

Re: substring parametrization under enabled delayed expansion

dbenham wrote:

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

#5 28 Feb 2013 03:07

dbenham
Member
From: U.S. east coast
Registered: 15 Apr 2012
Posts: 100

Re: substring parametrization under enabled delayed expansion

@Ocalabob - yep, I did intend to use percents. I edited the post. Thanks.

Offline

#6 28 Feb 2013 11:52

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 936

Re: substring parametrization under enabled delayed expansion

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%

endlocal

Last edited by bluesxman (28 Feb 2013 11:53)


cmd | *sh  | KiX | AutoIT3 | PowerShell | Puppet

Offline

#7 28 Feb 2013 12:15

dbenham
Member
From: U.S. east coast
Registered: 15 Apr 2012
Posts: 100

Re: substring parametrization under enabled delayed expansion

bluesxman wrote:

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

endlocal

Dave Benham

Last edited by dbenham (28 Feb 2013 12:16)

Offline

#8 28 Feb 2013 13:52

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 936

Re: substring parametrization under enabled delayed expansion

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

Offline

#9 28 Feb 2013 23:42

npocmaka
Member
From: Bulgaria
Registered: 03 Dec 2009
Posts: 325

Re: substring parametrization under enabled delayed expansion

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

#10 02 Mar 2013 15:06

dbenham
Member
From: U.S. east coast
Registered: 15 Apr 2012
Posts: 100

Re: substring parametrization under enabled delayed expansion

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

Offline

Board footer

Powered by FluxBB