How can i get a list of a file with a file type in one directory. i would like a way that is done all through code
Printable View
How can i get a list of a file with a file type in one directory. i would like a way that is done all through code
I didn't quite understand the question, however, if you wanted a list of all the .tmp files in a directory, here is how to do it.
Code:Option Explicit
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
Thats what i was looking for
can i get it to do sub dirs as well