Results 1 to 3 of 3

Thread: Get file locations within directory

  1. #1

    Thread Starter
    Frenzied Member I_Love_My_Vans's Avatar
    Join Date
    Jan 2005
    Location
    In the PHP compiler
    Posts
    1,275

    Get file locations within directory

    Hi fellas

    Right consider i have a folder, like my pictures, for every file within that folder and subfolders, i need the path.

    eg

    c:\documents and settings\ben\my documents\my pictures\logo.gif
    c:\documents and settings\ben\my documents\my pictures\vbf\program.jpg
    c:\documents and settings\ben\my documents\my pictures\site.png

    Any ideas?

    Thanks all, ILMV

  2. #2
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    376

    Re: Get file locations within directory

    I did something similar to this while searching for a file and found some helpful code so here ya go hope it helps


    VB Code:
    1. Public Function RecurseFolderList(FolderName As String) As Boolean
    2.     On Error GoTo errHandler
    3.     Dim fso, f, fc, fj, f1
    4.    
    5.     Set fso = CreateObject("Scripting.FileSystemObject")
    6.    
    7.     If Err.Number > 0 Then
    8.         RecurseFolderList = False
    9.         Exit Function
    10.     End If
    11.     On Error GoTo errHandler
    12.     If fso.FolderExists(FolderName) Then
    13.         Set f = fso.GetFolder(FolderName)
    14.         Set fc = f.Subfolders
    15.         Set fj = f.Files
    16.        
    17.         For Each f1 In fc
    18.             RecurseFolderList (f1)
    19.         Next
    20.         For Each f1 In fj
    21.             List1.AddItem f1
    22.             DoEvents: DoEvents
    23.         Next
    24.         Set f = Nothing
    25.         Set fc = Nothing
    26.         Set fj = Nothing
    27.         Set f1 = Nothing
    28.        
    29.     Else
    30.         RecurseFolderList = False
    31.     End If
    32.     Set fso = Nothing
    33.    
    34. Exit Function
    35. errHandler:
    36. Resume Next
    37. End If
    38.    
    39. End Function
    40.  
    41. Private Sub Form_Load()
    42.     Me.Show
    43.     RecurseFolderList "C:\documents and settings"
    44.     MsgBox "Done"
    45.     End
    46. End Sub

    be sure to change the dir to what u want as this setting will give many results

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Get file locations within directory

    I buried this gif file nine folders deep and it found it very quickly.
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function SearchTreeForFile Lib "imagehlp" _
    4. (ByVal RootPath As String, ByVal InputPathName As String, _
    5. ByVal OutputPathBuffer As String) As Long
    6.  
    7. Private Const MAX_PATH = 260
    8.  
    9. Private Sub Command1_Click()
    10.     Dim tempStr As String, Ret As Long
    11.     'create a buffer string
    12.     tempStr = String(MAX_PATH, 0)
    13.     'returns 1 when successfull, 0 when failed
    14.     Ret = SearchTreeForFile("c:\", "ivy.gif", tempStr)
    15.     If Ret <> 0 Then
    16.         MsgBox "Located file at " + Left$(tempStr, InStr(1, tempStr, Chr$(0)) - 1)
    17.     Else
    18.         MsgBox "File not found!"
    19.     End If
    20. End Sub

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