Nested IF statements

Posted on 2009-04-27
Last Modified: 2012-06-21
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.
Question by:t0t0
    LVL 3

    Accepted Solution

    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.
    LVL 16

    Author Closing Comment

    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

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (…
    YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT ( The crux of this batch f…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now