pharaoh fortune slot
ExamNotes.net
Welcome, Guest. Please login or register.
November 23, 2017, 11:06:19 AM

Login with username, password and session length
* Home Help Search Login Register
+  ExamNotes.net
|-+  General discussions
| |-+  Programming Forum
| | |-+  VbScript Question
0 Members and 1 Guest are viewing this topic. « previous next »
Pages: [1] Print
Author Topic: VbScript Question  (Read 3281 times)
mindmesh
Spit Fire
Hero Member
*****
Offline Offline

Posts: 1623

2


View Profile
« on: March 03, 2005, 04:42:56 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
Logged

I take no responsibility for the offensive nature of this post.. If you take offense I am sorry, but honestly don't give a damn. All rights reserved!!
dmaftei
Senior Member
Hero Member
*****
Offline Offline

Posts: 2156

1


View Profile
« Reply #1 on: March 03, 2005, 07:11:39 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
« Last Edit: March 03, 2005, 08:11:47 PM by dmaftei » Logged

BSEE, MSCS
www.maftei.net
mindmesh
Spit Fire
Hero Member
*****
Offline Offline

Posts: 1623

2


View Profile
« Reply #2 on: March 03, 2005, 08:37:25 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.
Logged

I take no responsibility for the offensive nature of this post.. If you take offense I am sorry, but honestly don't give a damn. All rights reserved!!
dmaftei
Senior Member
Hero Member
*****
Offline Offline

Posts: 2156

1


View Profile
« Reply #3 on: March 03, 2005, 09:28:40 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
Logged

BSEE, MSCS
www.maftei.net
mindmesh
Spit Fire
Hero Member
*****
Offline Offline

Posts: 1623

2


View Profile
« Reply #4 on: March 04, 2005, 01:30:37 PM »

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.
Logged

I take no responsibility for the offensive nature of this post.. If you take offense I am sorry, but honestly don't give a damn. All rights reserved!!
dmaftei
Senior Member
Hero Member
*****
Offline Offline

Posts: 2156

1


View Profile
« Reply #5 on: March 04, 2005, 02:31:48 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
« Last Edit: March 04, 2005, 02:34:54 PM by dmaftei » Logged

BSEE, MSCS
www.maftei.net
mindmesh
Spit Fire
Hero Member
*****
Offline Offline

Posts: 1623

2


View Profile
« Reply #6 on: March 04, 2005, 02:42:25 PM »

I'm a Domain Admin.. Maybe the Enterprise admin account.
Logged

I take no responsibility for the offensive nature of this post.. If you take offense I am sorry, but honestly don't give a damn. All rights reserved!!
Pages: [1] Print 
« previous next »
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.13 | SMF © 2006-2011, Simple Machines LLC Valid XHTML 1.0! Valid CSS!