String Indexing

  • Thread starter Thread starter Brown Delivers
  • Start date Start date
B

Brown Delivers

Can a varible not be used to indicate the "length" when
string indexing? This is what I observe:

Set Total=********************
Set X=10
Set Progress=%Total:~0,10%

C:\>ECHO %Progress%
**********

Now, say I try to use variable X to indicate the length:

Set Progress=%Total:~0,%X%%

C:\>ECHO %Progress%
X%%

What am I doing wrong?
 
Brown said:
Can a varible not be used to indicate the "length" when
string indexing? This is what I observe:

Set Total=********************
Set X=10
Set Progress=%Total:~0,10%

C:\>ECHO %Progress%
**********

Now, say I try to use variable X to indicate the length:

Set Progress=%Total:~0,%X%%

C:\>ECHO %Progress%
X%%

What am I doing wrong?

The first version below is for Win2000 or newer versions of NT:

<Win2000> c:\cmd>demo\StrIndex
Progress=**********

<Win2000> c:\cmd>rlist demo\StrIndex.cmd
=====begin c:\cmd\demo\StrIndex.cmd ====================
1. @echo off
2. setlocal enabledelayedexpansion
3. Set Total=********************
4. Set X=10
5. Set Progress=!Total:~0,%X%!
6. Set Progress
=====end c:\cmd\demo\StrIndex.cmd ====================

The following version is for NT 4.0 (but will work in any version
of NT):

<Win2000> c:\cmd>demo\StrIndexNT40
Progress=**********

<Win2000> c:\cmd>rlist demo\StrIndexNT40.cmd
=====begin c:\cmd\demo\StrIndexNT40.cmd ====================
1. @echo off
2. setlocal
3. Set Total=********************
4. Set X=10
5. call Set Progress=%%Total:~0,%X%%%
6. Set Progress
=====end c:\cmd\demo\StrIndexNT40.cmd ====================
 
Thanks Phil.

The first version (for 2000 and above) makes sense to me
but I'm curious as to how version two (for NT and above)
works.

If the line

call Set Progress=%%Total:~0,%X%%%

is replaced with

call :Set Progress=%%Total:~0,%X%%%

the shell complains "The system cannot find the batch
label specified - Set". This makes sense since their is
no label explicitly defined as Set. However, why or how
does it work without the colon?
 
See tip 4860 in the 'Tips & Tricks' at http://www.jsiinc.com

I guess if it no label is specified it simply parses the strings and runs the
command.


Thanks Phil.

The first version (for 2000 and above) makes sense to me
but I'm curious as to how version two (for NT and above)
works.

If the line

call Set Progress=%%Total:~0,%X%%%

is replaced with

call :Set Progress=%%Total:~0,%X%%%

the shell complains "The system cannot find the batch
label specified - Set". This makes sense since their is
no label explicitly defined as Set. However, why or how
does it work without the colon?


Jerold Schulman
Windows: General MVP
JSI, Inc.
http://www.jsiinc.com
 
Brown said:
Thanks Phil.

The first version (for 2000 and above) makes sense to me
but I'm curious as to how version two (for NT and above)
works.

If the line

call Set Progress=%%Total:~0,%X%%%

is replaced with

call :Set Progress=%%Total:~0,%X%%%

the shell complains "The system cannot find the batch
label specified - Set". This makes sense since their is
no label explicitly defined as Set. However, why or how
does it work without the colon?

CMD.EXE can call internal commands such as SET, ECHO, etc.
So in this case, SET is an internal command of CMD.EXE, not
an external batch file (no colon) or a subroutine of the
executing batch file (colon).
 
OK, but why the double %% sign:

call Set Progress=%%Total:~0,%X%%%

as opposed to one:

call Set Progress=%Total:~0,%X%%


Thanks again Phil.
 
Brown said:
OK, but why the double %% sign:

call Set Progress=%%Total:~0,%X%%%

because that's what works. :-)
as opposed to one:

call Set Progress=%Total:~0,%X%%

because that doesn't work now, does it? ;-)
Thanks again Phil.

The double percent signs are because the statement is going to
be evaluated (and variables resolved) *twice* when we CALL SET.
Beginning with X=10 and Total=********************, we first

CALL SET Progress=%%Total:~0,%X%%%

After the first resolution, we have

Progress=%Total:~0,10%

where '%X%' has been replaced by '10'. The second resolution
then gives us

Progress=**********
 
Back
Top