Private Sub Command1_Click()
listfile ("c:")
End Sub
Sub listfile(filepath As String)
Dim gg As Long
gg = vbNormal Or vbReadOnly Or vbHidden Or vbSystem Or vbVolume Or vbDirectory Or vbArchive
FileName = Dir(filepath & "\*", gg)
Combo1.AddItem FileName
If (GetAttr(FileName) And vbDirectory) Then listfile (FileName)'前半句为出错的地方
Do Until FileName = ""
FileName = Dir
como1.AddItem FileName
If (GetAttr(FileName) And vbDirectory) Then listfile (FileName)
Loop
End Sub
最简单的方法就是用dir()函数,它完全可以做到
例如:
sub listfile(filepath as string)
dim gg as long
gg=vbnormal or vbreadonly or vbhidden or vbsystem or vbsystem or vbvolume or vbdirectory or vbarchive
filename=dir(filepath & "\*",gg)
como1.additem filename
if (getattr(filename) and vbderectory) then listfile(filename)
do until filename=""
filename=dir
como1.additem filename
if (getattr(filename) and vbderectory) then listfile(filename)
loop
end sub
Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Public Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Public Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Public Declare Function GetForegroundWindow Lib "user32" () As Long
Public Declare Function IsWindow Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Public Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" (ByVal nDrive As String) As Long
Public Const DRIVE_CDROM = 5
Public Const DRIVE_FIXED = 3
Public Const DRIVE_RAMDISK = 6
Public Const DRIVE_REMOTE = 4
Public Const DRIVE_REMOVABLE = 2
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 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 FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
Public Sub FindFiles(strRootFolder As String, strFolder As String, strFile As String, colFilesFound As Collection)
Dim lngSearchHandle As Long
Dim udtFindData As WIN32_FIND_DATA
Dim strTemp As String, lngRet As Long
If Right$(strRootFolder, 1) <> "\" Then strRootFolder = strRootFolder & "\"
lngSearchHandle = FindFirstFile(strRootFolder & "*", udtFindData)
If lngSearchHandle = INVALID_HANDLE_VALUE Then Exit Sub
lngRet = 1
Do While lngRet <> 0
strTemp = TrimNulls(udtFindData.cFileName)
If (udtFindData.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) = FILE_ATTRIBUTE_DIRECTORY Then
If strTemp <> "." And strTemp <> ".." Then
Call FindFiles(strRootFolder & strTemp, strFolder, strFile, colFilesFound)
End If
Else
If (strRootFolder Like strFolder) Then
If (strTemp Like strFile) Then
colFilesFound.Add strRootFolder & strTemp
End If
End If
End If
lngRet = FindNextFile(lngSearchHandle, udtFindData)
Loop
Call FindClose(lngSearchHandle)
End Sub
Public Function TrimNulls(strString As String) As String
Dim l As Long
l = InStr(1, strString, Chr(0))
If l = 1 Then
TrimNulls = ""
ElseIf l > 0 Then
TrimNulls = Left$(strString, l - 1)
Else
TrimNulls = strString
End If
End Function