|
-
Apr 17th, 2008, 04:52 PM
#1
Thread Starter
Member
Adding File Names In A Folder To a Listbox
I wanted to add all the names of the files in a folder in my computer to a list box in my vb program, so i used:
Code:
Do
List1.AddItem Dir("G:\foler1\folder2\")
Loop
So i expect to get the file names in the path i've chosen ("G:\foler1\folder2\") to be added to List1. But there are 2 problems:
- the computer doesn't stop looping, so i need a code to tell it stop if there are no more files
- if the loop works, still the the program only adds the first file name to the listbox and keeps adding the first one in the loop.
a little help on this please, asap. thank u
-
Apr 17th, 2008, 04:56 PM
#2
Re: Adding File Names In A Folder To a Listbox
Try this...
Code:
Sub addfilenames()
Dim fold As Scripting.Folder
Dim fil As File
Dim fso As New FileSystemObject
'Replace this with your folder name
Set fold = fso.GetFolder("S:\aaa")
For Each fil In fold.Files
List1.AddItem fil.Name
Next fil
End Sub
Edit: Set reference to Microsoft Scripting Runtime library
Last edited by Siddharth Rout; Apr 17th, 2008 at 05:00 PM.
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Apr 17th, 2008, 04:59 PM
#3
Re: Adding File Names In A Folder To a Listbox
I wouldn't wanna add a dependency to the scripting object for listing files...
Code:
Dim strPath As String, strFile As String
strPath = "G:\foler1\folder2\"
strFile = Dir$(strPath)
Do While Len(strFile) > 0
List1.AddItem strFile
strFile = Dir$()
Loop
I think sometimes it might return ".." as a "go back to previous directory" so you might also need to add:
Code:
Dim strPath As String, strFile As String
strPath = "G:\foler1\folder2\"
strFile = Dir$(strPath)
Do While Len(strFile) > 0
If strFile <> ".." Then List1.AddItem strFile
strFile = Dir$()
Loop
Edit: There's also the FileListBox control in the toolbar...you can just set its path to whatever folder you want to list files from.
-
Apr 17th, 2008, 05:09 PM
#4
Re: Adding File Names In A Folder To a Listbox
I wouldn't wanna add a dependency to the scripting object for listing files...
just curious.... why is that?
The reason is that I don't see any harm in using scripting object for listing files....
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Apr 17th, 2008, 05:23 PM
#5
Thread Starter
Member
Re: Adding File Names In A Folder To a Listbox
 Originally Posted by koolsid
Try this...
Code:
Sub addfilenames()
Dim fold As Scripting.Folder
Dim fil As File
Dim fso As New FileSystemObject
'Replace this with your folder name
Set fold = fso.GetFolder("S:\aaa")
For Each fil In fold.Files
List1.AddItem fil.Name
Next fil
End Sub
Edit: Set reference to Microsoft Scripting Runtime library
I tried that and it gives me error msgs saying:
Compile error: User-defined type not defined
and the problem seams to be the "As Scripting.Folder", "As File",.... in the dim statements.
when i get rid of the "As File" and "As Scripting.Folder", "As New FileSystemObject" another error msg comes up saying "object required" for this part of the code:
Set fold = fso.GetFolder("G:\folder2/folder1/")
But anyways i think the FileListBox works for me, do you know what the property in the FileListBox that contains the file name is called? (e.g File1.text?? or file1. ? )
-
Apr 17th, 2008, 05:26 PM
#6
Re: Adding File Names In A Folder To a Listbox
That's because as I have mentioned in my post at the bottom... you need to set reference..to Microsoft Scripting Runtime library
Go to the "Project" menu, and select "References". You will be presented with a long list of available references, just scroll down to Microsoft Scripting Runtime library and select it
also
Set fold = fso.GetFolder("G:\folder2/folder1/")
should be
Set fold = fso.GetFolder("G:\folder2\folder1")
I have tested it and it works...
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Apr 17th, 2008, 06:01 PM
#7
Re: Adding File Names In A Folder To a Listbox
 Originally Posted by koolsid
just curious.... why is that?
The reason is that I don't see any harm in using scripting object for listing files....
If he was already using FSO for something else in his project then I would agree. But I just don't see the point in add 148KB to the project just to list files when there is a just as easy to way to do it using the built-in Dir$() function. Or the FileListBox.
-
Apr 17th, 2008, 08:12 PM
#8
Re: Adding File Names In A Folder To a Listbox
I agree with DigiRev.
However, no need to test for "." and ".." as both of these are directories
when you use Dir$(sPath & "\"), that will return a normal file only (but not hidden file).
Code:
Dim sFName As String
'-- noted that the path must ending with "\"
sFName = Dir$("G:\foler1\folder2\")
While Len(sFName)
List1.AddItem sFName
sFName = Dir$()
Wend
If using FSO, there are two ways Early or Late binding:
1. Late Binding (no need to set reference)
Code:
Dim fso As Object
Dim fo As Object
Dim fi As Object
Set fso = CreateObject("Scripting.FileSystemObject")
'-- the path below may be ended with "\" or not
Set fo = fso.GetFolder("G:\foler1\folder2")
For Each fi In fo.Files
List1.AddItem fi.Name
Next
Set fi = Nothing
Set fo = Nothing
Set fso = Nothing
2. Early Binding : "Microsoft Scripting Runtime" needs to be referenced.
Code:
Dim fso As Scripting.FileSystemObject
Dim fo As Scripting.Folder
Dim fi As Scripting.File
Set fso = New Scripting.FileSystemObject
Set fo = fso.GetFolder("G:\foler1\folder2")
For Each fi In fo.Files
List1.AddItem fi.Name
Next
Set fi = Nothing
Set fo = Nothing
Set fso = Nothing
To matching line-by-line with Late binding, I didn't use
Code:
Dim fso As New Scripting.FileSystemObject
It seems to be using Dir$() method is much simpler and faster, it takes less than half the time of the other methods.
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
|