Feeds:
Posts
Comments

Posts Tagged ‘parameter’

Something neat I did was working with command line parameters in VBScript. You can access the command line arguments array through

Wscript.Arguments(i)

where i goes from 0 upwards. So, if you’ve got one argument option you could handle it like this

If Wscript.Arguments.Count = 0 Then
    ‘ Do Proceed as Normal
Else
    If Wscript.Arguments(0) = “/something” Then
         RunFunctionA
  End If
End If

If you want to have multiple parameters (the script I did had /nolog & /reset), then you’ll want to process all the arguments before you proceed. So, I went through the array and turned on flags so I could act afterwards. If you don’t do this, you might end running something you don’t want or in a way you don’t want. You could process your arguments like this:

If Wscript.Arguments.Count = 0 Then
    ‘ Do Proceed as Normal
Else
    For i = 0 to Wscript.Arguments.Count – 1
        If Wscript.Arguments(i) = “/reset” Then
         resetflag=1 ‘Turn on the reset flag 
       ElseIf Wscript.Arguments(i) = “/nolog” Then
         loggingflag = 0 ‘Turn off the logging flag 
  End If
    Next
End If

If loggingFlag =0 Then
  ‘Don’t log
Else
 LogMyStuff ‘ Call the function you want
End If

‘==========================================================================
‘If reset command line parameter is detected, run the reset function only, then quit
‘==========================================================================
If resetFlag Then
 Reset
 WScript.Quit
End If

Read Full Post »