Solved

how to get a length of string with DOS commands

Posted on 2009-05-14
6
405 Views
Last Modified: 2013-11-29
In c, there is  a function called strlen(str1). How to get a string length in DOS commands. For example, str1= This is a test.
The length of str1 is 16.
0
Comment
Question by:jl66
  • 3
  • 2
6 Comments
 
LVL 82

Accepted Solution

by:
oBdA earned 260 total points
Comment Utility
See the subroutine below.
You'll need the "setlocal enabledelayedexpansion" at the beginning of your script, and the function at the end.
Add whatever you want inbetween.

@echo off

setlocal enabledelayedexpansion

set Var=This is a test.

call :StrLen VarLength "%Var%"

echo Length of "%Var%": %VarLength%
 

REM ----------------------------------------

REM Function StrLen

REM %1: Variable name in which to return the length of the string

REM %2: The string

REM Returns: Length of the string in the variable passed as %1

goto :eof

:StrLen

if "%~2"=="" (set %1=0 & goto :eof)

set /a i = 0 & set _TempStr_=%~2

:LengthLoop

if "!_TempStr_:~%i%,1!"=="" (set %1=%i% & goto :eof) else (set /a i+=1 & goto :LengthLoop)

REM ----------------------------------------

Open in new window

0
 
LVL 21

Assisted Solution

by:AmazingTech
AmazingTech earned 240 total points
Comment Utility
You can do it like this too.
@echo off

Set str1= This is a test.
 

Set Length=

for /l %%a in (0,1,30) do if "!str1:~%%a,1!" == "" if not defined Length Set Length=%%a
 

ECHO The length of str1 is %Length%.

Open in new window

0
 
LVL 16

Expert Comment

by:t0t0
Comment Utility
AmazingTech

You forgot to expand your variables...

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
Set str1= This is a test.
 
Set Length=
for /l %%a in (0,1,30) do if "!str1:~%%a,1!" == "" if not defined Length Set Length=%%a
 
ECHO The length of str1 is %Length%.
0
Too many email signature changes to deal with?

Are you constantly being asked to update your organization's email signatures? Do they take up too much of your time? Wouldn't you love to be able to manage all signatures from one central location, easily design them and deploy them quickly to users. Well, you can!

 
LVL 16

Expert Comment

by:t0t0
Comment Utility
@echo off
setlocal enabledelayedexpansion

set string=This is a string of characters

set /a i=0
if defined string (
   :loop
   if not "!string:~%i%,1!"=="" (
      set /a i+=1
      goto :loop
   )
)

echo %i%
0
 
LVL 21

Expert Comment

by:AmazingTech
Comment Utility
Oh yes you're right t0t0.

I was unfortunately playing around in a command prompt started with /v:on.
0
 
LVL 16

Expert Comment

by:t0t0
Comment Utility
i understand AmazingTech..... I often do it myself. And the biggest forgetter for me is the '/a' in set.

I could have swore my last example would have done the trick just nicely! Oh well, for the record,  heres a 'condensed' version....

@echo off
setlocal enabledelayedexpansion
set string=This is a string of characters

set /a i=0 & if defined string (
   :loop
   if not "!string:~%i%,1!"=="" set /a i+=1 & goto :loop
)
echo %i%

You win some, you lose some.....
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Sometimes drives fill up and we don't know why.  If you don't understand the best way to use the tools available, you may end up being stumped as to why your drive says it's not full when you have no space left!  Here's how you can find out...
If you get continual lockouts after changing your Active Directory password, there are several possible reasons.  Two of the most common are using other devices to access your email and stored passwords in the credential manager of windows.
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
This Micro Tutorial will give you a basic overview of Windows DVD Burner through its features and interface. This will be demonstrated using Windows 7 operating system.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now