#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2009
    Posts
    157
    Rep Power
    89

    Problem with winmgmts


    Hi, the following code works. If you enter a non existant directory for strFolderName it will err with 424.

    Code:
    On Error Resume Next
    
    strFolderName = "C:\test"
    Set objWMIService = GetObject("winmgmts:")
    Set objFolderSecuritySettings = objWMIService.Get("Win32_LogicalFileSecuritySetting='" & strFolderName & "'")
    intRetVal = objFolderSecuritySettings.GetSecurityDescriptor(objSD)
    
    If err.number = 424 Then 
    	MsgBox err.number
    End If
    err.clear


    The following code does not work

    Code:
    'On Error Resume Next
    
    test()
    
    Sub test()
    	strFolderName = "C:\test"
    	Set objWMIService = GetObject("winmgmts:")
    	Set objFolderSecuritySettings = objWMIService.Get("Win32_LogicalFileSecuritySetting='" & strFolderName & "'")
    	info = objFolderSecuritySettings.GetSecurityDescriptor(objSD)
    	
    	If err.number = 424 Then 
    		MsgBox err.number
    	End If
    	err.clear
    
    
    End Sub
    Error test2.vbs(8, 2) SWbemServicesEx: Not found

    I need run the code within a function or sub but whenever I put it in one the error comes up.

    Please do not reply saying there is an easyer way to check is a directory exists because that is not the goal of the script.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    289
    Rep Power
    44
    Your code:
    Code:
    Private Sub Command1_Click()
        On Error GoTo cmdErr
        strFolderName = "C:\test"
        Set objWMIService = GetObject("winmgmts:")
        Set objFolderSecuritySettings = objWMIService.Get("Win32_LogicalFileSecuritySetting='" & strFolderName & "'")
        intRetVal = objFolderSecuritySettings.GetSecurityDescriptor(objSD)
        Exit Sub
    cmdErr:
            MsgBox "Error " & Str$(Err) & ": " & Error$(Err)
    End Sub
    returns this error:
    Error -2147217406: Not found

    J.A. Coutts

IMN logo majestic logo threadwatch logo seochat tools logo