0

I require another evaluation on my code

if %programtorun% != "" (
    echo test
    pause
) else if %foundbootable% (
    set oserrorcode=1
) else (
   set computererrorcode=2
   goto error
)
if %programtorun% == init.lua (
   set foundbootable=true
)

I do not know what I'm doing wrong. If you need more details, please ask.

6
  • 6
    you should use NEQ instead of != . Also when comparing strings quotes are also compared so there's a chance that you should use "%programtorun%"
    – npocmaka
    Commented May 16, 2018 at 13:06
  • 2
    You haven't indicated what the original content of foundbootable is.
    – Magoo
    Commented May 16, 2018 at 13:13
  • 2
    if false ( is invalid syntax. The required syntax is if string1 op string2 where the operator may be one of == equ neq lss gtr leq geq and the strings need to be quoted if they contain spaces or other separators. If each string is purely numeric then the comparison will be made on the basis of magnitude. It's also possible to use if defined varname which will be true if varname is an existing environment variable (one that has a value assigned to it)
    – Magoo
    Commented May 16, 2018 at 13:45
  • 2
    @CoffeeGamer, did you even attempt to read the help file for the IF command? There is not a single reference to use != as a comparison operator.
    – Squashman
    Commented May 16, 2018 at 13:49
  • 1
    run if /? and see
    – phuclv
    Commented May 16, 2018 at 13:49

1 Answer 1

4

You can not use != in batch files. Second thing to watch out for are spaces.

!= is rewritten as IF NOT <first>==<second> (also NEQ could be used, that is a second option but NOT has been longer around and does not need anything extra to work).

I have taken the liberty to rewrite your code, now it should work for you:

if NOT "%programtorun%"=="" (
    echo test
    pause
) else if "%foundbootable%"=="false" (
   set "oserrorcode=1"
) else (
    set "computererrorcode=2"
    goto :error
)
if "%programtorun%"=="init.lua" (
   set "foundbootable=true"
)
2
  • 4
    if "%foundbootable%" is not valid syntax, it must read if "%foundbootable%"=="false"! Consider to use switch /I for case-insensitive comparison...
    – aschipfl
    Commented May 16, 2018 at 13:48
  • 1
    @aschipfl true, missed that. Will fix it, thank you. I don't know the value for that. The OP would have to add that to the question.
    – tukan
    Commented May 16, 2018 at 14:30