views:

32

answers:

1

Hi All,

I have some vbscript code I use to set paths to virtual directories when a developer switches his/her environment to work on another project. Currently, we try to set the path, and if there's an error, we create it. It just smells funny. Is there a way to check if a Virtual Directory exists? And if not, create it?

set objIIS = GetObject("IIS://" & strComputer & "/W3SVC/1/ROOT/SomeWeb")
objIIS.Path = strNewPath & "\SomeWeb"
objIIS.SetInfo

If Err.Number =  NOT_FOUND Then
    sVirtPath = strNewPath & "\SomeWeb"
CreateVirtualDirectory "SomeWeb", sVirtPath, true
Err.Clear
End If

This works just fine, but something tells me, there must be a better way. Does anyone have any suggestions? How do I check for the existence of a Virtual Directory? Any feedback is appreciated.

Thanks for any pointers.
Cheers,
~ck in San Diego

A: 

Your approach seems fine. Here is a slightly different approach I did years ago when working with IIS 5.0. I'm not sure if it would still work under later versions of IIS, but you may find it useful.

Function IsExistingWebApp(strAppPath)
    Dim oWeb

    On Error Resume Next

    ' Assume it does not exist
    IsExistingWebApp = False

    Set oWeb = GetObject( strAppPath )
    If ( Err <> 0 ) Then
        ' If an error occurs then we know the application does not exist
        'LogMessage strAppPath & " does not exist as an application."

        Err = 0
        Exit Function
    Else
        If oWeb.Class = "IIsWebVirtualDir" Then
        'LogMessage "Found existing web application " & oWeb.AdsPath
        ' If it does exist and it is configured as a 
        ' virtual directory then rerturn true
        IsExistingWebApp = True
        End If      
    End If  
End Function
Garett