Solved

how to get a length of string with DOS commands

Posted on 2009-05-14
6
407 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 83

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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

In this article, I will show you HOW TO: Install VMware Tools for Windows on a VMware Windows virtual machine on a VMware vSphere Hypervisor 6.5 (ESXi 6.5) Host Server, using the VMware Host Client. The virtual machine has Windows Server 2016 instal…
How to record audio from input sources to your PC – connected devices, connected preamp to record vinyl discs, streaming media, that play through your audio card: Vista, Windows 7, Windows 8, Windows 8.1 and Windows 10 – both 32 bit & 64.
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

813 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