How to find the author of a TXT File? (1 Viewer)

vtrm

Registered User.
Local time
Today, 23:26
Joined
Jun 8, 2005
Messages
21
I have a TXT file in a folder (C:\temp\tfile.txt) I have the file's Title property set to 'Log file' and Author property as 'John'

How can I get these property values into my variables var1 & var2
 

bonekrusher

Registered User.
Local time
Today, 13:26
Joined
Nov 19, 2005
Messages
266
I am not sure how to get the author, but this gets all the other properties of a file:

Code:
Dim fso As Object
Dim f As Object 
Dim TBfile As String

TBfile = ("C:\fileName.txt")
    
    Set fso = CreateObject("Scripting.FileSystemObject")
     Set f = fso.GetFile(TBfile)
    
    Me.Text1 = f.Name
    
    ' Or you can use f.Name f.drive f.ParentFolder f.Path f.Size f.DateCreated_
   ' f.DateLastAccessed f.DateLastModified f.shortname f.ShortPath f.Type
    
    Set f = Nothing
    Set fso = Nothing
 

bonekrusher

Registered User.
Local time
Today, 13:26
Joined
Nov 19, 2005
Messages
266
I've searched high and low over the internet, but I cant seem to find how to pull the Author/Creator from a file. Any one else have any ideas?
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 15:26
Joined
Feb 28, 2001
Messages
27,001
If you were using Word, you could open Word as an application object and see the document's properties including author. This is because Word "exposes" document properties to Active X.

If the creator of the document isn't Word, there is some question about what you can see. It depends on whether the document creator program (Wordpad? Notepad?) left anything behind. If it is there, try opening the file with Word and look into Word Help for the properties of a document object.
 

bonekrusher

Registered User.
Local time
Today, 13:26
Joined
Nov 19, 2005
Messages
266
Yeah, with an active document you can pull the author with

objwd.activedocument.author (or something like that)
 

MarkK

bit cruncher
Local time
Today, 13:26
Joined
Mar 17, 2004
Messages
8,178
Here's code I modified slightly
Code:
Sub Test()
   FileDetails "C:", "Autoexec.bat"
End Sub

Sub FileDetails(fpath As String, fname As String)
   Dim objShell As Object
   Dim objFolder As Object
   Dim vFile As Variant
   Dim i As Integer
   
   Set objShell = CreateObject("Shell.Application")
   Set objFolder = objShell.NameSpace(CStr(fpath))

   For Each vFile In objFolder.Items
      If vFile = fname Then
         For i = 0 To 33
            Debug.Print _
               i & vbTab & _
               objFolder.GetDetailsOf(objFolder.Items, i) & ": " & _
               objFolder.GetDetailsOf(vFile, i)
         Next
      End If
   Next
   
   Set objFolder = Nothing
   Set objShell = Nothing
   
End Sub
From here...
http://www.microsoft.com/technet/scriptcenter/scripts/storage/files/stfivb32.mspx
...which returns the extended properties of a file.
 

bonekrusher

Registered User.
Local time
Today, 13:26
Joined
Nov 19, 2005
Messages
266
Once this is run, how is the information displayed. I run the code and nothing happens?????


lagbolt said:
Here's code I modified slightly
Code:
Sub Test()
   FileDetails "C:", "Autoexec.bat"
End Sub

Sub FileDetails(fpath As String, fname As String)
   Dim objShell As Object
   Dim objFolder As Object
   Dim vFile As Variant
   Dim i As Integer
   
   Set objShell = CreateObject("Shell.Application")
   Set objFolder = objShell.NameSpace(CStr(fpath))

   For Each vFile In objFolder.Items
      If vFile = fname Then
         For i = 0 To 33
            Debug.Print _
               i & vbTab & _
               objFolder.GetDetailsOf(objFolder.Items, i) & ": " & _
               objFolder.GetDetailsOf(vFile, i)
         Next
      End If
   Next
   
   Set objFolder = Nothing
   Set objShell = Nothing
   
End Sub
From here...
http://www.microsoft.com/technet/scriptcenter/scripts/storage/files/stfivb32.mspx
...which returns the extended properties of a file.
 

bonekrusher

Registered User.
Local time
Today, 13:26
Joined
Nov 19, 2005
Messages
266
Nevermind, I figured it out!

Here is what i need to do to get the author of the TXT file:

Code:
Sub Test()
   FileDetails "C:\temp2\", "export.txt"
End Sub

