|
-
Dec 22nd, 2000, 09:16 AM
#1
Thread Starter
Frenzied Member
How can i add the filenames of all the files on my disk in a list box.
-
Dec 22nd, 2000, 09:39 AM
#2
New Member
Vlatko,
I'm doing this on the fly without VB in front of me but....
You can use the File system object (help on the FSO is not too bad. You can also search through this site to find more info. It is a topic that comes up often)to get the files collection from your drive and cycle through them with a statement like:
Code:
For each File in Files
Then as you run through all of them do a :
Code:
List1.Additem to put them in your listbox.
Hope this helps!
B.D.H.
VB6, ASP, and still working in good ol' Basic!
Training means learning the rules. Experience means learning the exceptions.
----Joe Cossman
-
Dec 22nd, 2000, 09:49 AM
#3
Frenzied Member
That's one possibility, yeah. But you have to ship yet *another* DLL with your program, so I don't like it.
Also you can use this Sub I wrote, it's a bit slow, modify it (API!) so it can get alot faster.
Code:
Private Sub GetAllFiles(StartDir As String)
Dim d$, dr As Boolean, dts As Boolean, dirs() As String
If Right(StartDir, 1) <> "\" Then StartDir = StartDir & "\"
If Dir(StartDir, vbDirectory) = "" Then Exit Sub
ReDim dirs(0)
d = Dir(StartDir, vbDirectory)
Do
d = Dir
dts = d <> "." And d <> ".." And Len(d) > 0
If Len(d) Then dr = GetAttr(StartDir & d) = vbDirectory
If dr = False And dts Then List1.AddItem StartDir & d 'add to list
If dr And dts And d <> "" Then
ReDim Preserve dirs(UBound(dirs) + 1)
dirs(UBound(dirs)) = d
End If
Loop While Len(d)
Dim x&
For x = 1 To UBound(dirs)
GetAllFiles StartDir & dirs(x)
Next x
End Sub
Call it like:
Code:
Private Sub Command1_Click()
GetAllFiles "C:\"
End Sub
I know it's pretty slow but it saves ya ditrubuting another DLL.
I'll write one with the API when I have time
Jop - validweb.nl
Alcohol doesn't solve any problems, but then again, neither does milk.
-
Dec 22nd, 2000, 10:41 AM
#4
Thread Starter
Frenzied Member
Is there a way to do this only with API. Without using the VB keywords. I am interested in doing this in VC++.
-
Dec 22nd, 2000, 10:49 AM
#5
Frenzied Member
Sure you can,
take a look at the FindFirstFile & FindNextFile Api Calls:
Code:
Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
Here's the type for the WIN32_FIND_DATA
Code:
Const MAX_PATH = 260
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Private Type WIN32_FIND_DATA
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * MAX_PATH
cAlternate As String * 14
End Type
I'll write one with the API, you can have it if it's finished.
Jop - validweb.nl
Alcohol doesn't solve any problems, but then again, neither does milk.
-
Dec 22nd, 2000, 11:55 AM
#6
Frenzied Member
Ok here's the API version, it's based (stripnulls function) on the example from the Api-Guide ( http://www.allapi.net ).
Code:
Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long
Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
Const MAX_PATH = 260
Const MAXDWORD = &HFFFF
Const FILE_ATTRIBUTE_DIRECTORY = &H10
Const FILE_ATTRIBUTE_HIDDEN = &H2
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Private Type WIN32_FIND_DATA
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * MAX_PATH
cAlternate As String * 14
End Type
Public Sub GetAllFilesAPI(StartDir As String)
Dim d$, dr As Boolean, dts As Boolean, dirs() As String
Dim FindData As WIN32_FIND_DATA, c%, file&
If Right(StartDir, 1) <> "\" Then StartDir = StartDir & "\"
ReDim dirs(0)
file = FindFirstFile(StartDir & "*", FindData)
c = 1
Do While c
c = FindNextFile(file, FindData)
d = StripNulls(FindData.cFileName)
dts = d <> "." And d <> ".." And Len(d) > 0
If dts Then dr = (GetFileAttributes(StartDir & d) And FILE_ATTRIBUTE_DIRECTORY)
If dr = False And dts Then Form1.List1.AddItem StartDir & d 'add to list
If dr And dts Then
ReDim Preserve dirs(UBound(dirs) + 1)
dirs(UBound(dirs)) = d
End If
Loop
Dim x&
FindClose file
For x = 1 To UBound(dirs)
GetAllFilesAPI StartDir & dirs(x)
Next x
End Sub
Function StripNulls(OriginalStr As String) As String
'Got this from the API-Guide ( http://www.allapi.net )
If (InStr(OriginalStr, Chr(0)) > 0) Then OriginalStr = Left(OriginalStr, InStr(OriginalStr, Chr(0)) - 1)
StripNulls = OriginalStr
End Function
It's 0.69140625 seconds faster than the one I posted before while processing a dir with 895 files (in a loooooot of different subdirs)
It may be even faster when assigning it to an array rather then passing it to the listbox in the loop, change as needed 
Hope it was of any help 
Jop - validweb.nl
Alcohol doesn't solve any problems, but then again, neither does milk.
-
Dec 22nd, 2000, 01:04 PM
#7
Thread Starter
Frenzied Member
Thanks it worked like a charm. I am going to put the function in a C++ DLL which i will be calling from VB. It should be a lot faster
-
Dec 22nd, 2000, 04:57 PM
#8
Frenzied Member
Yep, probably 
But you can't deny it's pretty fast in VB too, try putting it in an array instead of adding it to a listbox, will be even faster
Jop - validweb.nl
Alcohol doesn't solve any problems, but then again, neither does milk.
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
|