how can I retrieve all filenames in a folder on my LAN?
Thanks
Printable View
how can I retrieve all filenames in a folder on my LAN?
Thanks
Use the Findfirstfile,FindNextFile,Findclose API. It works great:)
Using the API can be done really quickly, especially if you have done it once, put it in a module, and then made it easy to call from your app.
US101 I don't use API often. I would love to learn the way you are suggesting. Can you post that module here?
thanks
If you only need the files in a directory try the function below, use dir as it is easy and unless you have a million files in the directory the performance should be reasonable.
If you are wanting to enumerate all the files in the directory and all of its subdirectories then go for the api Findfirstfile and FindNextfile calls http://pages.about.com/vbmakai/getfiles.htm
Code:Private Sub Command1_Click()
Dim a As Variant, i As Long
a = EnumDir("c:", "tmp")
If Not IsNull(a) Then
For i = 1 To UBound(a)
MsgBox a(i)
Next i
Else
MsgBox "No files found In that directory matching " & _
"the extension If you passed one."
End If
End Sub
Function EnumDir(ByVal DirPath As String, Optional ByVal Ext As String) As Variant
'Nucleus
Dim files() As String: ReDim files(1 To 100)
Dim fname$
Dim lfcount&
EnumDir = Null 'initialise Function
If Right(DirPath, 1) <> "\" Then DirPath = DirPath & "\" 'add path separator If missing
If Len(Dir$(DirPath, vbDirectory)) Then
fname = Dir$(DirPath & "*.*", vbNormal + vbHidden + vbSystem) 'you can include vbdirectory If you need To
Do While Len(fname)
If Len(Ext) = 0 Or (Len(Ext) And Right(fname, Len(fname) - InStr(1, fname, ".")) = Ext) Then
lfcount = lfcount + 1
files(lfcount) = DirPath & fname 'path And file name To array
If lfcount Mod 100 = 0 Then ReDim Preserve files(1 To lfcount + 100) 'resize array As required
End If
fname = Dir$
Loop
If lfcount Then ReDim Preserve files(1 To lfcount): EnumDir = files
End If
End Function