String Comparison
Moderator: DosItHelp
-
- Posts: 6
- Joined: 26 Jan 2011 16:19
String Comparison
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.
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
Hi flexbuffchest,
you can try something like this
hope it helps
jeb
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
-
- Posts: 6
- Joined: 26 Jan 2011 16:19
Re: String Comparison
jeb wrote:Hi flexbuffchest,
you can try something like thisCode: 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 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
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
Re: String Comparison
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.
Regards
aGerman
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
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.
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
@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
Yeah jeb, I also thought about that solution I learned from you. But finally I decided to FINDSTR because it's shorter
Regards
aGerman
Regards
aGerman
Re: String Comparison
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):
If you need case insensitive then inverting aGerman's findstr solution make's it more manageable:
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
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
Oops! In my 2nd option I forgot to remove the echo from the code I used for testing.
Here it is corrected
Needless to say, this option can be made case sensitive by removing the /i option from the findstr command
DBenham
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