请问怎样检测出一个目录中几个子目录和几个文件?谢谢!

miss 2000-07-25 09:34:00
...全文
136 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
UserReg 2000-07-25
  • 打赏
  • 举报
回复
Option Explicit
Public Const MAX_PATH = 260
Public Const INVALID_HANDLE_VALUE = -1
Public Const ERROR_NO_MORE_FILES = 18&
Public Const FILE_ATTRIBUTE_DIRECTORY = &H10
Public Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Public 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 Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA"
(ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Public Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (B
yVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
Public Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) A
s Long
Public Declare Function GetLastError Lib "kernel32" () As Long
'所有的文件列表,全局变量
Public thefilelist As New Collection
Public Sub findfile(ByVal thepathname As String) 'thepathname 的形式为"C:\pa
th\thesubpath\",
'例如,调用 findfile "c:\windows\"
Dim hfile As Long
Dim result As Long
Dim errorno As Long
Dim thefileinfo As WIN32_FIND_DATA
Dim findfinish As Boolean
Dim fullname As String
Dim realname As String
'debug
Dim pos As Long
Dim r As String
'debug
findfinish = False
hfile = FindFirstFile(thepathname & "*", thefileinfo)
If (hfile = INVALID_HANDLE_VALUE) Then
'错误处理......
errorlog
Exit Sub
End If
realname = trimstr(thefileinfo.cFileName)
fullname = thepathname & realname
If (thefileinfo.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY) Then
If (realname <> "." And realname <> "..") Then
findfile fullname & "\"
End If
Else
thefilelist.Add fullname
End If
Do Until (findfinish)
result = FindNextFile(hfile, thefileinfo)
If (result = 0) Then
errorno = GetLastError()
If (errorno = ERROR_NO_MORE_FILES) Then
'此目录查询完毕
findfinish = True
Else
'出错
errorlog
End If
'关闭查找句柄
result = FindClose(hfile)
If (result = 0) Then
errorlog
End If
Exit Sub
Else
realname = trimstr(thefileinfo.cFileName)
fullname = thepathname & realname
If (thefileinfo.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY) Then
If (realname <> "." And realname <> "..") Then
findfile fullname & "\"
End If
Else
thefilelist.Add fullname
End If
End If
Loop
End Sub
Public Sub errorlog()
Dim errorno As Long
Dim hfile As Integer
Dim thedate
Dim thetime
Dim msg As String
Dim errordir As String
errorno = GetLastError()
thedate = Date
thetime = time
errordir = App.Path
errordir = errordir & "\errorlog.txt"
hfile = FreeFile
msg = CStr(errorno) & " the systemerro " & CStr(thedate) & " " & CStr(thetime)
Open errordir For Append As hfile
Write #hfile, msg
Close hfile
thefilelist.Remove 1
End Sub
Function trimstr(thelongfilename As String) As String '?????? 这样写好么?因
为win32_file_data下面的文件名是定长的字符串,
Dim pos As Integer
Dim theend As String
theend = Right(thelongfilename, 1)
pos = InStr(thelongfilename, theend)
trimstr = Left(thelongfilename, pos - 1)
End Function

查找出文件名称放在thefilelist里面。。。

czh918 2000-07-25
  • 打赏
  • 举报
回复
最简单用filelistbox,使用时不让它显示即可
huntout 2000-07-25
  • 打赏
  • 举报
回复
這幾個例子應該有幫助

Dim MyFile, MyPath, MyName
' 傳回「WIN.INI」(如果該檔案存在)。
MyFile = Dir("C:\WINDOWS\WIN.ini")

' 如果超過一個 *.ini 檔案存在電腦裏,也可以傳入指定的副檔名加以搜尋。
' 函數將傳回第一個找到符合條件的檔案名稱。
MyFile = Dir("C:\WINDOWS\*.ini")

' 若再呼叫此函數,但不傳入任何引數,則函數將傳回和第一個找到的檔案相同目錄下第二個找到的 *.ini 檔案。
MyFile = Dir

' 傳回第一個找到隱藏的 *.TXT 檔案。
MyFile = Dir("*.TXT", vbHidden)

' 將「C:\」根目錄下的子目錄顯示出。
MyPath = "c:\" ' 指定路徑。
MyName = Dir(MyPath, vbDirectory) ' 找尋第一個子目錄。
Do While MyName <> "" ' 執行迴圈。
' 跳過目前的目錄及上層目錄。
If MyName <> "." And MyName <> ".." Then
' 使用位元比對來確定 MyName 代表一目錄。
If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then
Debug.Print MyName ' 將目錄名稱顯示出來。
End If
End If
MyName = Dir ' 尋找下一個目錄。
Loop

TopHead 2000-07-25
  • 打赏
  • 举报
回复
你可以用Dir函数,

返回一个 String,用以表示一个文件名、目录名或文件夹名称

7,762

社区成员

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

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