Computing.Net > Forums > Disk Operating System > length of a string in a batch file

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

length of a string in a batch file

Reply to Message Icon

Name: bob cat
Date: July 30, 2003 at 06:22:49 Pacific
OS: windows 2000 sp3
CPU/Ram: p3 800mhz/512M
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: Secret_Doom
Date: July 30, 2003 at 10:39:49 Pacific
Reply:

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

____________________________________________________________


0

Response Number 2
Name: bob cat
Date: July 30, 2003 at 11:25:47 Pacific
Reply:

excellent! just what I was looking for.

Thanks.


0

Response Number 3
Name: bunder
Date: August 28, 2003 at 09:03:47 Pacific
Reply:

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!


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Disk Operating System Forum Home


Sponsored links

Ads by Google


Results for: length of a string in a batch file

Max Line Length in a batch file www.computing.net/answers/dos/max-line-length-in-a-batch-file/9738.html

User log in date from a batch file www.computing.net/answers/dos/user-log-in-date-from-a-batch-file/7783.html

Dos Commands in a batch file www.computing.net/answers/dos/dos-commands-in-a-batch-file/1258.html