length of a string in a batch file

Score
0
Vote Up
bob cat July 30, 2003 at 06:22:49 Pacific
Specs: windows 2000 sp3, p3 800mhz/512M

I need to find the length of a string (variable actually) in a batch file, does anyone know how to do this simply?

Reply ↓  Report •


#1
Vote Down
Score
6
Vote Up
Secret_Doom July 30, 2003 at 10:39:49 Pacific

This will do it:

:: Send the length of the variable %MyVar%
:: to the variable %length%
set #=%MyVar%
set length=0
:loop
if defined # (set #=%#:~1%&set /A length += 1&goto loop)
echo MyVar is %length% characters long!

-- Leonardo Pignataro - Secret_Doom --

secret_doom@hotmail.com
www.batch.hpg.com.br

____________________________________________________________


Reply ↓  Report •

#2
Vote Down
Score
0
Vote Up
bob cat July 30, 2003 at 11:25:47 Pacific

excellent! just what I was looking for.

Thanks.


Reply ↓  Report •

#3
Vote Down
Score
6
Vote Up
bunder August 28, 2003 at 09:03:47 Pacific

I'm getting a syntax error when I try to use this code. Any thoughts as to why this might be happening? One thing I notice is that the command interpreter removes the %#:~1% bit. Is this by design?

Thank you!


Reply ↓  Report •

Reply to Message Icon Start New Discussion
Related Posts

« file sharing dos to dos n... if expression will not ev... »

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.

Ask the Community!
Describe your Problem
Example: Hard Drive Not Detected on My PC