Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


File/Folder enumeration

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,2220
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> File/Folder enumeration
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 File/Folder enumeration - 3/2/2005 1:36:33 PM   
  Mindmesh

 

Posts: 78
Score: 0
Joined: 11/23/2004
From:
Status: offline
I'm rather new to vbscript and I'm having trouble figuring out how to enumerate thru all the folders and subfolders on a drive, and listing the owners of all the files in the folders/subfolders. I have no idea where to begin with this and I was wondering if anyone could give me a hand. Any help you can give is appreciated.
 
 
Post #: 1
 
 Re: File/Folder enumeration - 3/2/2005 5:11:13 PM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
The following code will recursively enumerate all files and sub directories whthin a given directory and list their associated owner.

Note that if you intend to use this script to enumerate all files on a given drive, it WILL take a LONG time to complete. To do this more efficiently, you should use the built-in command dir and use the getOwner function to retrieve the respective owner.

Check out dir /? for details.

==================================================================================
Option Explicit
Dim folder

folder = "f:\test"
getFolder(folder)

Function getFolder(root)
Dim fso, folders, folder, file, files
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FolderExists(root) Then
WScript.Echo "Folder: " & root & vbTab & "Owner: " & getOwner(root)
For Each file In fso.GetFolder(root).Files
WScript.Echo "File: " & file & vbTab & "Owner: " & getOwner(fso.GetAbsolutePathName(file))
Next
For Each folder In fso.getFolder(root).SubFolders
getFolder(fso.GetAbsolutePathName(folder))
Next
Else
WScript.Echo "Folder doesn't exist: " & root
Exit Function
End If
End Function

Function getOwner(object)
Dim su, sd
Set su = CreateObject("ADsSecurityUtility")
Set sd = su.GetSecurityDescriptor(object, 1, 1)
getOwner = sd.Owner
End Function

(in reply to Mindmesh)
 
 
Post #: 2
 
 Re: File/Folder enumeration - 3/3/2005 4:25:19 AM   
  Mindmesh

 

Posts: 78
Score: 0
Joined: 11/23/2004
From:
Status: offline
Thanks for the script. I made some changes to it, so that it will output to a text file, but it only outputs from the main drive that I specify, it doesn't enumerate thru all the files in all the folders.. Here are the changes that I made. Let me know if you can help me out. Thanks again.

Option Explicit
On Error Resume Next
Dim folder
folder = Inputbox("Which drive do you want to check?")
getFolder(folder)

Function getFolder(root)

Dim fso, folders, folder, file, files, txtFile
Const Appending = 8

Set fso = CreateObject("Scripting.FileSystemObject")

If fso.FolderExists(root) Then
Set txtFile = fso.createtextfile("C:\Owners.txt", False)
If err.number <> 0 then
Set txtFile = fso.opentextfile("C:\Owners.txt", Appending)
End If



txtFile.WriteLine root & "," & getOwner(root)
For Each file In fso.GetFolder(root).Files
txtFile.WriteLine file & "," & getOwner(fso.GetAbsolutePathName(file))
Next
For Each folder In fso.getFolder(root).SubFolders
getFolder(fso.GetAbsolutePathName(folder))
Next
Else
WScript.Echo "Folder doesn't exist: " & root
Exit Function
End If
End Function

Function getOwner(object)
Dim su, sd
Set su = CreateObject("ADsSecurityUtility")
Set sd = su.GetSecurityDescriptor(object, 1, 1)
getOwner = sd.Owner
End Function


Thanks again.

(in reply to Mindmesh)
 
 
Post #: 3
 
 Re: File/Folder enumeration - 3/3/2005 1:50:12 PM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
Move these lines out of the function.

=========================================================
Dim fso, folders, folder, file, files, txtFile
Const Appending = 8

Set fso = CreateObject("Scripting.FileSystemObject")

If fso.FolderExists(root) Then
Set txtFile = fso.createtextfile("C:\Owners.txt", False)
If err.number <> 0 then
Set txtFile = fso.opentextfile("C:\Owners.txt", Appending)
End If
=========================================================

You may want to change the code (if necessary) to control whether the file gets overwritten etc.

(in reply to Mindmesh)
 
 
Post #: 4
 
 Re: File/Folder enumeration - 3/4/2005 3:56:25 AM   
  Mindmesh

 

Posts: 78
Score: 0
Joined: 11/23/2004
From:
Status: offline
Token,
Thanks for your help. I have a script that is now working, but when it reaches a folder it doesn't have access too it fails even if I turn off errors. Is there a way to make the script just skip that folder?

Here is the tweaked script:


      

(in reply to Mindmesh)
 
 
Post #: 5
 
 Re: File/Folder enumeration - 3/4/2005 12:24:59 PM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
Sure, I guess the easiest way is to use the getFolder method to bind to a directory and use the ERR object to determine if errors occur right after the binding. If not, continue the scripts downward; otherwise, abort.

