Solved

how to get a length of string with DOS commands

Posted on 2009-05-14
6
413 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 85

Accepted Solution

by:
oBdA earned 260 total points
ID: 24388802
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
ID: 24389142
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
ID: 24390784
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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
LVL 16

Expert Comment

by:t0t0
ID: 24391356
@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
ID: 24393210
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
ID: 24393764
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is a collection of issues that people face from time to time and possible solutions to those issues. I hope you enjoy reading it.
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

635 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