MS-DOS 8.0: Determine String Length?

10,062

Solution 1

From here I got and example and this is it cleaned up a litte

@echo off
setlocal
set #=%1
set length=0
:loop
if defined # (set #=%#:~1%&set /A length += 1&goto loop)
echo %1 is %length% characters long!
endlocal

alt text

Solution 2

Here's another option. Pass the string as a paramter like this:

   LEN "this is a long string"

Here's the code:

   @echo off

   echo.%~1>len
   for %%a in (len) do set /a len=%%~za -2

   echo %len%

Copy and paste the code into Notepad and save it as LEN.BAT.

NOTE: The fullstop following the ECHO statement is vital should a NUL string be entered. The -2 is required because ECHO automatically adds a CR & LF at the end of each line.

Solution 3

I regularly use the following method:

   @echo off
   set str=This is a line of text

   echo.%str%>len
   for %%a in (len) do set /a len=%%~za -2

   echo %len%
Share:
10,062
JustADude
Author by

JustADude

Barbarian coder, but trying to refine.

Updated on June 07, 2022

Comments

  • JustADude
    JustADude almost 2 years

    Using MS-DOS 8.0, what is the best way to determine the length of a string?

    I looked through Computer Hope (http://www.computerhope.com/msdos.htm), but no commands jumped out at me...

    Is there a built in command or must a function be built to address this?

    Thanks a ton.

  • JamesUsedHarden
    JamesUsedHarden over 13 years
    Great example of why we should never script in DOS. :-)