How do I use the :StartsWith function?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
chuntuk
Posts: 4
Joined: 07 Aug 2012 06:58

How do I use the :StartsWith function?

#1 Post by chuntuk » 07 Aug 2012 07:09

I'd like to use the :StartsWith function included on this site at

http://www.dostips.com/DtTipsStringOper ... StartsWith

but it isn't obvious to me how I get hold of the results of the function. It appears that if the passed string doesn't start with the passed characters, it'll do this:

set=2>NUL

Which isn't like the way any of the other functions pass back their results.

Could somebody provide me with a code fragment showing how this function is actually used?

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: How do I use the :StartsWith function?

#2 Post by Ed Dyreen » 07 Aug 2012 07:59

'

Code: Select all

@echo off &setlocal enableDelayedExpansion

set=
echo.%errorLevel%

pause
exit

Code: Select all

De syntaxis van de opdracht is onjuist.
1
Druk op een toets om door te gaan. . .

Code: Select all

@echo off &setlocal enableDelayedExpansion

call :StartsWith "hello world !" "hello" &&echo.it does ||echo.it doesn't
call :StartsWith "hello world !" "world" &&echo.it does ||echo.it doesn't

pause
exit


:StartsWith text string -- Tests if a text starts with a given string
::                      -- [IN] text   - text to be searched
::                      -- [IN] string - string to be tested for
:$created 20080320 :$changed 20080320 :$categories StringOperation,Condition
:$source http://www.dostips.com
SETLOCAL
set "txt=%~1"
set "str=%~2"
if defined str call set "s=%str%%%txt:*%str%=%%"
if /i "%txt%" NEQ "%s%" set=2>NUL
EXIT /b
an alternative solution that may be more obvious

Code: Select all

@echo off &setlocal enableDelayedExpansion

call :StartsWith "hello world !" "hello" &&echo.it does ||echo.it doesn't
call :StartsWith "hello world !" "world" &&echo.it does ||echo.it doesn't

pause
exit


:StartsWith text string -- Tests if a text starts with a given string
::                      -- [IN] text   - text to be searched
::                      -- [IN] string - string to be tested for
:$created 20080320 :$changed 20080320 :$categories StringOperation,Condition
:$source http://www.dostips.com
SETLOCAL
set "txt=%~1"
set "str=%~2"
if defined str call set "s=%str%%%txt:*%str%=%%"
if /i "%txt%" NEQ "%s%" set /a $err = 1
EXIT /b %$err%

Code: Select all

it does
it doesn't
Druk op een toets om door te gaan. . .

chuntuk
Posts: 4
Joined: 07 Aug 2012 06:58

Re: How do I use the :StartsWith function?

#3 Post by chuntuk » 07 Aug 2012 09:04

How would I do something along the lines of

IF {startswith expression here} (
echo yes it does
) else (
echo no it doesn't
)

Squashman
Expert
Posts: 4470
Joined: 23 Dec 2011 13:59

Re: How do I use the :StartsWith function?

#4 Post by Squashman » 07 Aug 2012 09:12

chuntuk wrote:How would I do something along the lines of

IF {startswith expression here} (
echo yes it does
) else (
echo no it doesn't
)

You use the last set of CODE that ED gave you.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How do I use the :StartsWith function?

#5 Post by foxidrive » 07 Aug 2012 11:26

Code: Select all

@echo off
SETLOCAL
set "txt=%~1"
set "str=%~2"
if defined str call set "s=%str%%%txt:*%str%=%%"
if /i "%txt%" NEQ "%s%" (
echo no it don't
) else (
echo yes it do
)



d:\>batch.bat "hello world" "hello"
yes it do

d:\>batch.bat "hello world" "woof"
no it don't

chuntuk
Posts: 4
Joined: 07 Aug 2012 06:58

Re: How do I use the :StartsWith function?

#6 Post by chuntuk » 08 Aug 2012 03:14

@Squashman

The "last bit of code that ED gave me" calls the function like this:

Code: Select all

call :StartsWith "hello world !" "hello" &&echo.it does ||echo.it doesn't
call :StartsWith "hello world !" "world" &&echo.it does ||echo.it doesn't

Hence my question about incorporating it into an IF statement.

@foxidrive
Thanks, but that's just unpicking the function and doing it inline. I'd like to keep this functionality wrapped up in a function so I can call it where I need it.

However, after a little experimentation, I figured out how to do it (thanks to Ed for the clues):

Code: Select all

@echo off &setlocal enableDelayedExpansion

call :StartsWith "hello world !" "hello"
if %errorLevel%==0 (
   echo.it does
) else (
   echo.it doesn't
)


pause
exit


:StartsWith text string -- Tests if a text starts with a given string
::                      -- [IN] text   - text to be searched
::                      -- [IN] string - string to be tested for
:$created 20080320 :$changed 20080320 :$categories StringOperation,Condition
:$source http://www.dostips.com
SETLOCAL
set "txt=%~1"
set "str=%~2"
if defined str call set "s=%str%%%txt:*%str%=%%"
if /i "%txt%" NEQ "%s%" set=2>NUL
EXIT /b

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: How do I use the :StartsWith function?

#7 Post by Ed Dyreen » 08 Aug 2012 03:47

'
Why compare the errorlevel ?

Code: Select all

@echo off &setlocal enableDelayedExpansion

call :StartsWith "hello world !" "hello" &if not errorlevel 1 (
       echo.it does
) else (
       echo.it doesn't
)

pause
exit
But I would prefer

Code: Select all

@echo off &setlocal enableDelayedExpansion

call :StartsWith "hello world !" "hello" &&(
       echo.it does
) ||(
       echo.it doesn't
)

pause
exit

Squashman
Expert
Posts: 4470
Joined: 23 Dec 2011 13:59

Re: How do I use the :StartsWith function?

#8 Post by Squashman » 08 Aug 2012 05:52

chuntuk wrote:@Squashman

The "last bit of code that ED gave me" calls the function like this:

Code: Select all

call :StartsWith "hello world !" "hello" &&echo.it does ||echo.it doesn't
call :StartsWith "hello world !" "world" &&echo.it does ||echo.it doesn't

Hence my question about incorporating it into an IF statement.


Because ED's code is more efficient. It is basically saying if this is TRUE then do this and IF it isn't TRUE then do this. That is what the Double Ampersand and Double PIPE are for. And as ED said why bother checking the errorlevel.
http://judago.webs.com/batchoperators.htm

chuntuk
Posts: 4
Joined: 07 Aug 2012 06:58

Re: How do I use the :StartsWith function?

#9 Post by chuntuk » 08 Aug 2012 09:01

In 20 years of working as a developer, this is the first time I've had to write a DOS batch file (other than the most trivial tweaking of AUTOEXEC.BAT back in the day). It may be many more years before I have to tangle with one again. Whoever has to maintain my script in the future will probably be equally unfamiliar with DOS scripting.

That being the case, I want to use syntax which is simple and obvious to non DOS scripters. Anyone who has tangled with any kind of computing will understand the IF/ELSE construct. The && || approach is a little more obscure, even if it saves a few keystrokes and the odd clock cycle.

Thanks for that operators link, though. I could have made good use of that if I'd found it yesterday!

Squashman
Expert
Posts: 4470
Joined: 23 Dec 2011 13:59

Re: How do I use the :StartsWith function?

#10 Post by Squashman » 08 Aug 2012 09:12

Then Document your code inside the batch file to explain what it is doing.

I have a batch file that I have maintained for our group for many years. It started as a very small 10 line batch file that did nothing but Automatically zip all files in a directory. This batch file has evolved into much much more over the years and I have comments before each chunk of code explaining how it works.

Post Reply