Counting Characters

  • Thread starter Thread starter Barney
  • Start date Start date
B

Barney

How can I determine the number of characters in an
environment variable (%ComputerName%)?
 
Barney said:
How can I determine the number of characters in an
environment variable (%ComputerName%)?

Hello Barney, one way is to append a char unlikely to occur in the var
to a copy, strip 1st char until your marker found:

set cnt=1
set chk=%computername%¬
:loop
set chk=%chk:~1%
if not "%chk%"=="¬" Set /A cnt+=1 & goto :loop
echo/Computername: %computername% length:%cnt%

HTH
 
Matthias Tacke said:
Hello Barney, one way is to append a char unlikely to occur in the var
to a copy, strip 1st char until your marker found:

set cnt=1
set chk=%computername%¬
:loop
set chk=%chk:~1%
if not "%chk%"=="¬" Set /A cnt+=1 & goto :loop
echo/Computername: %computername% length:%cnt%

HTH

Oops,
in the unlikely case of an empty computername or for general use, this
is correct:

set cnt=0
set chk=%computername%¬
:loop
if not "%chk%"=="¬" Set /A cnt+=1 & set chk=%chk:~1% & goto :loop
echo/Computername: %computername% length:%cnt%
 
Hello Barney, one way is to append a char unlikely to occur in the var
to a copy, strip 1st char until your marker found:

set cnt=1
set chk=%computername%¬
:loop
set chk=%chk:~1%
if not "%chk%"=="¬" Set /A cnt+=1 & goto :loop
echo/Computername: %computername% length:%cnt%

HTH

No need to append a character, though it needs an added call.

@echo off
set computername=12345678
set cnt=0
:loop
call set chk=%%computername:~%cnt%,1%%
if defined chk Set /A cnt+=1 & goto :loop
echo/Computername: %computername% length:%cnt%
 
How can I determine the number of characters in an
environment variable (%ComputerName%)?

Use the $LEN function of the (FREE) Advanced NT/2K/XP/K3 Command Library.

CALL ntlib $LEN ComputerName

*******
(http://TheSystemGuard.com/NTCmdLib/Functions/LEN.htm)
(http://ntlib.com)
(http://TheSystemGuard.com/MasterCatalog.asp)
*******

-tsg

/-----------------+---------------+----------------------\
| COMPATIBILITY | CLARITY | SPEED |
| Write code ONCE | Make it clear | THEN...Make it fast! |
\-----------------+---------------+----------------------/
400+ command-line resources using ONLY native NT commands!
(http://TheSystemGuard.com/default.asp#MasterCommandList)
 
Matthias Tacke said:
Oops,
in the unlikely case of an empty computername or for general use, this
is correct:

set cnt=0
set chk=%computername%¬
:loop
if not "%chk%"=="¬" Set /A cnt+=1 & set chk=%chk:~1% & goto :loop
echo/Computername: %computername% length:%cnt%
I know, never post before testing, but did it myself. The expression
of a set without /A should be quoted to avoid an endless loop in this
case.

set cnt=0
set chk=%computername%¬
:loop
if not "%chk%"=="¬" Set /A cnt+=1 & set "chk=%chk:~1%" & goto :loop
echo/Computername: %computername% length:%cnt%

foxidrive is right with the marker and a low count makes the extra
calls acceptable. In a loop the extra load may be a different thing.
 
Very nice. Thank you.
-----Original Message-----


No need to append a character, though it needs an added call.

@echo off
set computername=12345678
set cnt=0
:loop
call set chk=%%computername:~%cnt%,1%%
if defined chk Set /A cnt+=1 & goto :loop
echo/Computername: %computername% length:%cnt%
.
 
Barney said:
How can I determine the number of characters in an
environment variable (%ComputerName%)?

Here's another approach:

- - - - - - - - - - begin screen capture - - - - - - - - - -
<Win2000> c:\cmd>ruler 50&echo the quick brown fox jumps over the lazy dog.
.....+....1....+....2....+....3....+....4....+....5
the quick brown fox jumps over the lazy dog.

<Win2000> c:\cmd>length the quick brown fox jumps over the lazy dog.

<Win2000> c:\cmd>set length
length=44

<Win2000> c:\cmd>rlist util\length.cmd
=====begin c:\cmd\util\length.cmd ====================
1. @echo off
2. echo:%*>$rt5$.bak
3. for %%a in ($rt5$.bak) do set /a length=%%~za-2
4. del $rt5$.bak
=====end c:\cmd\util\length.cmd ====================
- - - - - - - - - - end screen capture - - - - - - - - - -
 
Back
Top