Counting Characters

  • Thread starter Thread starter Mr. Novice
  • Start date Start date
M

Mr. Novice

Hello,

Does anybody know how to count the number of characters in
a variable?

For example, if %var1% was equal to abcdefg is there a way
to set another variable to equal the number of characters
in %var1%, seven in this case?

Thanks in advance,

Chad
 
That's exactly what I need, but can you explain a few
things for me? In the loop section of the code that I
pasted below, he calls "$poslen" which hasn't been
defined. How does that work? He also references a variable
that hasn't been defined, "%$substring%". How does that
work?


:loop
call $poslen "%$string%" %$len% 4
if "%$substring%" EQU "#*#*" goto end
set /a $len=%$len% + 1
goto loop

Thanks!

Chad
 
Mr. Novice said:
That's exactly what I need, but can you explain a few
things for me? In the loop section of the code that I
pasted below, he calls "$poslen" which hasn't been
defined. How does that work? He also references a variable
that hasn't been defined, "%$substring%". How does that
work?


:loop
call $poslen "%$string%" %$len% 4
if "%$substring%" EQU "#*#*" goto end
set /a $len=%$len% + 1
goto loop

Thanks!

Chad

Maybe try this instead (originally posted some years ago
by Walter Zackery):

===== begin file c:\CMD\UTIL\LENGTH.CMD =====
1. @ECHO OFF & set/a length = 0
2. if %1[==[ goto :EOF
3. set cd=
4. if not defined cd (echo%*) > %temp%\$rt5$.bak
5. if defined cd (echo.%*) > %temp%\$rt5$.bak
6. for /f "tokens=3" %%? in (
7. 'dir/-c %temp%\$rt5$.bak^|find/i "$rt5$"') do (
8. set/a length = %%? - 2 & del %temp%\$rt5$.bak)
===== end file c:\CMD\UTIL\LENGTH.CMD =====
 
So the amount of characters in a string can be determined
by subtracting 2 from the size of the file it makes when
echoed to a junk file?

Sorry to be such a pain. Its just that I sleep better when
I understand how it works. ;)


Chad


-----Original Message-----
Mr. Novice said:
That's exactly what I need, but can you explain a few
things for me? In the loop section of the code that I
pasted below, he calls "$poslen" which hasn't been
defined. How does that work? He also references a variable
that hasn't been defined, "%$substring%". How does that
work?


:loop
call $poslen "%$string%" %$len% 4
if "%$substring%" EQU "#*#*" goto end
set /a $len=%$len% + 1
goto loop

Thanks!

Chad

Maybe try this instead (originally posted some years ago
by Walter Zackery):

===== begin file c:\CMD\UTIL\LENGTH.CMD =====
1. @ECHO OFF & set/a length = 0
2. if %1[==[ goto :EOF
3. set cd=
4. if not defined cd (echo%*) > %temp%\$rt5$.bak
5. if defined cd (echo.%*) > %temp%\$rt5$.bak
6. for /f "tokens=3" %%? in (
7. 'dir/-c %temp%\$rt5$.bak^|find/i "$rt5$"') do (
8. set/a length = %%? - 2 & del %temp%\$rt5$.bak)
===== end file c:\CMD\UTIL\LENGTH.CMD =====
characters

in


--
Phil Robyn
Univ. of California, Berkeley

u n z i p m y a d d r e s s t o s e n d e - m a i l

.
 
Hello,

Does anybody know how to count the number of characters in
a variable?

For example, if %var1% was equal to abcdefg is there a way
to set another variable to equal the number of characters
in %var1%, seven in this case?

Thanks in advance,

Here's a couple of ways.

Garry

@echo off
setlocal
set var1=abcdefg
call :getstrlen len var1
echo Len is %len%
goto :eof

:getstrlen
::--------------------------------------------
:: This routine will handle poison characters
::--------------------------------------------
for /f "tokens=1* delims==" %%a in ('set %2') do set str=%%b
set "str=%str:"= %"
set i=0
if not defined str goto :gsl_Done
set i=1
if "%str:~1%"=="" goto :gsl_Done
:gsl_loop
set /a i+=1
set "str= %str:~2%"
if not "%str:~1%"=="" goto :gsl_loop
:gsl_Done
endlocal & (set %1=%i%)
goto :eof

:getstrlen1
::--------------------------------------------
:: This routine is fastest for W2k and above
:: but writes a temp file and doesn't handle poison characters
::--------------------------------------------
echo:"%~2">"%temp%\gsl.txt"
for %%i in ("%temp%\gsl.txt") do (
set /a %1=%%~zi-4
del /q %%i
)
goto :eof

:getstrlen2
::--------------------------------------------
:: This routine is for NT or if you don't want to write a temp file
:: It doesn't handle poison characters
::--------------------------------------------
setlocal
set str=%2
set str=%str:"=%
set len=0
:gsl_loop2
If NOT "%str%"=="" (
set str=%str:~1%
set /A len+=1
goto :gsl_loop2
)
(endlocal) & (set %1=%len%)
goto :eof
 
Mr. Novice said:
So the amount of characters in a string can be determined
by subtracting 2 from the size of the file it makes when
echoed to a junk file?

Sorry to be such a pain. Its just that I sleep better when
I understand how it works. ;)
Chad
Echo adds a cr/lf sequence to your string that counts two bytes. If you
have a hexeditor or dump util you can this as Hex: 0D 0A.

hth
 
Maybe try this instead (originally posted some years ago
by Walter Zackery):

===== begin file c:\CMD\UTIL\LENGTH.CMD =====
1. @ECHO OFF & set/a length = 0
2. if %1[==[ goto :EOF
3. set cd=
4. if not defined cd (echo%*) > %temp%\$rt5$.bak
5. if defined cd (echo.%*) > %temp%\$rt5$.bak
6. for /f "tokens=3" %%? in (
7. 'dir/-c %temp%\$rt5$.bak^|find/i "$rt5$"') do (
8. set/a length = %%? - 2 & del %temp%\$rt5$.bak)
===== end file c:\CMD\UTIL\LENGTH.CMD =====

Cool! That inspired this version which runs in XP and should also run
in Win2K but not NT.

echo:%*>$rt5$.bak
for %%a in ($rt5$.bak) do set /a length=%%~za-2
del $rt5$.bak

Thanks

Clay Calvert
(e-mail address removed)
Replace "W" with "L"
 
Tip 4193 in the 'Tips & Tricks' at http://www.jsiinc.com has a link to $poslen,
which you must define/

That's exactly what I need, but can you explain a few
things for me? In the loop section of the code that I
pasted below, he calls "$poslen" which hasn't been
defined. How does that work? He also references a variable
that hasn't been defined, "%$substring%". How does that
work?


:loop
call $poslen "%$string%" %$len% 4
if "%$substring%" EQU "#*#*" goto end
set /a $len=%$len% + 1
goto loop

Thanks!

Chad


Jerold Schulman
Windows: General MVP
JSI, Inc.
http://www.jsiinc.com
 
Clay said:
Maybe try this instead (originally posted some years ago
by Walter Zackery):

===== begin file c:\CMD\UTIL\LENGTH.CMD =====
1. @ECHO OFF & set/a length = 0
2. if %1[==[ goto :EOF
3. set cd=
4. if not defined cd (echo%*) > %temp%\$rt5$.bak
5. if defined cd (echo.%*) > %temp%\$rt5$.bak
6. for /f "tokens=3" %%? in (
7. 'dir/-c %temp%\$rt5$.bak^|find/i "$rt5$"') do (
8. set/a length = %%? - 2 & del %temp%\$rt5$.bak)
===== end file c:\CMD\UTIL\LENGTH.CMD =====


Cool! That inspired this version which runs in XP and should also run
in Win2K but not NT.

echo:%*>$rt5$.bak
for %%a in ($rt5$.bak) do set /a length=%%~za-2
del $rt5$.bak

Thanks

Clay Calvert
(e-mail address removed)
Replace "W" with "L"

Hi, Clay,

That's very nice! Too bad I still have to use a version which
will work on all (NT4.0, Win2000, WinXP).
 
Back
Top