How do I use the :StartsWith function?
Moderator: DosItHelp
How do I use the :StartsWith function?
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?
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?
Re: How do I use the :StartsWith function?
'
an alternative solution that may be more obvious
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
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. . .
Re: How do I use the :StartsWith function?
How would I do something along the lines of
IF {startswith expression here} (
echo yes it does
) else (
echo no it doesn't
)
IF {startswith expression here} (
echo yes it does
) else (
echo no it doesn't
)
Re: How do I use the :StartsWith function?
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.
Re: How do I use the :StartsWith function?
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
Re: How do I use the :StartsWith function?
@Squashman
The "last bit of code that ED gave me" calls the function like this:
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):
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
Re: How do I use the :StartsWith function?
'
Why compare the errorlevel ?But I would prefer
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
Code: Select all
@echo off &setlocal enableDelayedExpansion
call :StartsWith "hello world !" "hello" &&(
echo.it does
) ||(
echo.it doesn't
)
pause
exit
Re: How do I use the :StartsWith function?
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
Re: How do I use the :StartsWith function?
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!
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!
Re: How do I use the :StartsWith function?
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.
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.