0

I haven't done one of these in a while and I'd appreciate the help.

here's the sample code:

:beam_type_toggle

if %beam_type%==E (set beam_type=X)

if %beam_type%==X (set beam_type=E)

goto menu

(:menu is the main section it goes back to)

I'm sure you can see what I'm trying to do. I want it to simply change the "E" and "X" whenever this section is executed. I thought this would be nice and simple and it probably is but it just doesn't do it and I can't be bothered to read through Microsoft's documentation. This is just a small thing I need to get done quickly. Thanks for your help

I tried putting the values in and out of quotations but it did nothing

Marc
  • 11,403
  • 2
  • 35
  • 45
Nick
  • 1
  • 1
  • 4
    if there can only be two states: `if %beam_type%==E (set beam_type=X) else (set beam_type=E)` – Stephan Mar 06 '23 at 20:25
  • 1
    How could `beam_type` ever NOT be `E`? When it starts out as `E`, you set it to `X`, which causes it to then get set back to `E`. If starts out as `X`, the second `if` statement sets it to `E`. So after your logic, it's _always_ `E`. – Marc Mar 06 '23 at 20:29
  • `for %%s in (E X) do if "%beam_type%" neq "%%s" set "beam_type=%%s"&goto menu` – Magoo Mar 06 '23 at 23:44

1 Answers1

0

You can easily make your code work by using the quirks of delayed expansion:

:beam_type_toggle
(
if %beam_type%==E (set beam_type=X)
if %beam_type%==X (set beam_type=E)
)
goto menu

(by the way: @Magoo's suggestion also uses this delayed expansion quirk:

for %%s in (E X) do if "%beam_type%" neq "%%s" set "beam_type=%%s"

)

But there is a better way - given a "toggle" that only has two defined states:

if "%beam_type%"=="E" (set "beam_type=X") else (set "beam_type=E")

Bonus: if the variable has any other (or no) value, it forces it to one of the two allowed values (E in this example)

Stephan
  • 53,940
  • 10
  • 58
  • 91