|
Home > Archive > Programming Forum > March 2005 > VbScript Question
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| mindmesh 2005-03-03, 2:42 pm |
| This script only enumerates the files in a specified folder, how do I get it to enumerate thru all folders and output the files? Thanks.
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 | |
| dmaftei 2005-03-03, 5:11 pm |
| code:
Option Explicit
'' On Error Resume Next; don't ignore errors
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
Dim start
start = fso.GetAbsolutePathName(".") '' or prompt for path
list(start)
'' Recursively list directory contents
Sub list(dirname)
Dim crt
Set crt = fso.GetFolder(dirname)
Dim file
For Each file In crt.Files
wscript.Echo fso.GetAbsolutePathName(file)
Next
Dim subdir
For Each subdir In crt.SubFolders
list(subdir.Name)
Next
End Sub
| |
| mindmesh 2005-03-03, 6:37 pm |
| Well that sort of works. If I point it to my s:\ it gives me all the files directly in the S:\ but doesn't give me the file that are in the subdriectories of the S: drive. If I point it to "." it goes thru all the files directly in my profile, but not thru all the other folders.
Here's what I'm trying to accomplish, and maybe you can point me in the right direction if you're not sure how to script this. I've stumped quite a few people, and I'm sort of new to VBScript so imagine the headaches.
Two people were recently fired for 'suspicious activity' they want a list of all the files that belong to them on a specific network drive (they want to be able to select which drive each time). I need to have a script that runs thru all the folders on the network drive and gives me the file path and owner of each file. That's where I'm stuck. I can't get it to run thru all the folders and give me all the files and their owners.
Thanks for your help. | |
| dmaftei 2005-03-03, 7:28 pm |
| code:
Option Explicit
'' On Error Resume Next '' don't ignore errors
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
Dim drive
Set drive = fso.GetDrive("z") '' replace this with an input box
Dim root
Set root = drive.RootFolder
list(root)
'' Recursively list directory contents
Sub list(dir)
Dim file
For Each file In dir.Files
wscript.Echo file.Path '' add your owner function here
Next
Dim subdir
For Each subdir In dir.SubFolders
list(subdir)
Next
End Sub
| |
| mindmesh 2005-03-04, 11:30 am |
| Thanks, dmaftei. Works like a charm. I have one more question for you. The script quits if it hits a file that it doens't have access too. Any ideas on how to let it keep going and just skip that folder? Thanks again. | |
| dmaftei 2005-03-04, 12:31 pm |
| As administrator you _should_ have access to everything. Failing that:
code:
Option Explicit
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
Dim drive
Set drive = fso.GetDrive("z") '' replace this with an input box
Dim root
Set root = drive.RootFolder
list(root)
'' Recursively list directory contents
Sub list(dir)
Dim file
For Each file In dir.Files
wscript.Echo file.Path '' add your owner function here
Next
Dim subdir
For Each subdir In dir.SubFolders
On Error Resume Next '' skip if cannot dig in
list(subdir)
Next
End Sub
| |
| mindmesh 2005-03-04, 12:42 pm |
| I'm a Domain Admin.. Maybe the Enterprise admin account. |
|
|
|
|