|
-
Sep 23rd, 2003, 05:06 PM
#1
The Dir way :
VB Code:
'Just add a listbox (List1)
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 Form_Load()
ListFiles "C:\", "txt"
End Sub
Has someone helped you? Then you can Rate their helpful post. 
-
Jun 16th, 2007, 09:16 AM
#2
New Member
Re: VB - List All The Files In A Directory
 Originally Posted by manavo11
The Dir way :
VB Code:
'Just add a listbox (List1)
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 Form_Load()
ListFiles "C:\", "txt"
End Sub
Hello,
Thank for the above code, works a treat but I'm just wondering if anyone could help me out with a similarly easy way to do the same but to list the names of directories ("folders") within a directory?
So, the above code works great, it lists all the files of whatever type in say "C:\folder1".
What I need is code to list all the directories within "C:\folder1", not including subdirectories.
If anyone could help I would be much obliged.
Thanks in advance.
-
Jun 18th, 2007, 01:26 PM
#3
Re: VB - List All The Files In A Directory
 Originally Posted by RedJam
What I need is code to list all the directories within "C:\folder1", not including subdirectories.
Code:
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const LB_DIR = &H18D
Private Const DDL_DIRECTORY = &H10
Private Const DDL_ARCHIVE = &H20
Private Const DDL_EXCLUSIVE = &H8000
Private Sub Command1_Click()
List1.Clear
SendMessage List1.hwnd, LB_DIR, DDL_EXCLUSIVE Or DDL_DIRECTORY, ByVal "C:\folder1\*.*"
End Sub
-
Dec 23rd, 2009, 01:24 AM
#4
New Member
Re: VB - List All The Files In A Directory
 Originally Posted by manavo11
The Dir way :
VB Code:
'Just add a listbox (List1)
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 Form_Load()
ListFiles "C:\", "txt"
End Sub
 Originally Posted by goldenix
just a note that none of the codes work in 2008.
this code is pretty close, the following is what i turned it into and used, in vb.net 2008:
Code:
Dim file As String
file = Dir$("D:\Documents and Settings\Compaq_Owner\My Documents\My Pictures\cards\*.png")
Do While Len(file)
ListBox2.Items.Add(Mid(file, 1, InStr(file, ".") - 1))
file = Dir$()
Loop
-
Jan 21st, 2010, 06:20 AM
#5
Member
Re: VB - List All The Files In A Directory
 Originally Posted by manavo11
The Dir way :
VB Code:
'Just add a listbox (List1)
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 Form_Load()
ListFiles "C:\", "txt"
End Sub
why do i get errors?
i added a list box named it List1
Error 1 Optional parameters must specify a default value. C:\Users\TECHKER\Desktop\PROGRAMMING\PROJECTS\WindowsApplication1\WindowsApplication1\Form5.vb 3 74 WindowsApplication1
Error 2 'Public ReadOnly Property Right() As Integer' has no parameters and its return type cannot be indexed. C:\Users\TECHKER\Desktop\PROGRAMMING\PROJECTS\WindowsApplication1\WindowsApplication1\Form5.vb 7 12 WindowsApplication1
Error 4 'Public Property Left() As Integer' has no parameters and its return type cannot be indexed. C:\Users\TECHKER\Desktop\PROGRAMMING\PROJECTS\WindowsApplication1\WindowsApplication1\Form5.vb 11 16 WindowsApplication1
Error 5 Type character '$' does not match declared data type 'Integer'. C:\Users\TECHKER\Desktop\PROGRAMMING\PROJECTS\WindowsApplication1\WindowsApplication1\Form5.vb 11 16 WindowsApplication1
Error 6 'AddItem' is not a member of 'System.Windows.Forms.ListBox'. C:\Users\TECHKER\Desktop\PROGRAMMING\PROJECTS\WindowsApplication1\WindowsApplication1\Form5.vb 17 13 WindowsApplication1
-
Mar 23rd, 2011, 12:44 PM
#6
New Member
Re: VB - List All The Files In A Directory
 Originally Posted by manavo11
The Dir way :
VB Code:
'Just add a listbox (List1)
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 Form_Load()
ListFiles "C:\", "txt"
End Sub
I'm attempting to use this code, it should do everything I need, but my Listbox comes up blank. Which was my problem before and hoped this code would help me fix this. I'm teaching myself how to use VB6 so please spell things out to me as though I were a little kid. Thank you.
-
Mar 24th, 2011, 11:10 AM
#7
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|