Link to home
Start Free TrialLog in
Avatar of Leadtheway
LeadthewayFlag for United States of America

asked on

Recursive folder search vbscript

Have a script that works fine on a parent folder if i specify an absolute path, i was wondering if there is a way to recurse subfolders as well?

strcomputer = "." 

Set objFSO = CreateObject("Scripting.FileSystemObject") 
objStartFolder = "C:\test" 
Set objFolder = objFSO.GetFolder(objStartFolder) 
WScript.Echo "Root folder Being Enumerated: " & objFolder.Path & vbcrlf 
WScript.Echo "Files Found: " & vbCrLf 
Set colFiles = objFolder.Files 
For Each objFile In colFiles 
intcount = intcount + 1 
 WScript.Echo "   File: " & objFile.Name & vbCrLf 
 WScript.Echo "  Permissions: " & vbcrlf 
  
  
 Call get_Security(objfile,strcomputer) 
Next 
If intcount = 0 Then 
 WScript.Echo "No Files Were Found in " & objstartfolder 
End if 
intcount = 0 
WScript.Echo 
WScript.Echo 

ShowSubfolders objFSO.GetFolder(objStartFolder) 
Sub ShowSubFolders(Folder) 
 For Each Subfolder In Folder.SubFolders 
  WScript.Echo "Sub Folder Name: " & Subfolder.Path 
     
 'Get Security for Folder 
 WScript.Echo "Folder Permissions for: " & subfolder.name & vbcrlf 
  Call get_Security(subfolder,strcomputer) 
   
   
  Set objFolder = objFSO.GetFolder(Subfolder.Path) 
  Set colFiles = objFolder.Files 
  For Each objFile In colFiles 
    IntCountFile = intCountFile + 1 
   
      WScript.Echo "File Permissons for: " & objFile.Name 
     call get_Security(objfile,strcomputer) 
       
  Next 
  
 If intCountFile = 0 Then 
 WScript.Echo "Files: " 
 WScript.Echo subfolder & " Has Zero Files " & vbCrLf & vbcrlf 
 End If 
 intCountFile = 0 
   
  ShowSubFolders Subfolder 
 Next 
End Sub 
  

Sub get_Security(FolderFileValue,strcomputer) 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") 
Set objFile = objWMIService.Get("Win32_LogicalFileSecuritySetting='"&FolderFileValue&"'") 
                                                                      
If objFile.GetSecurityDescriptor(objSD) = 0 Then 
WScript.Echo 
Wscript.Echo "Owner: " & objSD.Owner.Name 
Wscript.Echo 
For Each objAce in objSD.DACL 
    Wscript.Echo "Trustee: " & objAce.Trustee.Domain & "\" & objAce.Trustee.Name 
    If objAce.AceType = 0 Then 
        strAceType = "Allowed" 
    Else 
        strAceType = "Denied" 
    End If 
    Wscript.Echo "Ace Type: " & strAceType 
    Wscript.Echo "Ace Flags:" 
    If objAce.AceFlags AND 1 Then 
        Wscript.Echo vbTab & "Child objects that are not containers inherit permissions." 
    End If 
    If objAce.AceFlags AND 2 Then 
        Wscript.Echo vbTab & "Child objects inherit and pass on permissions." 
    End If 
    If objAce.AceFlags AND 4 Then 
        Wscript.Echo vbTab & "Child objects inherit but do not pass on permissions." 
    End If 
    If objAce.AceFlags AND 8 Then 
        Wscript.Echo vbTab & "Object is not affected by but passes on permissions." 
    End If 
    If objAce.AceFlags AND 16 Then 
        Wscript.Echo vbTab & "Permissions have been inherited." 
    End If 
    Wscript.Echo "Access Masks:" 
    If objAce.AccessMask AND 1048576 Then 
        Wscript.Echo vbtab & "Synchronize" 
    End If 
    If objAce.AccessMask AND 524288 Then 
        Wscript.Echo vbtab & "Write owner" 
    End If 
    If objAce.AccessMask AND 262144 Then 
        Wscript.Echo vbtab & "Write ACL" 
    End If 
    If objAce.AccessMask AND 131072 Then 
        Wscript.Echo vbtab & "Read security" 
    End If 
    If objAce.AccessMask AND 65536 Then 
        Wscript.Echo vbtab & "Delete" 
    End If 
    If objAce.AccessMask AND 256 Then 
        Wscript.Echo vbtab & "Write attributes" 
    End If 
    If objAce.AccessMask AND 128 Then 
        Wscript.Echo vbtab & "Read attributes" 
    End If 
    If objAce.AccessMask AND 64 Then 
        Wscript.Echo vbtab & "Delete dir" 
    End If 
    If objAce.AccessMask AND 32 Then 
        Wscript.Echo vbtab & "Execute" 
    End If 
    If objAce.AccessMask AND 16 Then 
        Wscript.Echo vbtab & "Write extended attributes" 
    End If 
    If objAce.AccessMask AND 8 Then 
        Wscript.Echo vbtab & "Read extended attributes" 
    End If 
    If objAce.AccessMask AND 4 Then 
        Wscript.Echo vbtab & "Append" 
    End If 
    If objAce.AccessMask AND 2 Then 
        Wscript.Echo vbtab & "Write" 
    End If 
    If objAce.AccessMask AND 1 Then 
        Wscript.Echo vbtab & "Read" 
    End If 
    Wscript.Echo 
    Wscript.Echo 
Next 
End If 
  

End Sub 

Open in new window

Avatar of Bill Prew
Bill Prew

I'm confused on what the question is, the script you showed already seems to drill down into all subfolders in the folder tree of the parent folder.  Can you explain further the problem you are having or what you need to add to this?

~bp
Avatar of Leadtheway

ASKER

its not recursing folders, and its doing all the files as well, i just want parent and child objects if the permissions differ from parent, attached is what i'm getting, but it seems once it completes the root folder it errors instead of recursing.  i get the following error:llpathscript.vbs(57, 1) SWbemServicesEx: Invalid object path
output.txt
It certainly seems to be recursing into subfolders, I see the following in the output file:

  2202 1:Sub Folder Name: E:\Share\.TemporaryItems
  2367 1:Sub Folder Name: E:\Share\.TemporaryItems\folders.1686636526
  2532 1:Sub Folder Name: E:\Share\.TemporaryItems\folders.502
  2697 1:Sub Folder Name: E:\Share\.Trashes
  2862 1:Sub Folder Name: E:\Share\.Trashes\0
  3027 1:Sub Folder Name: E:\Share\.Trashes\1686636526
  3498 1:Sub Folder Name: E:\Share\Accounting
  5808 1:Sub Folder Name: E:\Share\Accounting\2016 SOC 1 Audit
  6336 1:Sub Folder Name: E:\Share\Accounting\2016 SOC 1 Audit\6A
  15264 1:Sub Folder Name: E:\Share\Accounting\2016 SOC 1 Audit\6B
  22032 1:Sub Folder Name: E:\Share\Accounting\2016 SOC 1 Audit\A3
  28800 1:Sub Folder Name: E:\Share\Accounting\2016 SOC 1 Audit\A5

Open in new window

~bp
Yeah i see its recursing the first folder, but not all the way through and not the rest of the folders, plus that error might be breaking it,
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
There are also different ways to enumerate the flag values, take a look at the dictionary technique in this example, makes the executing code go a little cleaner to read and *may* execute a little faster.

http://www.indented.co.uk/2009/02/19/reading-ntfs-and-share-security-with-vbscript/

~bp
Thanks for the assist!