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.
[QUOTE=Paul M;2973969]Here is my shot it gets all files in sub directories to!
[QUOTE]
Hi
Apologies in advance for needing to ask a dumb question, but I'm pretty new to this and flying solo. I'm a tech writer, not a programmer, and have been making a few half-hearted attempts to learn VB so that I can have more control over my word docs. Problem is I am still at the starting blocks and need to catalogue all the template files in a directory, including all sub-dirs, so this post is very interesting to me, but I don't know what to do with the code!
I know enough to open the VB editor in word, and paste it into a code window, but I don't know how to name it, or how to call it. Also, there's the question of the sub tacked on at the end and how to deal with it (where does it go, etc).
Ok so banish me to the sandpit, but if someone would be kind enough to give me some steps on how to use this code first, I'll be extremely grateful. My other option is to hand type out several thousand file names.
(PS While registering for the forum, I noticed an office forum, so i will head over there and read up, but this post is the first one i found covering exactly my required task)
Hello there, Thanks alot for your code,
just a quick question...
is there a way to list files in a directory but get the contents arranged in some ways like "last modified" or "by name" ?
I'm not sure if there is a way to get the list of files in a specific sorted order. However, since you can get all files, you can then read any information you want for each file (last modified, size, etc) and then do the sorting yourself after you get the whole list!
Has someone helped you? Then you can Rate their helpful post.
List All The Files In A Directory and it's subdirs
Hi I am making a search engine in VB6, your code works but I also need to list all files in subdirs as well. I already searched on google but I couldnt find anything. ( except the ones that cause your whole project to crash:S ) Do you know how to achieve this?
Thanks in advance,
Jason
Last edited by jason8100; Dec 19th, 2009 at 03:52 PM.
Reason: wrong quote
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
You could specify an extension. It is mentioned in the examples if you read through them carefully.
well itryed just this but it shows nothing
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
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
You are getting errors because you are in the wrong place... this is the VB6 Codebank, but you are using VB.Net
Post #22 is the only thing from this thread that you should consider using - for anything else (including help fixing issues with it) you should post in the VB.Net forum.
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.
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.
Unless we see exactly your code, we won't be able to help! You can create a new thread in the Visual Basic 6 and Earlier forum to get more help
Has someone helped you? Then you can Rate their helpful post.
The folder structure I am working with is - drive letter/folder level1 (single folder)/folder level 2 (single folder)/folder level 3(multiple folders)/folder level 4 (multiple folders)/multiple filenames in each folder at level 4.
What I am needing to do is to return, on a form, a list of all files showing part of the path (including the extension) but only that part of the path showing folder level 3, folder level 4, and the filename.
The drive letter is not to be hard coded as the exe could be run from any drive letter.
The folder structure I am working with is - drive letter/folder level1 (single folder)/folder level 2 (single folder)/folder level 3(multiple folders)/folder level 4 (multiple folders)/multiple filenames in each folder at level 4.
What I am needing to do is to return, on a form, a list of all files showing part of the path (including the extension) but only that part of the path showing folder level 3, folder level 4, and the filename.
The drive letter is not to be hard coded as the exe could be run from any drive letter.
And of course using VB5
You can modify DirSize function from my archive above to return an array of file and folders. And then filter/use it as you want. Or filter right "in place" before return.
Sorry Hwoarang, I cannot find your DirSize archive. Where is it please?
FileSystemFunctions.zip attached - 3 posts above. There is a VB6 project inside with a class that contains DirSize function. You can try to change it for your requirements - it should be pretty easy.