1.

Solve : OR operator?

Answer»

I've searched a lot for this but I can't find the answer. What I want to do is the following (doesn't work otherwise I wouldn't have opened a new topic  )

Code: [Select]IF "%1" == "help" OR "%1" == "--help" OR "%1" == "-help" (
ECHO:HELP FILE
GOTO :EOF
)
If this is something mentioned, please point me to that topic. Thanks in advance.I am presuming you mean NT/W2K/XP/Vista/W7 cmd.exe - there is no OR operator in batch language, so you have to build the test manually. Here is an example:

Code: [Select]set bool=0
IF "%1"=="help" set /a bool+=1
IF "%1"=="--help" set /a bool+=1
IF "%1"=="-help" set /a bool+=1

IF %bool% GTR 0 (
    ECHO:HELP FILE
    GOTO :EOF
)


alternative format for incrementing %bool%

Code: [Select]set /a bool=%bool%+1This is exactly what I was looking for! THANK you, it makes a lot of sense the way you do it That code above will work with "help" or "--help" or "-help" but would fail with HELP or --HELP or -HELP so if you want the string tests to be case insensitive use IF with the /I (or /i) switch


Code: [Select]set bool=0
IF /I "%1"=="help" set /a bool+=1
IF /I "%1"=="--help" set /a bool+=1
IF /I "%1"=="-help" set /a bool+=1

IF %bool% GTR 0 (
    ECHO:HELP FILE
    GOTO :EOF
)
Yep, I know I already use the /I switch. An alternative OR implemetation

Code: [Select]IF /I "%1"=="help" goto help
IF /I "%1"=="--help" goto help
IF /I "%1"=="-help" goto help

REM not asking for help
REM main code here
goto end

:help
echo Usage syntax
echo bla bla bla

:end
REM end of script

Thanks for the help, both ways work perfectly.

Now a small problem I've come across:

Code: [Select]ECHO OFF
CALL :SUBMESSAGE "first argument" "second argument"
PAUSE
GOTO :EOF

:MSG
ECHO. && ECHO:______________________________
IF /I %~2=="" (
  SET %2=Failed!
)
ECHO:[%~1] %~2
ECHO:______________________________ && ECHO.
IF /I %1=="error" (
  PAUSE
  EXIT
)

GOTO :EOF
I cannot get it work if I specify only one argument... It's related to quotes but it's still driving me crazy

Thank you in advance.I'm not surprised you can't get it to work...

Code: [Select]CALL :SUBMESSAGE "first argument" "second argument"
Where is the :SUBMESSAGE label?

Code: [Select]IF /I %~2==""
Since %~2 is the %2 parameter without (any) SURROUNDING quotes, this equality will never be satisfied.

Code: [Select]SET %2=Failed!
I don't know what you think this is going to do...




:MSG = :SUBMESSAGE, just a typo

I simply want to use a SUBROUTINE to display some messages based on the input. But the input arguments will always have quotes.

Code: [Select]ECHO OFF
CALL :SUBMESSAGE "first argument" "second argument"
PAUSE
GOTO :EOF

:MSG
ECHO. && ECHO:______________________________
IF /I %~2=="" (
  SET %2=Failed!
)
ECHO:[%~1] %~2
ECHO:______________________________ && ECHO.
IF /I %1=="error" (
  PAUSE
  EXIT
)

GOTO :EOFDid you even read my POST?
Of course I did. Well a solution would be to always use quotes and always specify %2.
Anyway, thanks. Quote from: Chem4 on November 09, 2010, 03:16:21 AM

Of course I did. Well a solution would be to always use quotes and always specify %2.
Anyway, thanks.

if you want to know if %2 is NULL (blank) then you need to do

if "%~2"=="" then [whatever]

or

if {%~2}=={} then [whatever]

or you can use any non-control character as the prefix and suffix.





Unlike some programming languages this "" is not an empty string in batch. It is a string of length 2 characters.

And you cannot do this

SET %2=Failed!



Discussion

No Comment Found