PDA

View Full Version : VBS - find creator of latest file in folder and subfolder



OTWarrior
05-19-2011, 03:21 AM
Hello

I am trying to write a script to search through a folder and it's subfolders, reporting back the file name and the user who created it.

At the moment, what I have only searches one folder and I cannot find any way of finding who created the file. I have looked through the Scripting.FileSystemObject library and cannot see a "Creator" or "Author" member.

This is the code so far:

dim sMostRecent, dMostRecent
dim nID
dim Dept
nID = inputbox("Please enter ID")
MostRecent("x:\" & nSwift & "\DOM\")
msgbox sMostRecent, dMostRecent

Sub MostRecent (sFolder)
Set oFSO = CreateObject("Scripting.FileSystemObject")
dMostRecent = 0
sMostRecent = ""

For Each oFile In oFSO.GetFolder(sFolder).Files
dFileDate = oFile.DateLastModified
If dFileDate > dMostRecent Then
dMostRecent = dFileDate
sMostRecent = "File: " & oFile.Name & vbcrlf & "Created by: " & oFile.Name
End If
Next

End Sub

Charlize
05-19-2011, 06:21 AM
Maybe you could try this one :
Sub test_attributes()
Dim sFile As Variant
Dim oShell As Object, oDir As Object
Dim i As Long
Set oShell = CreateObject("Shell.Application")
'directory you want to test
Set oDir = oShell.Namespace("c:\data")
For Each sFile In oDir.Items
For i = 0 To 40
'watch the debug window, I believe 8 is the author
'0 = filename, 1 = size ...
Debug.Print i, oDir.GetDetailsOf(sFile, i)
Next i
Next sFile
End SubCharlize

OTWarrior
05-20-2011, 02:14 AM
I tried that code Charlize, but it fails on the first few lines. I have had this problem with VB script where I can't seem to dimension a item as a particular type

For example

Dim I as Long

for a vbscript only works as

Dim I

Is there another way of doing this in a vbscript?
Thanks for the code though.

I had another thought with what I am trying to do. It's most likely the last email or html file which will be the latest item. Would there be a way of reading either type of file properties?

Or simply opening the latest file in the folder regardless of file type?

Charlize
05-20-2011, 02:53 AM
not sure, what about thisSub test_attributes()
Dim sFile
Dim oShell, oDir
Dim i
Dim themessage
Set oShell = CreateObject("Shell.Application")
'directory you want to test
Set oDir = oShell.Namespace("c:\data")
For Each sFile In oDir.Items
For i = 0 To 40
'Maybe microsoft has got more info on those numbers
'9 = guess it's name in properties of file
'0 = filename, 2 = what kind of file, 1 = size ...
'3 = saved, 4 = created, 5 = last acces
themessage = themessage & i & " - " & oDir.getdetailsof(sFile, i) & Chr(13)
'Debug.Print i, oDir.getdetailsof(sFile, i)
Next i
MsgBox themessage
themessage = ""
Next sFile
End SubCharlize

OTWarrior
05-20-2011, 03:17 AM
Very close. Had to mod it to:

Dim sFile
Dim oShell, oDir
Dim i
Dim themessage
Dim nSwift
nSwift = inputbox("Please enter ID")

Set oShell = CreateObject("Shell.Application")
'directory you want to test
Set oDir = oShell.Namespace("x:\" & nSwift & "\DOM\")
For Each sFile In oDir.Items
For i = 0 To 40
'Maybe microsoft has got more info on those numbers
'9 = guess it's name in properties of file
'0 = filename, 2 = what kind of file, 1 = size ...
'3 = saved, 4 = created, 5 = last acces
themessage = themessage & i & " - " & oDir.getdetailsof(sFile, i) & Chr(13)
'Debug.Print i, oDir.getdetailsof(sFile, i)
Next
MsgBox themessage
themessage = ""
Next


Now I know I can get the properties, I just have to figure out how to only use the latest item, and extract the user who created it (it's 8)

Thank you very much for your help :D

OTWarrior
05-20-2011, 03:36 AM
Got it :)

Dim sFile
Dim oShell, oDir
Dim i
Dim nSwift
Dim User
nSwift = inputbox("Please enter Swift ID to find last User to create a file in this folder","DOM")
On Error Resume Next

Set oShell = CreateObject("Shell.Application")
'directory you want to test
Set oDir = oShell.Namespace("x:\" & nSwift & "\DOM\")
For Each sFile In oDir.Items
For i = 0 To 40
User= oDir.getdetailsof(sFile, 8)
Next
Next
MsgBox "Last User was: " & vbrclf & Replace(Mid(User, 9, len(User)), ".", " "),,"DOM"


Thanks for your help :)

Charlize
05-20-2011, 03:43 AM
Great you found it.

But what does that user means. Is it saved by, last accessed (opened), created by ... ? Or it doesn't matter, the os changes the username whatever action you do ?

OTWarrior
05-20-2011, 05:12 AM
I am fairly sure it's who created the file, it doesn't seem to change if I edit anything on the file.