Sub FileDetails(fpath As String, fname As String)
   Dim objShell As Object
   Dim objFolder As Object
   Dim vFile As Variant
   Dim i As Integer
   Dim getit
   Set objShell = CreateObject("Shell.Application")
   Set objFolder = objShell.NameSpace(CStr(fpath))

   For Each vFile In objFolder.Items
            getit = _
               objFolder.GetDetailsOf(objFolder.Items, 9) & ": " & _
               objFolder.GetDetailsOf(vFile, 9)
    
   Next

MsgBox getit
   Set objFolder = Nothing
   Set objShell = Nothing
   
End Sub

Also, this was helpful from the links you guys provided:
(http://www.microsoft.com/technet/scriptcenter/guide/sas_fil_lunl.mspx?mfr=true)
 

MarkK

bit cruncher
Local time
Today, 13:26
Joined
Mar 17, 2004
Messages
8,178
Extended File Property, Author

Here's a more efficient, more user friendly version. Checks that file exists, splits file path internally, doesn't loop through the folder, but references the member of the collection directly.
Code:
Function GetFileAuthor(filespec As String) As String
[COLOR="Green"]'  Returns the extended file property of a file, in this case the author
'  Checks that 'filespec' exists and raises an error if not
'  Splits the filename from the path locally[/COLOR]
   Dim obj As Object
   Dim fso As Object
   
[COLOR="Green"]   'create scripting object to check for existence and manage file path and name[/COLOR]
   Set fso = CreateObject("Scripting.FileSystemObject")
[COLOR="Green"]   'check that filespec exists[/COLOR]
   If fso.FileExists(filespec) Then
[COLOR="Green"]      'if so, create shell app[/COLOR]
      Set obj = CreateObject("Shell.Application")
[COLOR="Green"]      'reference namespace method of shell application[/COLOR]
      With obj.NameSpace(fso.GetParentFolderName(filespec))
[COLOR="Green"]         'return extended file property 9 of file indexed by name[/COLOR]
         GetFileAuthor = .GetDetailsOf(.Items.Item(fso.GetFileName(filespec)), 9)
      End With
   Else
[COLOR="Green"]      'file does not exist, so raise custom error[/COLOR]
      Err.Raise 40001, "GetFileAuthor()", "File '" & filespec & "' not found"
   End If
   
End Function
Cheers,
 

bonekrusher

Registered User.
Local time
Today, 13:26
Joined
Nov 19, 2005
Messages
266
I was having some fun with your code and tweaked it a little. This will open up a file dialog, select a file and load all the info into a list box.

Thanks for the help!

Code:
Dim path As String
Dim TBfile As String
Dim fullpath As String
Dim FD As FileDialog
Dim name As String
Dim loc As String
Dim obj As Object
Dim getit
Dim f As Object
Dim i
 
    Set FD = Application.FileDialog(msoFileDialogFilePicker)
    Dim vrtSelectedItem As Variant
    With FD
         .AllowMultiSelect = False
         .Filters.Add "PDF", "*.pdf", 1
         .Filters.Add "Word", "*.doc", 1
         .Filters.Add "Excel", "*.xls", 1
         .Filters.Add "Any", "*.*", 1
         .ButtonName = "Get Some!"
         .Title = "Find File"

         
         If .Show = -1 Then
            For Each vrtSelectedItem In .SelectedItems
            name = Dir(vrtSelectedItem, vbSystem)
                Dim fso As Object

    Set fso = CreateObject("Scripting.FileSystemObject")
     Set f = fso.GetFile(name)

   'create scripting object to check for existence and manage file path and name
   Set fso = CreateObject("Scripting.FileSystemObject")
   'check that filespec exists
   If fso.FileExists(f.path) Then
      'if so, create shell app
      Set obj = CreateObject("Shell.Application")
      'reference namespace method of shell application
      With obj.NameSpace(fso.GetParentFolderName(f.path))
         'return extended file property 9 of file indexed by name
         For i = 0 To 34
         getit = .GetDetailsOf(.Items, i) & ":" & .GetDetailsOf(.Items.Item(fso.GetFileName(f.path)), i)
     Me.List2.AddItem Item:=getit
     Next
     End With
   Else
      'file does not exist, so raise custom error
      Err.Raise 40001, "GetFileAuthor()", "File '" & f.path & "' not found"
   End If

      
            Next vrtSelectedItem
      
          Else
        End If
    End With
    
    Set FD = Nothing
    Set f = Nothing
    Set fso = Nothing
 

MarkK

bit cruncher
Local time
Today, 13:26
Joined
Mar 17, 2004
Messages
8,178
Thanks. Nice implementation.
Cheers krusher.
Mark
 

Users who are viewing this thread

Top Bottom