Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows Batch check if variable starts with, ends with and contains a specific string

I'm trying to check if a variable in a batch file starts with " contains BETA somewhere and ends with ").

Is it possible? And if yes, may somebody help me with that?

like image 507
BrainStone Avatar asked Mar 29 '13 16:03

BrainStone


People also ask

What does %% do in batch?

Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.

What is %% K in batch file?

So %%k refers to the value of the 3rd token, which is what is returned.

What is %% P in batch file?

The /P switch allows you to set the value of a variable to a line of input entered by the user. Displays the specified promptString before reading the line of input. The promptString can be empty.

What does %% A mean?

%%a refers to the name of the variable your for loop will write to. Quoted from for /? : FOR %variable IN (set) DO command [command-parameters] %variable Specifies a single letter replaceable parameter.


1 Answers

Assuming your variable does not contain any line feeds or carriage returns, then all that is needed is a single FINDSTR command. It has limited regular expression support that is more than enough for this problem.

@echo off
setlocal
set test1=abc
set test2="abc"
set test3="")
set test4=")"
set test5=abc")"
set test6=xyzBETAzyx
set test7="xyz BETA zyx"
set test8=xyzBETAzyx")"
set test9=xyzBETAzyx")"
set test10="xyzbetazyx")
set test11="xyzBETAzyx")

for /l %%N in (1 1 11) do call :test test%%N
exit /b

:test  variableName
setlocal enableDelayedExpansion
echo !%1!|>nul findstr /rx \".*BETA.*\") && set "result=PASS" || set "result=FAIL"
echo %1 = !result!: !%1!
exit /b

Only TEST11 passes.

The /R option forces the search string to be treated as a regular expression.

The /X option means it must be an exact match - the entire string must match the search string.

.* is the regex wildcard expression that will match any string of characters.

I redirect the FINDSTR output to NUL because we don't need to see the line. We just want the return code

FINDSTR requires double quote literals to be escaped as \"

Use delayed expansion so that you can safely echo any value within the variable.

I use && to conditionally take action when FINDSTR succeeded, and || to conditionally take action when FINDSTR failed.

My :TEST routine takes the name of the variable as the sole argument.

Note that in some situations, the batch parser requires that you also escape the double quote as ^", meaning a fully escaped quote will look like \^". But that complication is not needed in your case. The FINDSTR escape rules are actually a bit wonky. See What are the undocumented features and limitations of the Windows FINDSTR command? for more info.

My test code does a case sensitive search. So beta will not match BETA. If you want a case insensitive search, then simply add the /I option.

EDIT: 2015-07-22

Another option, if and only if you want to ignore case, is to use a combination of variable expansion search/replace and expansion substring operations. Both test10 and test11 pass below.

@echo off
setlocal
set test1=abc
set test2="abc"
set test3="")
set test4=")"
set test5=abc")"
set test6=xyzBETAzyx
set test7="xyz BETA zyx"
set test8=xyzBETAzyx")"
set test9=xyzBETAzyx")"
set test10="xyzbetazyx")
set test11="xyzBETAzyx")

for /l %%N in (1 1 11) do call :test test%%N
exit /b

:test  variableName
setlocal enableDelayedExpansion
set "result=FAIL"
if "!%1:beta=!" neq "!%1!" if "!%1:~0,1!" equ ^""" if "!%1:~-2!" equ ^"")" set "result=PASS"
echo %1 = !result!: !%1!
exit /b

The first IF checks for beta anywhere (case insensitive). The second IF checks for " at the beginning, and the last IF checks for ") at the end. The only tricky thing is figuring out how to escape the comparison string when it has an odd number of quotes.

like image 174
dbenham Avatar answered Sep 22 '22 22:09

dbenham