Page 1 of 1

pass value to string handle function failed

Posted: 11 Apr 2019 04:38
by sincos2007

Code: Select all

:test2
echo on

setlocal EnableDelayedExpansion
set "input_line=1234567"
set "count_of_chars=2"

echo %input_line:~0,!count_of_chars!%

endlocal
goto :eof
I hope to print 12, but the result is incorrect. variable count_of_chars is necessary.

Thanks

Re: pass value to string handle function failed

Posted: 11 Apr 2019 09:04
by aGerman

Code: Select all

echo !input_line:~0,%count_of_chars%!
Steffen

Re: pass value to string handle function failed

Posted: 11 Apr 2019 22:30
by sincos2007
hi Steffen,

I have test the code you post, it really works. Thanks.

There is another code related to this topic which not work:

Code: Select all

:test4
echo on
setlocal EnableDelayedExpansion
set "input_line=1234567"
set "n=1"

for /l %%i in (1,1,7) do (
   set "count_of_chars=!n!"
   echo !input_line:~0,%count_of_chars%!
   set /a "n=n+1"
)

endlocal
goto :eof
This code have a for loop, which not work by your code.

Thanks.

Re: pass value to string handle function failed

Posted: 11 Apr 2019 23:58
by IcarusLives
sincos2007 wrote:
11 Apr 2019 22:30
hi Steffen,

I have test the code you post, it really works. Thanks.

There is another code related to this topic which not work:

Code: Select all

:test4
echo on
setlocal EnableDelayedExpansion
set "input_line=1234567"
set "n=1"

for /l %%i in (1,1,7) do (
   set "count_of_chars=!n!"
   echo !input_line:~0,%count_of_chars%!
   set /a "n=n+1"
)

endlocal
goto :eof
This code have a for loop, which not work by your code.

Thanks.
You need to expand !count_of_chars! inside ( )

But perhaps this is what you're looking for

Code: Select all

for /l %%i in (1,1,7) do echo !input_line:~0,%%i!

Re: pass value to string handle function failed

Posted: 12 Apr 2019 04:02
by sincos2007
Hi IcarusLives,

Could you show me how to expand !count_of_chars! inside ()?

Thanks

Re: pass value to string handle function failed

Posted: 12 Apr 2019 05:47
by aGerman
First of all - why don't you just use the FOR /L variable. I don't understand what n is even good for.

Another possibility:

Code: Select all

for /l %%i in (1,1,7) do (
   for %%j in (!n!) do echo !input_line:~0,%%j!
   set /a "n+=1"
)
Steffen

Re: pass value to string handle function failed

Posted: 12 Apr 2019 09:17
by IcarusLives
sincos2007 wrote:
12 Apr 2019 04:02
Hi IcarusLives,

Could you show me how to expand !count_of_chars! inside ()?

Thanks

Code: Select all

 call echo %%input_line:~0,!count_of_chars!%%
 
 or
 
 for %%i in (!count_of_chars!) do echo !input_line:~0,%%i!

Re: pass value to string handle function failed

Posted: 12 Apr 2019 11:21
by sincos2007
Hi IcarusLives,

By my test, your code works fine.

Thanks

Re: pass value to string handle function failed

Posted: 12 Apr 2019 11:26
by sincos2007
Hi Steffen,

Thanks for your code, it works fine.

In my real project, there are several other variables in for loop, which works with the loop together. The variable n is abstraction of them.