Nested IF statements

Due to serious lack of sleep I am having difficulties concentrating on the following:

The problem is this. The simple condiional statements work well on their own however, when the conditional statements are combined the code produces an error.

I am not concerned with the mathematical formulas.

For the curious though, the code simply generates a year-value. If no parameters are passed then it generates a random date between 1752 to the current year. If parameters are passed then it generates a random year-value between %1 and %2. If only one parameter is passed then a year-value is not generated.

CODE 1 (Works okay)
@echo off
if "%1"=="" set /a RndYear = %random% %% (%date:~6,4% - 1752 + 1) + 1752
if not "%1"=="" if "%2"=="" exit /b
if not "%1"=="" set /a RndYear = %random% %% (%2 - %1 + 1) + %1
exit /b %RndYear%
CODE 2 (Same code using nested IFs - does not work)
@echo off
if "%1"=="" (
   set /a RndYear = %random% %% (%date:~6,4% - 1752 + 1) + 1752
) else (
   if "%2"=="" (
      exit /b
   ) else (
      if not "%1"=="" set /a RndYear = %random% %% (%2 - %1 + 1) + %1
exit /b %RndYear%

I have tried negating the conditions however, I receive the same error.

This may be a simple problem as it may be due to something I have overlooked. As I said, I'm dog-tired and mentally drained.

I have deliberately not included the error message because glancing at the code, both appear to be logically and lexically correct. My XP DOS version is 5.1.2600.
LVL 16
Who is Participating?
MMierzwaConnect With a Mentor Commented:
It seems that problem is in set not with if.
Try to put it in " ".

set /a RndYear = "%random% %% (%date:~6,4% - 1752 + 1) + 1752"

Should work.
t0t0Author Commented:
That is SO, SO, SO unusual.... I don't think it would ever have occurred to me to enclose the expression in quotes...

Thank you
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.