How can I simply get a file listing from a directory (non recursive) without displaying the path, just the filenames, while *NOT* using the 'DIR' command in VB.
Printable View
How can I simply get a file listing from a directory (non recursive) without displaying the path, just the filenames, while *NOT* using the 'DIR' command in VB.
well if you don't want dir then it has to be the API's of:
FindFirstFile
FindNextFile
FindClose
otherwise you are into Ouigi Board devination
Thanks for the reply.
Trouble is, them API's are recursive.
What's wrong with Dir?
Yes, there is something called but FSO, but it's not the best, the API makai gave is recursive, from what i know.
WHy is it that you don't want to use Dir?
I'd love to use DIR, but I'm already using it to get the directory's, if I then try and use DIR for the files in the same loop it goes hectic, VB won't let you use DIR whilst in another DIR loop.
It would be great if I knew a way around this.
I could use DIR for the files if I knew an API command that would give me just the DIRECTORY'S from a given DIRECTORY(recursive).
It's doing me in.
the API's aren't recursive unless you make them so
but the first returns a handle for the process that is closed by the last which is precisely what you want
Ok, I see the problem, now you could avoid calling Dirs while enumerating another by just doing one enumeration at a time, and store each enumeration in an array before proceeding with the next. You could use my Dir enumerator function:
Code:Sub EnumDir(ByRef edir() As String, path As String)
Dim n As Integer, a As String
a = Dir(path)
Do While Len(a)
ReDim Preserve edir(n)
edir(n) = a
n = n + 1
a = Dir
Loop
End Sub
Sounds good, but to be honest, the 'ByRef edir() As String' section is too much for me, don't know what to do with it.
Sorry to be a pain.
If I'm not correct, it wants the address of the variable you want the files to be listed in, for example:
Code:'Create a variable for the files to be stored in
Dim sFiles() As String
'Call the function
EnumDir sFiles, "C:\Windows"
thats correct, you pass a string array and it returns with all files in the folder
Hmm, looks like I'm beat.
I tried the above example and all I got was a list of files in 1 directory.
It might be better if I show an example of what I'm trying to do:
Example, lets say I've got a directory called 'MyFolder'
and in this directory is 4 files and 2 more directory's.
'MyFolder2'
'MyFolder3'
MyFolder 2 contains 2 files
MyFolder 3 contains 1 file and 1 Folder called MyFolder4'
MyFolder4 contains 3 files, phew.
So I'd pass the directory 'MyFolder' to a routine and I want it to fill a ListBox to look like this.
C:\MyFolder
File1.poo
File2.poo
File3.poo
File4.poo
C:\MyFolder\MyFolder2
File1.poo
File2.poo
C:\MyFolder\MyFolder3
File1.poo
C:\MyFolder\MyFolder3\MyFolder4
File1.poo
File2.poo
File3.poo
Hope this makes sense.
Infact I first though this would be easy but have ran into many probs, FSO seems a right pain in the butt.
Hope you or someone can help.
Looks like you need recursive functions anyway, i guess it's faster with the apis but it also works with dir.
well i have the code for api if you need them or do you want to use dir?
Too be honest, any, as long as it works :o)
HEre's the thing, findfilesfast, should return all files with their whole pathsCode:Option Explicit
'API Consts, Types and Functions
Private Const MAX_PATH = 260
Private Const FILE_ATTRIBUTE_DIRECTORY = &H10
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
Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
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
Private Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long
Private aFileList As Variant
Private nFileCount As Long
Function FastFindFiles(ByVal sFolder As String, Optional ByVal sPattern As String = "*") As Variant
'Initialize the Private File Array and Count, then call the Fast
'File Recursive Function to populate the Array, then return it.
nFileCount = 0
aFileList = Array()
Screen.MousePointer = vbArrowHourglass
Call RecurseFindFiles(sFolder, sPattern)
Screen.MousePointer = vbDefault
FastFindFiles = aFileList
End Function
Private Sub RecurseFindFiles(ByVal sFolder As String, ByVal sPattern As String)
Dim tFD As WIN32_FIND_DATA, lFile As Long, bFound As Long, aSubs() As String, nSubs As Long, sFilename As String
'Make sure the passed folder includes an ending "\"
If Right(sFolder, 1) <> "\" Then sFolder = sFolder & "\"
'Find the First File in the Specified Location
lFile = FindFirstFile(sFolder & "*", tFD)
bFound = lFile
'Loop while a File is found
While bFound
'Get the Filename
sFilename = UCase(Left(tFD.cFileName, InStr(tFD.cFileName, Chr(0)) - 1))
If (tFD.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) Then
'If it's a Folder, add it to the Sub Folders Array
If Left(sFilename, 1) <> "." Then
ReDim Preserve aSubs(nSubs)
aSubs(nSubs) = sFilename
nSubs = nSubs + 1
End If
Else
'If it's a File, compare it to the Pattern for a Match
If sFilename Like UCase(sPattern) Then
'If it matches, add it to the File Array
ReDim Preserve aFileList(nFileCount)
aFileList(nFileCount) = sFolder & sFilename
nFileCount = nFileCount + 1
End If
End If
'Find the Next File, (if there is one).
bFound = FindNextFile(lFile, tFD)
Wend
'Close the API Find Handle
Call FindClose(lFile)
'If there were Sub Folders found, Recurse them too..
If nSubs Then
For nSubs = 0 To UBound(aSubs)
Call RecurseFindFiles(sFolder & aSubs(nSubs), sPattern)
Next
End If
End Sub
wow, that's it.
I thank you very much and everyone else who helped.
You a great bunch! :-)