pass value to string handle function failed

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
sincos2007
Posts: 21
Joined: 05 Apr 2019 05:52

pass value to string handle function failed

#1 Post by sincos2007 » 11 Apr 2019 04:38

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

aGerman
Expert
Posts: 3563
Joined: 22 Jan 2010 18:01
Location: Germany

Re: pass value to string handle function failed

#2 Post by aGerman » 11 Apr 2019 09:04

Code: Select all

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

sincos2007
Posts: 21
Joined: 05 Apr 2019 05:52

Re: pass value to string handle function failed

#3 Post by sincos2007 » 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.

IcarusLives
Posts: 105
Joined: 17 Jan 2016 23:55

Re: pass value to string handle function failed

#4 Post by IcarusLives » 11 Apr 2019 23:58

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!

sincos2007
Posts: 21
Joined: 05 Apr 2019 05:52

Re: pass value to string handle function failed

#5 Post by sincos2007 » 12 Apr 2019 04:02

Hi IcarusLives,

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

Thanks

aGerman
Expert
Posts: 3563
Joined: 22 Jan 2010 18:01
Location: Germany

Re: pass value to string handle function failed

#6 Post by aGerman » 12 Apr 2019 05:47

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

IcarusLives
Posts: 105
Joined: 17 Jan 2016 23:55

Re: pass value to string handle function failed

#7 Post by IcarusLives » 12 Apr 2019 09:17

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!

sincos2007
Posts: 21
Joined: 05 Apr 2019 05:52

Re: pass value to string handle function failed

#8 Post by sincos2007 » 12 Apr 2019 11:21

Hi IcarusLives,

By my test, your code works fine.

Thanks

sincos2007
Posts: 21
Joined: 05 Apr 2019 05:52

Re: pass value to string handle function failed

#9 Post by sincos2007 » 12 Apr 2019 11:26

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.

Post Reply