Page 1 of 1 1
Topic Options
#76549 - 2003-08-30 01:43 AM DirList with Owner of files
Richie19Rich77 Offline
Seasoned Scripter
*****

Registered: 2002-08-16
Posts: 624
Loc: London, England
Hi Everyone

Been doing some digging into M$ site and have created a much faster code that lists the owner of each file in a given folder.

code:
$folderName = "C:\Winnt"
ShowFileList($folderName)

Function ShowFileList($folderName)
$FileList = CreateObject("Scripting.FileSystemObject").GetFolder($folderName).Files
For Each $FileListArray In $FileList
$Filename = $FolderName+"\"+$FileListArray.name
$WMIFileSecSetting = GetObject ("Winmgmts:").ExecQuery("ASSOCIATORS OF {win32_LogicalFileSecuritySetting='"+$FileName+"'}WHERE AssocClass=Win32_LogicalFileOwner ResultRole=Owner")
For Each $Object In $WMIFileSecSetting
? "FileName: "+$FileListArray.name+" Owner: "+$Object.AccountName
Next
Next
EndFunction

and a basic DirList - Looks super fast to me.

code:
$folderName = "C:\Winnt"
ShowFileList($folderName)

Function ShowFileList($folderName)
$FileList = CreateObject("Scripting.FileSystemObject").GetFolder($folderName).Files
For Each $FileListArray In $FileList

? "FileName: "+$FileListArray.name

Next
EndFunction



[ 30. August 2003, 01:44: Message edited by: Richard Farthing ]

Top
#76550 - 2003-08-30 01:47 AM Re: DirList with Owner of files
Richie19Rich77 Offline
Seasoned Scripter
*****

Registered: 2002-08-16
Posts: 624
Loc: London, England
Hmm the second bit of code is not that much faster [Roll Eyes] , It is much faster when doing a DirList and checking the owner.
Top
#76551 - 2003-08-30 03:25 PM Re: DirList with Owner of files
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
the question now is, is it faster than FileOwner()
_________________________
!

download KiXnet

