Can you add command line switches to execute the WinBatch routine?

Started by jpbreese, August 21, 2014, 11:00:50 AM

Previous topic - Next topic

jpbreese

I am writing a WinBatch program that will ask the user if they really want to run the routine.  I'd like to add the capability of running the routine with command line switches like "/s" "/silent", "/?", ...etc.

How can code the WinBatch routine to accept these switches when they are passed and to provide a list of valid switches if they use the "/?".

td

Yes, you can pass parameters to a compiled or uncompiled script. 

To quote from the Consolidated WIL Help file, "Parameters passed to a WBT file will be automatically inserted into variables named param1, param2, etc. An additional variable, param0, gives you the total number of command-line parameters."

You can find more information by viewing the Command Line Parameters topic in help file.

You can also pass parameters to a called script using the WIL Call function. The parameter variables are filled in a similar fashion to command line parameters
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

jpbreese

I had already found this information, but was having problems coming up with a way to script this.  After reading your response I decided to play around with it a little and have successfully created the parameters that I was looking for.  Thanks for the guidance.


Response = param1
If Response == ("/s") Then GoTo SILENT
If Response == ("/silent") Then GoTo SILENT
If Response == ("/u") Then GoTo UNINSTALL
If Response == ("/uninstall") Then GoTo UNINSTALL
If Response == ("/?") Then GoTo LIST
   Else GoTo OOPS


:SILENT
Message ("Results", "Silent Switch has been enabled.")
Exit

:UNINSTALL
Message ("Results", "Uninstall Switch has been enabled.")
Exit

:OOPS
Message ("Results", "Switch is not recognized.")
Exit

:LIST
;Switch_MenuX=031
;Switch_MenuY=088

Switch_MenuFormat=`WWWDLGED,6.2`

Switch_MenuCaption=`Available Switches`
Switch_MenuX=410
Switch_MenuY=240
Switch_MenuWidth=171
Switch_MenuHeight=098
Switch_MenuNumControls=005
Switch_MenuProcedure=`DEFAULT`
Switch_MenuFont=`MS Sans Serif|9216|70|34`
Switch_MenuTextColor=`255|255|255`
Switch_MenuBackground=`DEFAULT,0|0|255`
Switch_MenuConfig=0

