Solved

how to get a length of string with DOS commands

Posted on 2009-05-14
6
412 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 84

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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

While rebooting windows server 2003 server , it's showing "active directory rebuilding indices please wait" at startup. It took a little while for this process to complete and once we logged on not all the services were started so another reboot is …
In this article, I will show you HOW TO: Perform a Physical to Virtual (P2V) Conversion the easy way from a computer backup (image).
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…

751 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