(in reply to Mindmesh)
 
 
Post #: 6
 
 Re: File/Folder enumeration - 3/15/2005 11:06:08 AM   
  Mindmesh

 

Posts: 78
Score: 0
Joined: 11/23/2004
From:
Status: offline
Thanks again, Token. I'm trying to use the GetFolder Method, but I'm still having the same problem. If I get a Access Denied error the program terminates instead of moving to the next folder/subfolder. Here is the code, any ideas?:





      

(in reply to Mindmesh)
 
 
Post #: 7
 
 Re: File/Folder enumeration - 3/15/2005 11:12:14 AM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
Uncomment On Error Resume Next =)

(in reply to Mindmesh)
 
 
Post #: 8
 
 Re: File/Folder enumeration - 3/17/2005 4:22:57 AM   
  Mindmesh

 

Posts: 78
Score: 0
Joined: 11/23/2004
From:
Status: offline
Nope. Tried that already. just stops at the folder without the proper permissions, instead of moving to the next folder. Thanks again for the help.

(in reply to Mindmesh)
 
 
Post #: 9
 
 Re: File/Folder enumeration - 3/17/2005 4:30:53 AM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
In that case, you will have to use dir cmd to enuemrate though the list of files and sub directoreis.

(in reply to Mindmesh)
 
 
Post #: 10
 
 Re: File/Folder enumeration - 3/17/2005 4:46:39 AM   
  mbouchard


Posts: 1916
Score: 16
Joined: 5/15/2003
From: USA
Status: offline
Move the on error resume next into the sub. What might be happening is that one of your err.clear's is resetting your error supression so the next time you would get an error it is not being suppressed.

      

(in reply to Mindmesh)
 
 
Post #: 11
 
 Re: File/Folder enumeration - 3/17/2005 5:06:12 AM   
  kirrilian


Posts: 628
Score: 3
Joined: 3/15/2005
From:
Status: offline
i have a wmi implementation that we use for file space billing on remote machines if this helps. A couple of things, wmi will error out if it finds certain special characters, i think the single quote was one. and you have to call both subs on the same directory since they do different things to get the total for a folder. also, wmi is very slow if there are a bunch of files/folders.


      

(in reply to Mindmesh)
 
 
Post #: 12
 
 Re: File/Folder enumeration - 3/18/2005 3:22:07 AM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
I'm terrified by WMI and filesystem combination =)

(in reply to Mindmesh)
 
 
Post #: 13
 
 Re: File/Folder enumeration - 3/18/2005 4:29:38 AM   
  kirrilian


Posts: 628
Score: 3
Joined: 3/15/2005
From:
Status: offline
it was horrible, lemme tell ya. but we have 200+ servers and i didnt relish collecting data from locally run scripts either...

it wouldnt be so bad if it wasnt sooooo slow

(in reply to Mindmesh)
 
 
Post #: 14
 
 Re: File/Folder enumeration - 3/18/2005 4:43:57 AM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
heh..unfortunately, it is [:(!]

I've done some tests in the past and I have decided never to use it again unless the objects are within a managable size. eg: <= 500

(in reply to Mindmesh)
 
 
Post #: 15
 
 Re: File/Folder enumeration - 3/18/2005 6:05:22 AM   
  Mindmesh

 

Posts: 78
Score: 0
Joined: 11/23/2004
From:
Status: offline
Guys, thank you for all your help. Moving the On Error Resume Next into the sub worked like a champ..

(in reply to Mindmesh)
 
 
Post #: 16
 
 Re: File/Folder enumeration - 3/18/2005 6:11:41 AM   
  kirrilian


Posts: 628
Score: 3
Joined: 3/15/2005
From:
Status: offline
quote:
Originally posted by token

heh..unfortunately, it is [:(!]

I've done some tests in the past and I have decided never to use it again unless the objects are within a managable size. eg: <= 500





yea my boss wanted a file comparison script using wmi. [B)] *shiver* but i did it and other than the wait, it does some custom stuff that i havent seen in any commercial products.

(in reply to Mindmesh)
 
 
Post #: 17
 
 Re: File/Folder enumeration - 3/18/2005 7:33:21 AM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
hm.. Glad you got it all worked out.

I don't understand why it worked though. When I tested, it just stopped at the error directory without going any further. [?]

(in reply to Mindmesh)
 
 
Post #: 18
 
 Re: File/Folder enumeration - 3/29/2005 6:37:20 AM   
  Mindmesh

 

Posts: 78
Score: 0
Joined: 11/23/2004
From:
Status: offline
Me neither, but so far it's worked. My boss still hasn't tested it, but when i tested it worked fine.

(in reply to Mindmesh)
 
 
Post #: 19
 
 Re: File/Folder enumeration - 3/29/2005 11:19:18 PM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
I have a version of the script that uses dir cmd too. If you encounter any more problems and wish to use the alternative script, just let me know =)

(in reply to Mindmesh)
 
 
Post #: 20
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> File/Folder enumeration Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts