C:\_G\WWW\~ELISANET\INFO\tscmd026.html
<http://www.elisanet.fi/tsalmi/info/tscmd026.html>
Copyright © 2003- by Prof. Timo Salmi  
Last modified Fri 12-Oct-2018 02:41:08

 
Assorted NT/2000/XP/.. CMD.EXE Script Tricks
From the html version of the tscmd.zip 1cmdfaq.txt file
To the Description and the Index
 

This page is edited from the 1cmdfaq.txt faq-file contained in my tscmd.zip command line interface (CLI) collection. That zipped file has much additional material, including a number of detached .cmd script files. It is recommended that you also get the zipped version as a companion.

Please see "The Description and the Index page" for the conditions of usage and other such information.



26} How do I get the length of a string into a variable?

There are many variations to get a string's length
  @echo off & setlocal enableextensions
  set testString_=This is a test string
  call :GetStrLength "%testString_%" length_
  echo 123456789 123456789 123456789
  echo.%testString_%
  echo Length = %length_%
  goto :EOF
  endlocal & goto :EOF
  ::
  ::===============================================================
  :: Subroutine: Return the length of a string
  :GetStrLength string count
  setlocal enableextensions enabledelayedexpansion
  if "%~1"=="" (endlocal & set "%2=0" & goto :EOF)
  set s=%~1
  for /L %%c in (0,1,255) do (
    set si=!s:~%%c,1!
    if defined si set charCount=%%c
    )
  set /a charCount+=1
  endlocal & set "%2=%charCount%" & goto :EOF

The output is
  C:\_D\TEST>cmdfaq
  123456789 123456789 123456789
  This is a test string
  Length = 21
If there are leading or trailing blanks in the string they are included, as they logically should.

Alternatively
  @echo off & setlocal enableextensions enabledelayedexpansion
  ::
  :: Get a test string
  if "%~1"=="" (
    set s=This is a test string
    ) else (
    set s=%*
    )
  ::
  :: Get the length of the quoted string assuming a max of 255
  set charCount=0
  for /l %%c in (0,1,255) do (
    set si=!s:~%%c!
    if defined si set /a charCount+=1)
  if %charCount% EQU 256 set charCount=0
  echo The length of "%s%" is %charCount% characters
  endlocal & goto :EOF

The output could be e.g.
  C:\_D\TEST>cmdfaq Hello World!
  The length of "Hello World" is 11 characters
Note the (undesirable) effect of the ! character. The length of "Hello World!" is 12 characters.
As for %* "the %* batch parameter is a wildcard reference to all the arguments, not including %0, that are passed to the batch file".

A Visual Basic Script (VBScript) aided command line script solution
  @echo off & setlocal enableextensions disabledelayedexpansion
  set testString_=This is a test string!
  call :GetStrLength "%testString_%" length_
  echo 123456789 123456789 123456789
  echo.%testString_%
  echo Length = %length_%
  goto :EOF
  endlocal & goto :EOF
  ::
  ::===============================================================
  :: Subroutine: Return the length of a string using VBScript
  :GetStrLength
  setlocal enableextensions
  >"%temp%\tmp$$$.vbs" echo WScript.Echo Len("%~1")
  for /f "tokens=* delims=" %%a in (
    'cscript //nologo "%temp%\tmp$$$.vbs"') do set return_=%%a
  for %%f in ("%temp%\tmp$$$.vbs") do if exist %%f del %%f
  endlocal & set "%~2=%return_%" & goto :EOF

The output is
  C:\_D\TEST>cmdfaq
  123456789 123456789 123456789
  This is a test string!
  Length = 22

One method for getting the length of the string is to truncate a copy of the string one character at a time (from the front) until the copy becomes undefined, while at the same time keeping score of the number of steps required. The possibility of an already empty string must be taken into account.
  @echo off
  setlocal enableextensions
  set testString_=This is a test string!
  call :GetStrLength %testString_%
  echo 123456789 123456789 123456789
  echo.%testString_%
  echo Length = %length_%
  goto :EOF
  ::
  ::===============================================================
  :GetStrLength
  setlocal enableextensions
    set rest_=%*
    set /a count_=0
    if not defined rest_ (set /a count_=-1 & set rest_=X)
    :_loop
      set /a count_+=1
      set rest_=%rest_:~1%
      if defined rest_ goto :_loop
  endlocal & set length_=%count_% & goto :EOF

C:\_D\TEST>cmdfaq
123456789 123456789 123456789
This is a test string!
Length = 22

Yet another option is to write the string into a temporary file and get that file's size (minus 2).
  @echo off & setlocal enableextensions
  set testString_=This is a test string!
  echo %testString_%>"%temp%\length.tmp"
  for %%f in ("%temp%\length.tmp") do set length_=%%~zf
  for %%f in ("%temp%\length.tmp") do if exist %%f del %%f
  set /a length_ = %length_% - 2
  echo The length of "%testString_%" is %length_%
  endlocal & goto :EOF

The output is
  D:\TEST>cmdfaq
  The length of "This is a test string!" is 22

Or
  @echo off & setlocal enableextensions
  set testString_=This is a test string!
  0>nul 1>"%temp%\cmdfaq.tmp" set /p var_="%testString_%"
  for %%f in ("%temp%\cmdfaq.tmp") do (
    set length_=%%~zf
    if exist %%f del %%f)
  echo The length of "%testString_%" is %length_%
  endlocal & goto :EOF

The output is again
  D:\TEST>cmdfaq
  The length of "This is a test string!" is 22

A G(nu)AWK solution to the problem is
  @echo off & setlocal enableextensions
  set testString_=This is a test string!
  if not exist c:\mytemp mkdir c:\mytemp
  echo %testString_%|unxgawk^
    "{printf\"@set length_=%%s\n\",length()}"^
    >c:\mytemp\length$$.cmd
  call c:\mytemp\length$$.cmd
  echo 123456789 123456789 123456789
  echo.%testString_%
  echo Length = %length_%
  set length_=
  del c:\mytemp\length$$.cmd
  rmdir c:\mytemp
  endlocal & goto :EOF

  C:\_D\TEST>cmdfaq
  123456789 123456789 123456789
  This is a test string!
  Length = 22

A FINDSTR option, based on Usenet postings in alt.msdos.batch.nt
  @echo off & setlocal enableextensions
  set testString_=This is a test string!
  for /f "tokens=1 delims=:" %%a in (
    '^(echo.%testString_%^&echo AnUncommonString^)^|
      findstr /o "AnUncommonString"'
    ) do set /a length_=%%a-3
  echo.%testString_%
  echo Length = %length_%
  endlocal & goto :EOF

  C:\_D\TEST>cmdfaq
  This is a test string!
  Length = 22

Another rendition of a FINDSTR solution
  @echo off & setlocal enableextensions
  set testString_=This is a test string!
  for /f "delims=:" %%a in ('
    ^(echo."%testString_%"^& echo.^)^|findstr /o .'
    ) do set /a length_=%%a-5
  echo The length of "%testString_%" is %length_%
  endlocal & goto :EOF

References/Comments: (If a Google message link fails try the links within the brackets.)
  Google Groups Nov 27 2004, 8:16 pm [M]
  Google Groups Nov 28 2004, 5:26 am [M]
  Google Groups 15 Jul 2009 11:44:35 -0700 (PDT) [M]
  Google Groups Nov 12 2009, 3:10 pm [M]
  Google Groups Nov 13 2009, 12:32 am [M]

[Previous] [Next]

C:\_G\WWW\~ELISANET\INFO\tscmd026.html
C:\_G\WWW\~ELISANET\FTPCMD\TSALMI.CMD /tscmd026
http://www.elisanet.fi/tsalmi/info/tscmd026.html
file:///c:/_g/www/~elisanet/info/tscmd026.html