Results 1 to 6 of 6

Thread: list all files help needed

  1. #1
    tamathaltarak
    Guest

    list all files help needed

    Hi,

    I need to build some code to do the following.

    Read a directory and all files and all subfolders not matter how deep they go and output this into a database.

    e.g. start from a folder say \c\docs\data\

    i want to be able to read all folders and files after the above path and output them to the follwing access table.

    tblOutput
    ID - autonumber
    txtFilePath - the actual path of the file e.g. \c\docs\data\
    txtFileName - the name of the file including the extension
    intFileSize - the size of the file in butes or kilobytes or whatever

    Has anyone done this before? and if so can you help me please

    thanks

  2. #2
    Black Cat JoshT's Avatar
    Join Date
    Nov 2000
    Location
    WNY, USA
    Posts
    4,032
    Try looking at http://www.mvps.org/vbnet/ - they have examples to do what you need for the file searching ( http://www.mvps.org/vbnet/index.html...renumbasic.htm ).
    Josh
    Get these: Mozilla Opera OpenBSD
    I have books for sale: "MCSD in a Nutshell" and "VB Distributed Exam Cram" - PM me for details. Will also trade for a decent ATX Pentium 2 MB/CPU/RAM combo.

  3. #3
    tamathaltarak
    Guest

    Arrow thanks but...

    Hi,

    Looked around the 2 sites you gave and found not much joy. After searching these forums I found some code using the FSO. I am able to start in a folder and look for all files and output to a listbox. With some simple mods I managed to output to a database but only the filename including the path. The code I used is below.

    VB Code:
    1. Private Sub Command1_Click()
    2.     List1.Clear
    3.     Label2.Caption = "0 files found"
    4.     Screen.MousePointer = vbHourglass
    5.     Call GetAllDirsFrom(Dir1.Path, txtExtension, List1)
    6.     Label2.Caption = List1.ListCount & " files found"
    7.     Screen.MousePointer = vbDefault
    8. End Sub

    The above code calls the GetAllDirsFrom

    VB Code:
    1. Public Function GetAllDirsFrom(ByVal pstrDir As String, ByVal Extension As String, _
    2.                                ByVal ListBox As ListBox)
    3.     Dim fso As FileSystemObject
    4.     Dim fldrMain As Folder
    5.     Dim fldrsSub As Folders
    6.     Dim fldr As Folder
    7.     Set fso = New FileSystemObject
    8.     Set fldrMain = fso.GetFolder(pstrDir & "\")
    9.     If Right(fldrMain.Path, 1) = "\" Then
    10.         addallfilesfrom Left(fldrMain.Path, Len(fldrMain.Path) - 1), Extension, ListBox
    11.     Else
    12.         addallfilesfrom fldrMain.Path, Extension, ListBox
    13.     End If
    14.     'Recurse subdirectories
    15.     Set fldrsSub = fldrMain.SubFolders
    16.     For Each fldr In fldrsSub
    17.         GetAllDirsFrom fldr.Path, Extension, ListBox
    18.     Next fldr
    19.     ListBox.Refresh
    20. End Function

    The above function then calls the addallfilesfrom function

    VB Code:
    1. Public Function addallfilesfrom(ByVal pstrDir, ByVal Extension As String, _
    2.                                 ByVal ListBox As ListBox)
    3.        
    4.     Dim strfile As String
    5.     Dim strfilename As String
    6.     Dim dblfilesize As Integer
    7.     strfile = pstrDir & "\" & dir(pstrDir & "\*." & Extension)
    8.     Do Until strfile = pstrDir & "\"
    9.         ListBox.AddItem strfile
    10.         ListBox.ListIndex = ListBox.ListCount - 1
    11.         Form1.Label2.Caption = Form1.List1.ListCount & " files found"
    12.         Form1.Label3.Caption = pstrDir
    13.         strfile = pstrDir & "\" & dir
    14.         AddName strfile
    15.         DoEvents
    16.     Loop
    17. End Function
    In the above function I added my own function call to AddName

    VB Code:
    1. Public Function AddName(strfile As String)
    2. Dim MyDB As Database
    3. Set MyDB = OpenDatabase("c:\documents and settings\leighm\desktop\vb file folder stuff\fileoutput.mdb")
    4. Dim MySQL As String
    5. MySQL = "SELECT * from [tblFilePath]"
    6. Dim rstTemp As Recordset
    7. Set rstTemp = MyDB.OpenRecordset(MySQL)
    8.     With rstTemp
    9.         .AddNew
    10.         !txtFilePath = strfile
    11. '        !txtFileName = not done yet
    12. '        !intFileSize = not done yet
    13.         .Update
    14.     End With
    15. End Function

    This all works and updates my table with the filepath etc...

    BUT

    There is always a but..........hehehe

    1) I need to be able to grab the file size for each file and pass this into the AddName fucntion
    2) I need to be able to add the filename for each file into the table (not as the above does which will be c:\autoexec.bat)
    3) the full path not including the filename to go into !txtFilePath

    Can anyone help on this or alter the above code to do this.

    The functions came from this forum somewhere (sorry I misplaced the user who uploaded but all credit for the design goes to him/her with the exception of the additional db stuff which I inserted)

  4. #4
    Black Cat JoshT's Avatar
    Join Date
    Nov 2000
    Location
    WNY, USA
    Posts
    4,032
    I'm not too familiar with the FSO's capabilities, but if you use the API the WIN32_FIND_DATA structure you pass to FindFirstFile/FindNextFile functions will be set the file size, short name, access dates, etc.
    Josh
    Get these: Mozilla Opera OpenBSD
    I have books for sale: "MCSD in a Nutshell" and "VB Distributed Exam Cram" - PM me for details. Will also trade for a decent ATX Pentium 2 MB/CPU/RAM combo.

  5. #5
    tamathaltarak
    Guest

    Arrow

    thanks for the reply Josh,

    do you have any example code of this ???

    I would rather steer clear of the FSO if possible and would prefer to use the API functions. Speed reasons.

  6. #6
    Black Cat JoshT's Avatar
    Join Date
    Nov 2000
    Location
    WNY, USA
    Posts
    4,032
    Here's some code I wrote for finding and deleting *.bak files. See if you can modify it to suit your needs:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Const INVALID_HANDLE_VALUE As Long = -1&
    4. Private Const MAX_PATH As Long = 260&
    5. Private Const FILE_ATTRIBUTE_DIRECTORY As Long = &H10&
    6. Private Const FILE_ATTRIBUTE_ARCHIVE As Long = &H20&
    7. Private Const FILE_ATTRIBUTE_COMPRESSED As Long = &H800&
    8. Private Const FILE_ATTRIBUTE_HIDDEN As Long = &H2&
    9. Private Const FILE_ATTRIBUTE_NORMAL As Long = &H80&
    10. Private Const FILE_ATTRIBUTE_READONLY As Long = &H1&
    11. Private Const FILE_ATTRIBUTE_SYSTEM As Long = &H4&
    12. Private Const FILE_ATTRIBUTE_TEMPORARY As Long = &H100&
    13.  
    14.  
    15. Private Type FILETIME
    16.         dwLowDateTime As Long
    17.         dwHighDateTime As Long
    18. End Type
    19.  
    20. Private Type WIN32_FIND_DATA
    21.         dwFileAttributes As Long
    22.         ftCreationTime As FILETIME
    23.         ftLastAccessTime As FILETIME
    24.         ftLastWriteTime As FILETIME
    25.         nFileSizeHigh As Long
    26.         nFileSizeLow As Long
    27.         dwReserved0 As Long
    28.         dwReserved1 As Long
    29.         cFileName As String * MAX_PATH
    30.         cAlternate As String * 14
    31. End Type
    32.  
    33. Private Type SYSTEMTIME
    34.         wYear As Integer
    35.         wMonth As Integer
    36.         wDayOfWeek As Integer
    37.         wDay As Integer
    38.         wHour As Integer
    39.         wMinute As Integer
    40.         wSecond As Integer
    41.         wMilliseconds As Integer
    42. End Type
    43.  
    44. Private Declare Sub GetSystemTime Lib "kernel32" (lpSystemTime As SYSTEMTIME)
    45.  
    46. Private Declare Function FileTimeToSystemTime Lib "kernel32" _
    47.     (lpFileTime As FILETIME, lpSystemTime As SYSTEMTIME) As Long
    48.  
    49. Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
    50.  
    51. Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" _
    52.     (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
    53.  
    54. Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" _
    55.     (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
    56.    
    57. Public Declare Function DeleteFile Lib "kernel32" Alias "DeleteFileA" _
    58.     (ByVal lpFileName As String) As Long
    59.  
    60. Private Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" _
    61.     (ByVal lpFileName As String) As Long
    62.    
    63. Private Declare Function SetFileAttributes Lib "kernel32" Alias "SetFileAttributesA" _
    64.     (ByVal lpFileName As String, ByVal dwFileAttributes As Long) As Long
    65.  
    66. Public Function FileSearch(ByVal strFolder As String, LB As ListBox) As Boolean
    67.     Dim hFile As Long 'handle to a file
    68.     Dim WFD As WIN32_FIND_DATA
    69.     Dim strTemp As String
    70.    
    71.     If Len(strFolder) <= 0 Then
    72.         FileSearch = False
    73.         Exit Function
    74.     End If
    75.    
    76.     If Right$(strFolder, 1) <> "\" Then
    77.         strFolder = strFolder & "\"
    78.     End If
    79.    
    80.     hFile = FindFirstFile(strFolder & "*" & vbNullChar, WFD)
    81.    
    82.     If hFile <> INVALID_HANDLE_VALUE Then
    83.         strTemp = Left$(WFD.cFileName, InStr(1, WFD.cFileName, vbNullChar) - 1)
    84.         If (WFD.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) = FILE_ATTRIBUTE_DIRECTORY Then
    85.             strTemp = strTemp & "\"
    86.             If Right$(strTemp, 2) <> ".\" Then Call FileSearch(strFolder & strTemp, LB)
    87.         ElseIf Right$(strTemp, 4) = ".bak" Then
    88.             LB.AddItem strFolder & strTemp
    89.         End If
    90.    
    91.         Do While FindNextFile(hFile, WFD)
    92.             strTemp = Left$(WFD.cFileName, InStr(1, WFD.cFileName, vbNullChar) - 1)
    93.             If (WFD.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) = FILE_ATTRIBUTE_DIRECTORY Then
    94.                 strTemp = strTemp & "\"
    95.                 If Right$(strTemp, 2) <> ".\" Then Call FileSearch(strFolder & strTemp, LB)
    96.             ElseIf Right$(strTemp, 4) = ".bak" Then
    97.                 'older than 7 days
    98.                 If DateDiff("d", GetDate(WFD.ftLastAccessTime), Now) > 7 Then LB.AddItem strFolder & strTemp '& " -- " & GetDate(WFD.ftLastAccessTime)
    99.             End If
    100.         Loop
    101.        
    102.         FileSearch = True
    103.         Call FindClose(hFile)
    104.     Else
    105.         FileSearch = False
    106.     End If
    107.    
    108. End Function
    109.  
    110. Private Function GetDate(FT As FILETIME) As String
    111.     Dim ST As SYSTEMTIME
    112.  
    113.     If FileTimeToSystemTime(FT, ST) <> 0 Then
    114.         GetDate = ST.wMonth & "/" & ST.wDay & "/" & ST.wYear
    115.     Else
    116.         GetDate = ""
    117.     End If
    118. End Function
    Josh
    Get these: Mozilla Opera OpenBSD
    I have books for sale: "MCSD in a Nutshell" and "VB Distributed Exam Cram" - PM me for details. Will also trade for a decent ATX Pentium 2 MB/CPU/RAM combo.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width