Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test if VBScript named argument exists

I want to test if a specific named argument has been provided before validating that argument, so I can provide meaningful error codes for missing and invalid conditions. I have this now

Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set colArgs = WScript.Arguments.Named

If colArgs.Item("Script") Then
    If not objFSO.FileExists(colArgs.Item("Script")) Then
        intReturn = 1805
    End If
Else
    intReturn = 1639
End If

If Not intReturn Then
    msgBox colArgs.Item("Script"), 0, "Script"
Else
    msgBox intReturn, 0, "Error"
End If

And my expectation would be that if I don't provide an argument called Script at all, I would get the Error msgBox with the 1639 value. Instead I get the good msgBox, with a blank for Script. I also tried

If Not colArgs.Item("Script") = "" Then

EDIT: Per @Tomalak, I now have this

Option Explicit
Dim objShell, objFSO, colArgs, intReturn

Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set colArgs = WScript.Arguments.Named

If Not IsEmpty(colArgs.Item("Script")) Then
    If Not objFSO.FileExists(colArgs.Item("Script")) Then
        intReturn = 1805
    End If
Else
    intReturn = 1639
End If

If IsEmpty(intReturn) Then
    msgBox colArgs.Item("Script"), 0, "Script"
Else
    msgBox intReturn, 0, "Error"
End If

And for what it is worth, I am calling the VBScript from PowerShell like this

$script = "\\Mac\iCloud Drive\Px Tools\Dev 4.0\#Spikes\Helper\Helper Target.ps1"
$arguments ="`"\\Mac\iCloud Drive\Px Tools\Dev 4.0\#Spikes\Helper\PxHelper.vbs`" //nologo /script:`"$script`" /wait:1"
Start-Process -filePath:Wscript.exe  -argumentList:$arguments

And now I get the error condition even when the script IS provided. Grrr, Mondays.

like image 665
Gordon Avatar asked Oct 15 '25 08:10

Gordon


1 Answers

If a named argument is not given on the command line

  • WScript.Arguments.Named.Exists("argname") will return False
  • WScript.Arguments.Named("argname") will return an empty value

If a named argument is given but not assigned a value on the command line (/argname)

  • WScript.Arguments.Named.Exists("argname") will return True
  • WScript.Arguments.Named("argname") will return an empty value

If a named argument is given with an empty string on the command line (/argname:)

  • WScript.Arguments.Named.Exists("argname") will return True
  • WScript.Arguments.Named("argname") will return an empty string

If a named argument is given with a value on the command line (/argname:value)

  • WScript.Arguments.Named.Exists("argname") will return True
  • WScript.Arguments.Named("argname") will return a string with that value

Empty values are different from empty strings: They are uninitialized, whereas the empty string is a is regular string of zero length.

You can check for empty values with the IsEmpty() function.

If Not WScript.Arguments.Named.Exists("foo") Then
    ' show message / end script / use default
End If

If IsEmpty(WScript.Arguments.Named("foo")) Then
    ' show message / end script / use default
End If
like image 182
Tomalak Avatar answered Oct 17 '25 23:10

Tomalak