[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
Solved

# Nested IF statements

Posted on 2009-04-27
Medium Priority
7,751 Views
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.
0
Question by:t0t0

LVL 3

Accepted Solution

MMierzwa earned 500 total points
ID: 24248387
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.
0

LVL 16

Author Closing Comment

ID: 31575321
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
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
###### Suggested Courses
Course of the Month19 days, 19 hours left to enroll