?
Solved

Command Line Parameters In Nested IF Statements

Posted on 2009-04-28
6
Medium Priority
?
3,897 Views
Last Modified: 2012-05-06
The problem here is simple. The code is written lexically correct. The nested IF statements are logically correct. Before the interpreter attempts to run this code, it first tries to resolve variables including command line variables. Where comparisons are made against %2, for the program to reach that part of the code %2 would infact be defined but the interpreter appears to ignore this logical fact. Is there a way to force the interpreter to accept this code without detracting from the current style?

What the code does: Simply put, it generates a random number between a min value and a max value. If there are no parameters, min and max are set to 1 and 9 respectively. If two values are passed as parameters then min and max are set to %1 and %2. Tests are then performed to validate that %1 and %2 are in range as well as the order of %1 and %2. Really simple but an annoyingly frustrating oversight.

@echo off
if "%1"=="" (
   set /a Min = 1                                             // There are no parameters so set default values
   set /a Max = 9
) else (
   if not "%2"=="" (                                         // %1 exists....
      if /i %1 lss 1 exit /b -2                             // %2 also exists so validate both %1 and %2....
      if /i %1 gtr 9 exit /b -3
      if /i %2 lss 1 exit /b -4
      if /i %2 gtr 9 exit /b -5
      if /i %1 gtr %2 exit /b -6
      set /a Min = %1                                       // and if all is well, set min and max values
      set /a Max = %2
   ) else (
      exit /b -1                                                  // %1 exists but %2 does not so no need to validate either
   )
)                                                                     // The rest of the code os okay

set /a num = %random% %% (%max% - %Min% + 1) + %Min%

echo %num%                                               // This line will be removed after debugging is complete
exit /b %num%
0
Comment
Question by:t0t0
  • 4
6 Comments
 
LVL 9

Accepted Solution

by:
dlb6597 earned 300 total points
ID: 24254774
will it work if you put quotes around your variables and the value you are comparing to in all your "if /i" statements?
LIke such:

if /i "%1" lss "1" exit /b -2
0
 
LVL 16

Author Comment

by:t0t0
ID: 24255123
Oddly enough, I did try that approach earlier and it returned a bad value however, I'm back-tracking my test runs in case of a human error on my part...

Will let you know when I have completely retested the code.

0
 
LVL 16

Author Comment

by:t0t0
ID: 24255217
So far, the following code appears to work:

@echo off
if "%1"=="" (
   set /a RndYear = "%random% %% (%date:~6,4% - 1752 + 1) + 1752"
) else (
   if not "%2"=="" (
      if "%1" lss "1752" exit /b -31
      if "%1" gtr "9999" exit /b -32
      if "%2" lss "1752" exit /b -33
      if "%2" gtr "9999" exit /b -34
      if "%1" gtr  "%2"  exit /b -35
      set /a RndYear = "%random% %% (%2 - %1 + 1) + %1"
   ) else (
      exit /b -36
   )
)
echo %RndYear%
exit /b %RndYear%
0
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 
LVL 21

Assisted Solution

by:AmazingTech
AmazingTech earned 200 total points
ID: 24261577
Shouldn't your max and min not be equal as well?

if "%1" geq  "%2"  exit /b -35
0
 
LVL 16

Author Comment

by:t0t0
ID: 24262989
Hello AmazingTech....

DOS is full of surprises. I recently discovered we can also use IF DEFINED and IF NOT DEFINED with %1, %2 %3 etc. but who would have thought of enclosing arithmetic expressions in double-quotes let alone individual operands of a conditional arithmetic IF statement....

For the purpose of my application, I don't want to exit with an error code if %1 = %2. But well spotted anyway.
0
 
LVL 16

Author Closing Comment

by:t0t0
ID: 31575592
Well done. Awardng point proportionally.
0

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Question has a verified solution.

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

TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month15 days, 9 hours left to enroll

850 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