Top
#76552 - 2003-08-30 04:10 PM Re: DirList with Owner of files
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
for each $file in FileList('\\server\share,'.pdf')
? $file
next

code:
 
Function FileList($folderName,optional $mask)
$objDir = CreateObject("Scripting.FileSystemObject").GetFolder($folderName).Files
if @error exit 1 endif ; usually folder not found
For Each $objFile In $objDir
$name=$objFile.name
if ($mask and instr($name,$mask)) or not $mask
$t=$t+'|'+$name
endif
Next
$FileList=split(substr($t,2),'|')
EndFunction


_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#76553 - 2003-08-31 01:26 AM Re: DirList with Owner of files
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
hmm...
radimus, nice code you have but what it addresses?

richard, do you live your weekends like normal ppl as haven't heard a reply for many hours! [Eek!]
_________________________
!

download KiXnet

Top
#76554 - 2003-08-31 02:37 PM Re: DirList with Owner of files
Richie19Rich77 Offline
Seasoned Scripter
*****

Registered: 2002-08-16
Posts: 624
Loc: London, England
Nope I have been on my computer all weekend.

Yeah looked at your code, and you code seems faster.

This is the 1st time I have see your code, will start using it.

Thanks

Top
#76555 - 2003-08-31 04:59 PM Re: DirList with Owner of files
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
can I ask for clarification [Wink]
are you saying that the SHELL object is faster than FSO?
or?
_________________________
!

download KiXnet

Top
#76556 - 2003-08-31 07:28 PM Re: DirList with Owner of files
Richie19Rich77 Offline
Seasoned Scripter
*****

Registered: 2002-08-16
Posts: 624
Loc: London, England
I will do some more testing, but beofre I saw your code, I was using DirList with my methold of getting the owner of files.

I found that FSO was quicker in conjuction with my metold of getting owner of files over using dirlist.

Top
#76557 - 2003-08-31 08:20 PM Re: DirList with Owner of files
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
FSO [Razz]
not my code, me not likey it [Big Grin]
_________________________
!

download KiXnet

Top
#76558 - 2003-09-01 01:12 AM Re: DirList with Owner of files
Richie19Rich77 Offline
Seasoned Scripter
*****

Registered: 2002-08-16
Posts: 624
Loc: London, England
Ok I have updated my script, this is so much better.

Using FSO.Files and FSO.SubFolders and then FileOwner to get ownership.

ProgressBar all working now, NO bugs found.

Works really fast.

 -




;    *********************************************************************************
;    * Script Name:        Display Owner of Files & Folders                         *
;    * Creator:        Richard Farthing - Queen Elizabeth Hospital NHS Trust        *
;    * Date:            1/09/03                                            *   
;    * Requirments        FileOwner Function, KiXforms v2.3.0 Beta 3,                * 
;    *                KiXtart 4.22 Beta 1                                    *
;    *********************************************************************************


Break On ; Break On to allow user to break out

;**********************************************************************************************************
;Checking that KiXtart Forms DLL is Registered
If NOT ReadValue("HKEY_CLASSES_ROOT\CLSID\{78F2E57A-21C9-467D-B20B-6836E9A36272}\InprocServer32""ThreadingModel") = "Both"
    MessageBox("KiXtart Forms Register DLL Failed, Please contact the IMT Helpdesk on x6049"
    "Displays Owner of Files & Folders - KiXtart Forms DLL Register Failed"0)
GoTo End
EndIf

;**********************************************************************************************************
$KiX = "D:\KiX" ; Set the KiX verable
Call $Kix+'\Functions\FileOwner().Kix' ; Calling the FileOwner() Function

;**********************************************************************************************************
;Type in the Drive Letters of the Shared Drive and the IMS Drive.

$FirstDrive = "C:\"
$SecondDrive = "D:\" ;Leave as "" if you do not want a second drive.

;**********************************************************************************************************
;Create Object to KiXtart Form
$FORM = CreateObject("Kixtart.Form")

;**********************************************************************************************************
;Main Form

$FORM.Caption                                = "Displays Owner of Files & Folders"
$FORM.ScaleHeight                            = 240
$FORM.ScaleWidth                            = 405
$FORM.FontSize                                = 8
$FORM.Center   

;**********************************************************************************************************
;Current Directory Label

$ListCurrentDir                            = $FORM.Label
$ListCurrentDir.Left                        = 5
$ListCurrentDir.Top                            = 30
$ListCurrentDir.Width                        = 380
$ListCurrentDir.Height                        = 20
$ListCurrentDir.FontSize                        = 8
$ListCurrentDir.FONTNAME                     = "Arial"
$ListCurrentDir.Enabled                        = 0

$lblCurrentDir                                = $FORM.Label("Current Folder: ")
$lblCurrentDir.Left                            = 5
$lblCurrentDir.Top                            = 15
$lblCurrentDir.Width                        = 120
$lblCurrentDir.Height                        = 20
$lblCurrentDir.FontSize                        = 8
$lblCurrentDir.FONTNAME                         = "Arial"
$lblCurrentDir.Enabled                        = 0

;**********************************************************************************************************
;ListView for OwnerList

$GetOwnerListBox                            = $FORM.Listview
$GetOwnerListBox.Left                        = 5
$GetOwnerListBox.Top                        = 55
$GetOwnerListBox.Width                        = 395
$GetOwnerListBox.Height                        = 160
$GetOwnerListBox.FontSize                    = 8
$GetOwnerListBox.FONTNAME                     = "Arial"
$GetOwnerListBox.View                         = 1
$GetOwnerListBox.SmallImageList                 = $Form.SmallImageList
$GetOwnerListBox.OnDoubleClick                = "ListView_DoubleClick()"
$=$GetOwnerListBox.Columns.Add("Name",250
$=$GetOwnerListBox.Columns.Add("Owner",125)
$FolderRoot = $FirstDrive

;**********************************************************************************************************
;Command Button for select Drive

If $SecondDrive
    $SelectDrive                                    = $FORM.CommandButton
    $SelectDrive.Left                                = 278
    $SelectDrive.Top                                = 7
    $SelectDrive.Width                                = 115
    $SelectDrive.Height                                = 20
    $SelectDrive.FontSize                            = 8
    $SelectDrive.FONTNAME                             = "Arial"
    $SelectDrive.Text                                = "Click to Browse "+$SecondDrive
    $SelectDrive.OnClick                            = "SelectDrive_Click()"
EndIf

;**********************************************************************************************************
;ProgressBar for DirList

$ProgressBar                                = $FORM.ProgressBar() 
$ProgressBar.Width                           = 300
$ProgressBar.Height                          = 15
$ProgressBar.Left                            = 50
$ProgressBar.Top                             = 220
$ProgressBar.Style                           = 2
$ProgressBar.Minimum                         = 0 
$ProgressBar.Enabled                         = 0

;**********************************************************************************************************
;Objects within Main Form

$FORM.Show ; Show Form

ListView() ; Call ListView Function

While $FORM.Visible
 $=Execute($FORM.DoEvents)
Loop
:End
Exit 1

;***********************************************************************************************************
;Call Functions & UDF's ****
;***********************************************************************************************************


;***********************************************************************************************************
;ListView Function

Function ListView()

Enabled(0; Call Enabled Function
$ListCurrentDir.Text = $FolderRoot ; Set Current Directory Label

$FSO = CreateObject("Scripting.FileSystemObject").GetFolder($FolderRoot)
$ProgressBar.Maximum = $FSO.SubFolders.Count + $FSO.Files.Count ; Set the maximum value of the ProgressBar to count of the Files and Folders Array
$=$GetOwnerListBox.Items.Add("..",47; Create Item for ".."           

$x = 1
;Folder Array
For Each $FolderListArray In $FSO.SubFolders
    $ProgressBar.Value = $x ; Set Value of ProgressBar to $x
    $Owner = Split(FileOwner($FolderListArray),"\";Split Object into an Array using "\"
    $=$GetOwnerListBox.Items.Add($FolderListArray.Name+"\",1;Add FolderName Item into Listview
    $GetOwnerListBox.Items($x).SubItems(1).Text = $Owner[Ubound($Owner)] ; Add Owner Item into ListView
    $x = $x + 1   
Next

;File Array
For Each $FileListArray In $FSO.Files
    $ProgressBar.Value = $x
    $Owner = Split(FileOwner($FileListArray),"\")
    $=$GetOwnerListBox.Items.Add($FileListArray.Name,0)
    $GetOwnerListBox.Items($x).SubItems(1).Text = $Owner[Ubound($Owner)] 
    $x = $x + 1   
Next

Enabled(1; Call the Enabled Function

EndFunction

;***********************************************************************************************************
;ListView Double Click Function

Function ListView_DoubleClick()
 
For Each $Item In $GetOwnerListBox.SelectedItems
    $ItemName = $Item.Text
    If $ItemName = ".." ; If ItemName = ".."
        If NOT $FolderRoot = SubStr($FolderRoot,1,2)+'\' ; If $FolderRoot does NOT Contain the 1st 2 charcters of FolderRoot
            $FolderRootSplit = Split($FolderRoot,"\"; Splits the FolderRoot into an Array on "\" charcter     
            ReDim PRESERVE $FolderRootSplit[Ubound($FolderRootSplit) -2; ReDim the FolderRootSplit Array and cut off the last 2 segments of the array but kee p the data
            $FolderRoot = Join($FolderRootSplit,"\")+'\' ; Re join the array and use "\" as the joining character
            $GetOwnerListBox.Items.Clear ; Clear the ListView of all Items
            ListView() ; Call the ListView Function
        EndIf
    Else
        If NOT InStr($ItemName,"\") = 0 ; If "\" is NOT in Selected ItemName
            $FolderRoot = $FolderRoot+"$ItemName" ; Set FolderRoot
            $GetOwnerListBox.Items.Clear ; Clear the ListView of all Items
            ListView() ; Call the ListView Function
        EndIf
    EndIf
Next   

EndFunction

;***********************************************************************************************************
;Set the FolderRoot Verable

Function SelectDrive_Click()

If SubStr($FolderRoot,1,3) = $FirstDrive 
    $FolderRoot = $SecondDrive
    $SelectDrive.Text = "Click to Browse "+$FirstDrive
Else
    $FolderRoot = $FirstDrive
    $SelectDrive.Text = "Click to Browse "+$SecondDrive
EndIf

$GetOwnerListBox.Items.Clear
ListView()

EndFunction

;***********************************************************************************************************
; Set the Enabled Function

Function Enabled($Option)
    $ProgressBar.Value = 0
    $GetOwnerListBox.Enabled = $Option

If $SelectDrive.Enabled = 0 OR $SelectDrive.Enabled = 1
     $SelectDrive.Enabled = $Option   
EndIf

EndFunction

;***********************************************************************************************************



[ 01. September 2003, 13:56: Message edited by: Richard Farthing ]

Top
#76559 - 2003-09-01 01:24 AM Re: DirList with Owner of files
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
see that you did cut of part of the postpreped code...
_________________________
!

download KiXnet

Top
#76560 - 2003-09-01 01:49 AM Re: DirList with Owner of files
Richie19Rich77 Offline
Seasoned Scripter
*****

Registered: 2002-08-16
Posts: 624
Loc: London, England
?? sorry what have I done. [Confused]

Someone answer me this simple thing.

Why can't I find the UBound of the following array.

code:
$FSO = CreateObject("Scripting.FileSystemObject").GetFolder($FolderRoot).Files

$ProgressBar.Maximum = Ubound($FSO) + 1

I can list each object within the $FSO, but it looks like it is not a true array.

??? any ideas

[ 01. September 2003, 02:22: Message edited by: Richard Farthing ]

Top
#76561 - 2003-09-01 03:06 AM Re: DirList with Owner of files
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
not easy to explain but that's how kix works with com-objects.
the array is not fully translated to kixtart array.
there might be a change in that in some future release.
until that, we just need to bare with it.
_________________________
!

download KiXnet

Top
#76562 - 2003-09-01 03:23 AM Re: DirList with Owner of files
Richie19Rich77 Offline
Seasoned Scripter
*****

Registered: 2002-08-16
Posts: 624
Loc: London, England
And a work around ???, if not I will just lose the progressbar.

Thanks for letting me know, otherwise I would have been pulling my hair out thinking it was a scripting problem on my part.

Thanks

Top
#76563 - 2003-09-01 03:31 AM Re: DirList with Owner of files
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
well.
I would not like to help you as I hate FSO...
you should check the .count property.

at least in shell-object there is that one.
_________________________
!

download KiXnet

Top
#76564 - 2003-09-01 01:22 PM Re: DirList with Owner of files
Richie19Rich77 Offline
Seasoned Scripter
*****

Registered: 2002-08-16
Posts: 624
Loc: London, England
Thanks works like a dream.

code:
$ProgressBar.Maximum = $FSO.SubFolders.Count + $FSO.Files.Count


Top
Page 1 of 1 1


Moderator:  Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
1 registered (Allen) and 675 anonymous users online.
Newest Members
batdk82, StuTheCoder, M_Moore, BeeEm, min_seow
17885 Registered Users

Generated in 0.069 seconds in which 0.028 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org