怎么把一目录下所有文件(包括子目录)显示出来

yk1028 2001-07-12 10:54:11
文件要跨目录显示,把目录下文件夹下的文件也显示出来,怎么办
...全文
406 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
quhaifeng 2002-01-29
  • 打赏
  • 举报
回复
vbDirectory到底是什么东西呢?为什么我运行这段程序提示“文件未找到”

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

yk1028 2001-07-12
  • 打赏
  • 举报
回复
是我没说清楚,
把一个文件下所有文件名显示出来(例如显示到文本框中),如果该文件下还有文件,则也要把其下的文件名显示出来.dir()好像功能有限.
lianghn 2001-07-12
  • 打赏
  • 举报
回复
怎么看不明白?是不是我变笨了?
说清楚点。
playyuer 2001-07-12
  • 打赏
  • 举报
回复
HOWTO: Search Directories to Find or List Files
http://support.microsoft.com/support/kb/articles/Q185/4/76.asp

HOWTO: Recursively Search Directories Using FileSystemObject
http://support.microsoft.com/support/kb/articles/Q185/6/01.asp
yk1028 2001-07-12
  • 打赏
  • 举报
回复
第一次上csdn提问就有这么多网友把问题摆平了
好感动呀,有解决方法还可以写,分继续给,尽管我很穷
呵呵,
qaymuic 2001-07-12
  • 打赏
  • 举报
回复
有几个字母打错了:vbdirectory,
vbsystem多打了一个。
como1是一个组合框控件
qaymuic 2001-07-12
  • 打赏
  • 举报
回复
最简单的方法就是用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
enmity 2001-07-12
  • 打赏
  • 举报
回复
加一个filelistbox控件不就可以了??????????
y_shen 2001-07-12
  • 打赏
  • 举报
回复

编历文件:

'以下在 .bas
'-----------------------------------------------------------------------------------------------------------
Option Explicit

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

Public Const MAX_PATH = 260
Private Const INVALID_HANDLE_VALUE = -1
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 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


'------------------------------------------------------------------------------------------
'以下在窗体!
Option Explicit

Private Sub Form_Load()
Dim a As New Collection
Dim i As Integer

FindFiles "c:\", "*", "*.*", a
For i = 1 To a.Count
Debug.Print a(i)
Next
End Sub

是别人教我的!
fishzone 2001-07-12
  • 打赏
  • 举报
回复
用dir加一个递归算法
lianghn 2001-07-12
  • 打赏
  • 举报
回复
使用FileSystemObject对象,指定一个Folder,看下面的对象是File还是Folder,然后分别处理就行了。

7,785

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