Command Line Parameters In Nested IF Statements

Posted on 2009-04-28
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%
Question by:t0t0
    LVL 9

    Accepted Solution

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

    Author Comment

    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.

    LVL 16

    Author Comment

    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%
    LVL 21

    Assisted Solution

    Shouldn't your max and min not be equal as well?

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

    Author Comment

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

    Author Closing Comment

    Well done. Awardng point proportionally.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    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…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    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…

    737 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

    22 Experts available now in Live!

    Get 1:1 Help Now