Hi all,
I need to list all files in directory.
How i can do this.
Thank you
Hi all,
I need to list all files in directory.
How i can do this.
Thank you
since you are on VB6, skip the .NET DirectoryInfo approach shared and build on ’s idea by adding recursion, full paths, and safe handling of attributes. The key detail: Dir$ keeps internal state and cannot be used recursively, so gather subfolders first, then recurse into them. (learn.microsoft.com)
' VB6: list files (optionally by pattern) including subfolders
Option Explicit
Private Function JoinPath(ByVal dir As String, ByVal name As String) As String
If dir = "" Then
JoinPath = name
ElseIf Right$(dir, 1) = "\" Then
JoinPath = dir & name
Else
JoinPath = dir & "\" & name
End If
End Function
Public Sub ListFilesRecursive(ByVal root As String, ByVal pattern As String, ByVal target As ListBox)
Dim n As String, full As String
Dim subdirs As New Collection, d As Variant
' Files in current folder
n = Dir$(JoinPath(root, pattern), vbNormal Or vbHidden Or vbSystem Or vbReadOnly)
Do While Len(n) > 0
full = JoinPath(root, n)
If (GetAttr(full) And vbDirectory) = 0 Then target.AddItem full
n = Dir$()
Loop
' Collect subfolders first (avoid clobbering Dir state)
n = Dir$(JoinPath(root, "*"), vbDirectory Or vbHidden Or vbSystem)
Do While Len(n) > 0
If n <> "." And n <> ".." Then
full = JoinPath(root, n)
If (GetAttr(full) And vbDirectory) <> 0 Then subdirs.Add full
End If
n = Dir$()
Loop
' Recurse
For Each d In subdirs
ListFilesRecursive CStr(d), pattern, target
Next
End Sub Notes:
GetAttr with vbDirectory to tell files from folders and to include/exclude hidden/system items as needed. (learn.microsoft.com)Dir$ can stumble. In those edge cases, switch to the Unicode Win32 APIs (FindFirstFileW/FindNextFileW) and, on modern Windows, long-path support (up to 32,767 chars) when opted in. (learn.microsoft.com)Scripting.FileSystemObject lets you iterate GetFolder(root).Files and SubFolders cleanly. (learn.microsoft.com)Call it like: ListFilesRecursive "D:\musicfolder", "*.mp3", List1.
Jump to Post— Jx_Man 987See if this help :
Private Sub ListFiles(strPath As String, Optional Extention As String) 'Leave Extention blank for all files' Dim File As String If Right$(strPath, 1) <> "\" Then strPath = strPath & "\" If Trim$(Extention) = "" Then Extention = "*.*" ElseIf Left$(Extention, 2) <> …
Dim di As New IO.DirectoryInfo("Your directory path")
Debug.Print("di: " + di.ToString)
Dim diar1 As IO.FileInfo() = di.GetFiles()
Dim dra As IO.FileInfo
'list the names of all files in the specified directory
For Each dra In diar1
lstFiles.Items.Add(dra) 'all files present in the directory are viewed in list box
Next
hope it helps u...
See if this help :
Private Sub ListFiles(strPath As String, Optional Extention As String)
'Leave Extention blank for all files'
Dim File As String
If Right$(strPath, 1) <> "\" Then strPath = strPath & "\"
If Trim$(Extention) = "" Then
Extention = "*.*"
ElseIf Left$(Extention, 2) <> "*." Then
Extention = "*." & Extention
End If
File = Dir$(strPath & Extention)
Do While Len(File)
List1.AddItem File
File = Dir$
Loop
End Sub
Private Sub Command1_Click()
ListFiles "D:\musicfolder", "*"
' Or ListFiles "D:\musicfolder", "" leave it blank to get all files'
End Sub
: Than you for replying but i'm using vb6 to do this.
: Thank you for complete code.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.