Switch_Menu001=`059,077,043,013,PUSHBUTTON,"PushButton_OK",DEFAULT,"OK",1,10,32,DEFAULT,DEFAULT,"128|0|64"`
Switch_Menu002=`007,019,063,013,STATICTEXT,"StaticText_1",DEFAULT,"Silent Install",DEFAULT,20,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
Switch_Menu003=`007,043,063,013,STATICTEXT,"StaticText_2",DEFAULT,"Uninstall",DEFAULT,30,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
Switch_Menu004=`089,019,063,013,STATICTEXT,"StaticText_3",DEFAULT,"/s or /silent",DEFAULT,21,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
Switch_Menu005=`087,043,072,013,STATICTEXT,"StaticText_Uninstall",DEFAULT,"/u or /uninstall",DEFAULT,31,DEFAULT,DEFAULT,DEFAULT,DEFAULT`

ButtonPushed=Dialog("Switch_Menu")

Exit

td

You might consider making your command line parameters case-insensitive.

Code (winbatch) Select

Response = StrLower(Param1)


Your users will appreciate you even more...
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

td

Neglected to mention that you should always check param0 for the number of parameters.  This is because the other numbered param* variables are only defined when the corresponding parameter actually exists. In your case, the user would get a variable not defined error, if a user ran your script without parameters.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

DAG_P6

To that end, I created GetCommandLineArgs_P6C.WIL, an #include file that is designed to be inserted inline near the top of the main routine of a script. If there are arguments, it creates a precisely sized array, called ArgV, and copies them into it.

Usage is something like the following.


#include 'GetCommandLineArgs_P6c.WIL'

sProgressMsg = NULL_STRING_P6C

    if VarType ( ArgV ) == VARTYPE_ARRAY
        iLastArg                = ArgC - 1

        for iCurrArg = ARRAY_FIRST_ELEMENT_P6C to iLastArg
            sArgSw              = GetArgSwitchName_WW ( ArgV [ iCurrArg ] )

; ----------------------------------------------------------------
; H:\DEASE\EXPORTS\XFER_FTP\FTP_LIST.TXT
; C:\Users\Public\Documents\hdrive\DEASE\EXPORTS\XFER_FTP\FTP_LIST.TXT
; ----------------------------------------------------------------

            if sArgSw != SW_FLAG_NONE
                switch @true
                    case sArgSw == SW_CONFIG_FILE_NAME
                        sConfigFileFQFN     = GetArgSwitchValue_WW ( ArgV [ iCurrArg ] )
                        sProgressMsg        = LBL_CONFOG_FILE : sConfigFileFQFN
                        break

                    case sArgSw == SW_SHORTCUT_LIST
                        sShortcutList       = GetArgSwitchValue_WW ( ArgV [ iCurrArg ] )
                        sProgressMsg        = sProgressMsg : @lf : LBL_SHORTCUT_LIST : sShortcutList
                endswitch   ; switch @true
            endif   ; if sArgSw != SW_FLAG_NONE
        next    ; for iCurrArg = ARRAY_FIRST_ELEMENT_P6C to iLastArg
    else ; if VarType ( ArgV ) == VARTYPE_ARRAY
        RunTimeStatus           = RTStatus ( )

        switch RunTimeStatus


The rest of the switch block (not shown) does alternative processing for scripts running in WB Studio.
David A. Gray
You are more important than any technology.

jpbreese

One of my co-workers helped expand what we were trying to accomplish.  We were looking to pass a "/silent" and/or a "/reboot" switch.  This was our solution and it works really well...Thanks to everyone for their input.


;Default Action = no parameters then run setup normally
;Invalid Response = Message installer with valid options list

SILENT = @FALSE
REBOOT = @FALSE


;param0 contanins the number of parameters passed
;check for up to 2 parameters
Select param0
  Case 0
    Goto NO_PARAMETERS_PASSED
    Break
  Case 1
    Goto ONE_PARAMETER_PASSED
    Break
  Case 2
    Goto TWO_PARAMETERS_PASSED
    Break
  Case param0
    Goto TOO_MANY_PARAMETERS
    Break
  End Select


:NO_PARAMETERS_PASSED
  ;Message("None Passed", "Running without parameters")
  Goto FINISH

:ONE_PARAMETER_PASSED
  ;Message("One Passed", "Running with 1 parameter")

  param1 = StrUpper(param1)

  If param1 == '/?" Then Goto PARAMETERS_RESPONSE
  Else
     If param1 == "/SILENT" || param1 == "-SILENT" Then
       SILENT = @TRUE
       Else
          If param1 == "/REBOOT" || param1 == "-REBOOT" Then
          REBOOT = @TRUE
             Else
                Goto PARAMETERS_RESPONSE
             EndIf
       EndIf
  EndIf

  Goto FINISH


:TWO_PARAMETERS_PASSED
  ;Message("Two Passed", "Running with 2 parameters")

  param1 = StrUpper(param1)
  param2 = StrUpper(param2)

  ;check to see if the 2 parameters are different
  ;compare only the 2nd character of the string so that /s and -s are the same
  If StrSub(param1,2,1) != StrSub(param2,2,1) Then

    If param1 == "/SILENT" || param1 == "-SILENT" Then
      SILENT = @TRUE
    Else
      If param1 == "/REBOOT" || param1 == "-REBOOT" Then
        REBOOT = @TRUE
      Else
        Goto PARAMETERS_RESPONSE
      EndIf
    EndIf

    If param2 == "/SILENT" || param2 == "-SILENT" Then
      SILENT = @TRUE
    Else
      If param2 == "/REBOOT" || param2 == "-REBOOT" Then
        REBOOT = @TRUE
      Else
        Goto PARAMETERS_RESPONSE
      EndIf
    EndIf

  ;if both parameters are the same, just use param1   
  Else
    If param1 == "/SILENT" || param1 == "-SILENT" Then
     SILENT = @TRUE
    Else
      If param1 == "/REBOOT" || param1 == "-REBOOT" Then
        REBOOT = @TRUE
      Else
        Goto PARAMETERS_RESPONSE)
      EndIf
    EndIf
  EndIf

  Goto FINISH

:TOO_MANY_PARAMETERS
  Goto PARAMETERS_RESPONSE

:FINISH
  If SILENT Then
    If REBOOT Then
      Message("Installation Response", "Setup will be installed silently and force a reboot.")
    Else
      Message("Installation Response", "Setup will be installed silently and the installer will be responsible for rebooting the computer to complete the install.")
    EndIf
  Else
    If REBOOT Then
      Message("Installation Response", "Setup will install with user status notifications and will force a reboot at the completion of the install.")
    Else
      Message("Installation Response", "Setup will install with user status notifications and the installer will be responsible for rebooting the computer to complete the install.")
    EndIf
  EndIf

  Goto EXIT

:EXIT
Exit



:PARAMETERS_RESPONSE
Message ("Setup Installer", "Setup Installer.%@CRLF%%@CRLF%Setup.exe /? <Install Option> <Reboot Option>%@CRLF%%@CRLF%Install Option%@CRLF%          /silent  = a silent install.%@CRLF%%@CRLF%Reboot Option%@CRLF%          /reboot = to force a reboot after the install.%@CRLF%%@CRLF%%@CRLF%Example 1...Setup.exe /silent /reboot%@CRLF%          This will install Setup silently and force a reboot after the install.%@CRLF%%@CRLF%Example 2...Setup.exe /silent%@CRLF%          This will install Setup silently.%@CRLF%          A user reboot will be required to complete the install.")
Exit

DAG_P6

Excellent work! Been there, done that, and a lot more. There is also a way to use string literals in switch blocks. For example:

switch @true
    case StrLower ( param1 ) == "condition1"
        ; Do something
    case StrLower ( param1) == "condition2"
        ; Do something else.
endswitch
David A. Gray
You are more important than any technology.