Page 1 of 1

String Comparison

Posted: 26 Jan 2011 16:25
by flexbuffchest
Hello,

As part of a larger script I need to figure out how to use the string comparison to accept multiple values. I am doing this so that multiple different command options can be entered for the same thing. The basic structure that I have in my script right now is
if "%1"=="/version" goto :GetVersion
and, in addition to /version I want to add things such as /v, -v, etc. I know that I could easily just hard code all the different options but that seems like bad practice. I've spent a couple hours trying to figure this out, such as using a space between the different options, a comma, a semicolon but none seem to work. Any help would be much appreciated.

Re: String Comparison

Posted: 26 Jan 2011 17:15
by jeb
Hi flexbuffchest,

you can try something like this

Code: Select all

for %%T in ("/version" "/v" "-v") DO if "%~1" == "%%~T" goto :GetVersion


hope it helps
jeb

Re: String Comparison

Posted: 26 Jan 2011 17:40
by flexbuffchest
jeb wrote:Hi flexbuffchest,

you can try something like this

Code: Select all

for %%T in ("/version" "/v" "-v") DO if "%~1" == "%%~T" goto :GetVersion


hope it helps
jeb

Oh wow, it never crossed my mind to use a for loop :oops: Yes this fixed my problem perfectly. I feel kind of embarrassed of this oversight seeing as I used a bunch of for loops in the script I am building right now. Thanks a lot :D

Edit: Perhaps there is another thing you can help me with or point me in the right direction. I created a for loop that looks like this

Code: Select all

for %%a in ("-help" "/?" "-?" "/help" "/h") do if "%~1"=="%%~a" goto :Help
but when I try to use the command options of /? or -? they don't work. What I mean is that when I turn echo on and look at what is happening it is taking the argument variable of either /? or -? and comparing it to everything in the for loop, the only problem is that the /? or -? don't show up in the for loop when the script is ran. I thought this might be a case where I have to use the escape character, ^, but that didn't solve anything. Once again, any help would be much appreciated.

Re: String Comparison

Posted: 28 Jan 2011 13:07
by aGerman
Yeah, there is a small problem. The question mark is interpreted as wildcard inside a FOR loop. I also found no solution to escape it.
I would use FINDSTR to compare the first argument. But this is tricky as well.

Code: Select all

set "Param=%~1" &setlocal enabledelayedexpansion
echo "!Param!"|findstr /i /x "\"/help\" \"-help\" \"/?\" \"-?\" \"/h\" \"-h\"" >nul &&(
  set "Param=" &endlocal &goto :Help) ||(set "Param=" &endlocal)

Regards
aGerman

Re: String Comparison

Posted: 28 Jan 2011 14:59
by jeb
Ok, the "?" and "*" wildcards are a little bit complicated.

@german: Ok it's a solution but as you say, it seems to be a too tricky.

I would prefere an other way with FOR /F
It doesn't interpret the wildcards, but it doesn't take single parameters, it only takes lines.
A line is defined by a characters to a line feed, so you only have to add line feeds, to get the result.

Code: Select all

rem create a single linefeed
set LF=^


Rem TWO empty lines are neccessary for the LF

rem Now I create a variable which creates a single LF after expansion, it consists of three characters ^LFLF
set ^"nlf=^^^%lf%%lf%^%lf%%lf%"
for /F "delims=" %%a in (^""-help"%nlf%"/?"%nlf%"-?"%nlf%"/help"%nlf%"/h") do (
   echo( Sample %%a   '%%~a'
   if "%~1"=="%%~a" goto :Help
)


There is only one curious thing with the first parameter, "-help" expands only to -help" but ^""-help" expands to "-help".

The FOR /F <linefeed> could also be useful in some other situations.

hope it helps
jeb

Re: String Comparison

Posted: 28 Jan 2011 15:20
by aGerman
Yeah jeb, I also thought about that solution I learned from you. But finally I decided to FINDSTR because it's shorter :wink:

Regards
aGerman

Re: String Comparison

Posted: 12 Feb 2011 22:29
by dbenham
The problem is simplified if you prepare a delimited list of options, making sure to include the delimiter at both the beginning and end of the string.

If it's OK for the search to be case sensitive then the following works (expand delimited list as needed):

Code: Select all

setlocal enableDelayedExpansion
set helpOpts="|-help|/?|-?|\?|/h|\h|-h|"
if not %helpOpts%==!helpOpts:^|%~1^|=! endlocal & goto :Help
endlocal


If you need case insensitive then inverting aGerman's findstr solution make's it more manageable:

Code: Select all

setlocal
set parm="|%~1|"
set parm=%parm:\=\\%
echo "|-help|/?|-?|\?|/h|\h|-h|"|findstr /i %parm% >nul && (endlocal & echo goto :Help)
endlocal


The 2nd option is particularly nice because you need only prepare parm once in preparation for many tests.
The 1st option requires setting a variable before each test.

DBenham

Re: String Comparison

Posted: 12 Feb 2011 22:40
by dbenham
Oops! In my 2nd option I forgot to remove the echo from the code I used for testing.

Here it is corrected

Code: Select all

setlocal
set parm="|%~1|"
set parm=%parm:\=\\%
echo "|-help|/?|-?|\?|/h|\h|-h|"|findstr /i %parm% >nul && (endlocal & goto :Help)
endlocal


Needless to say, this option can be made case sensitive by removing the /i option from the findstr command

DBenham