#1 28 Feb 2018 15:05

stefano.gatto
Member
From: Geneva, Switzerland
Registered: 26 Nov 2014
Posts: 7

IF/ELSE with nested parenthesis

Hello, I need to implement a SELECT CASE logic and wonder whether I can use nested IFs. I did not find any special mention in ss64, whether nested parenthesis are allowed. Do you know why the interpreter refuses to execute this? (Windows2012R2 says "The syntax of the command is incorrect."):

________________________________________

if /I %1==dev
       (set ROUTERID=D0001EURKAM
       set TOADDRESSEES=EurydiceIT@lists.abc.com
       set CCADDRESSEES=EurydiceIT@lists.abc.com) 
else ( if /I %1==tst 
              (set ROUTERID=Q0001EURKAM
              set TOADDRESSEES=EurydiceUsers@lists.abc.com
              set CCADDRESSEES=EurydiceIT@lists.abc.com) 
       else ( if /I %1==stg
                     (set ROUTERID=Q0001EURKAM
                     set TOADDRESSEES=EurydiceUsers@lists.abc.com
                     set CCADDRESSEES=EurydiceIT@lists.abc.com) 
              else ( if /I %1==prd
                           (set ROUTERID=P0001EURKAM
                           set TOADDRESSEES=myself@abc.com
                           set CCADDRESSEES=EurydiceUsers@lists.abc.com,EurydiceIT@lists.abc.com) 
                     else (GOTO SyntaxError)
              )
       )
)

________________________________________

Many thanks!

Offline

#2 28 Feb 2018 16:14

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 998
Website

Re: IF/ELSE with nested parenthesis

When combining an ELSE statement with parenthesis, always put the parenthesis on the same line as ELSE.

 ) ELSE ( 

This is because CMD does a rather primitive one-line-at-a-time parsing of the command.

I have added this extra explanation to the IF page.

Offline

#3 28 Feb 2018 19:53

Aacini
Member
Registered: 05 Dec 2012
Posts: 142

Re: IF/ELSE with nested parenthesis

I suggest you to use this format:

if /I %1==dev (
      set ROUTERID=D0001EURKAM
      set TOADDRESSEES=EurydiceIT@lists.abc.com
      set CCADDRESSEES=EurydiceIT@lists.abc.com
) else if /I %1==tst (
      set ROUTERID=Q0001EURKAM
      set TOADDRESSEES=EurydiceUsers@lists.abc.com
      set CCADDRESSEES=EurydiceIT@lists.abc.com
) else if /I %1==stg (
      set ROUTERID=Q0001EURKAM
      set TOADDRESSEES=EurydiceUsers@lists.abc.com
      set CCADDRESSEES=EurydiceIT@lists.abc.com
) else if /I %1==prd (
      set ROUTERID=P0001EURKAM
      set TOADDRESSEES=myself@abc.com
      set CCADDRESSEES=EurydiceUsers@lists.abc.com,EurydiceIT@lists.abc.com
) else (
   GOTO SyntaxError
)

Antonio

Offline

#4 28 Feb 2018 20:13

Aacini
Member
Registered: 05 Dec 2012
Posts: 142

Re: IF/ELSE with nested parenthesis

Yoy may also use another (simpler) approach. For example, using an array:

@echo off
setlocal EnableDelayedExpansion

rem Define the values per device:
set "device[dev]=D0001EURKAM EurydiceIT@lists.abc.com EurydiceIT@lists.abc.com"
set "device[tst]=Q0001EURKAM EurydiceUsers@lists.abc.com EurydiceIT@lists.abc.com"
set "device[stg]=Q0001EURKAM EurydiceUsers@lists.abc.com EurydiceIT@lists.abc.com"
set "device[prd]=P0001EURKAM myself@abc.com EurydiceUsers@lists.abc.com,EurydiceIT@lists.abc.com"

rem Extract the values based on first parameter
if not defined device[%1] GOTO SyntaxError
for /F "tokens=1-3" %%a in ("!device[%1]!") do (
   set ROUTERID=%%a
   set TOADDRESSEES=%%b
   set CCADDRESSEES=%%c
)

ECHO ROUTER="%ROUTERID%", TOADDRESS="%TOADDRESSEES%", CCADDRESS="%CCADDRESSEES%"
GOTO :EOF

:SyntaxError
ECHO Invalid device

Antonio

Offline

#5 01 Mar 2018 00:16

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 998
Website

Re: IF/ELSE with nested parenthesis

Good points, thanks Aacini
I think I cling to using ') else (' just because it's an easy pattern to remember.

Offline

#6 01 Mar 2018 16:05

stefano.gatto
Member
From: Geneva, Switzerland
Registered: 26 Nov 2014
Posts: 7

Re: IF/ELSE with nested parenthesis

Very useful and instructive replies guys! Thanks a lot!

Offline

Board footer

Powered by FluxBB