Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

how to get a length of string with DOS commands

Posted on 2009-05-14
6
Medium Priority
?
415 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 85

Accepted Solution

by:
oBdA earned 1040 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 960 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
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Windows 10 Creator Update has just been released and I have it working very well on my laptop. Read below for issues, fixes and ideas.
By default Outlook 2016 displays only one time zone in the Calendar. The following article explains how to display two time zones in one calendar view.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

971 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