又是一个小小问题,关于文件的。

fannyfang79 2001-12-03 05:44:54
不知如何计算一个目录下有多少个文件,而且是否可以知道他们的文件名?
...全文
198 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
fannyfang79 2001-12-03
  • 打赏
  • 举报
回复
thank you, i will try later.
Bardo 2001-12-03
  • 打赏
  • 举报
回复
Open the Project


Add CommandButton named Command2, two more TextBoxes named Text5 and Text6 and another ListBox, List2, to Form1.


Copy the following code into Form1's module:

Function FindFiles(path As String, SearchStr As String, _
FileCount As Integer, DirCount As Integer)
Dim FileName As String ' Walking filename variable.
Dim DirName As String ' SubDirectory Name.
Dim dirNames() As String ' Buffer for directory name entries.
Dim nDir As Integer ' Number of directories in this path.
Dim i As Integer ' For-loop counter.

On Error GoTo sysFileERR
If Right(path, 1) <> "\" Then path = path & "\"
' Search for subdirectories.
nDir = 0
ReDim dirNames(nDir)
DirName = Dir(path, vbDirectory Or vbHidden) ' Even if hidden.
Do While Len(DirName) > 0
' Ignore the current and encompassing directories.
If (DirName <> ".") And (DirName <> "..") Then
' Check for directory with bitwise comparison.
If GetAttr(path & DirName) And vbDirectory Then
dirNames(nDir) = DirName
DirCount = DirCount + 1
nDir = nDir + 1
ReDim Preserve dirNames(nDir)
'List2.AddItem path & DirName ' Uncomment to list
End If ' directories.
sysFileERRCont:
End If
DirName = Dir() ' Get next subdirectory.
Loop

' Search through this directory and sum file sizes.
FileName = Dir(path & SearchStr, vbNormal Or vbHidden Or vbSystem _
Or vbReadOnly)
While Len(FileName) <> 0
FindFiles = FindFiles + FileLen(path & FileName)
FileCount = FileCount + 1
' Load List box
List2.AddItem path & FileName & vbTab & _
FileDateTime(path & FileName) ' Include Modified Date
FileName = Dir() ' Get next file.
Wend

' If there are sub-directories..
If nDir > 0 Then
' Recursively walk into them
For i = 0 To nDir - 1
FindFiles = FindFiles + FindFiles(path & dirNames(i) & "\", _
SearchStr, FileCount, DirCount)
Next i
End If

AbortFunction:
Exit Function
sysFileERR:
If Right(DirName, 4) = ".sys" Then
Resume sysFileERRCont ' Known issue with pagefile.sys
Else
MsgBox "Error: " & Err.Number & " - " & Err.Description, , _
"Unexpected Error"
Resume AbortFunction
End If
End Function

Private Sub Command2_Click()
Dim SearchPath As String, FindStr As String
Dim FileSize As Long
Dim NumFiles As Integer, NumDirs As Integer

Screen.MousePointer = vbHourglass
List2.Clear
SearchPath = Text1.Text
FindStr = Text2.Text
FileSize = FindFiles(SearchPath, FindStr, NumFiles, NumDirs)
Text5.Text = NumFiles & " Files found in " & NumDirs + 1 & _
" Directories"
Text6.Text = "Size of files found under " & SearchPath & " = " & _
Format(FileSize, "#,###,###,##0") & " Bytes"
Screen.MousePointer = vbDefault
End Sub

Private Sub Form_Load()
Command1.Caption = "Use API code"
Command2.Caption = "Use VB code"
' start with some reasonable defaults
Text1.Text = "C:\My Documents\"
Text2.Text = "*.*"
End Sub



Run the Project. Enter a starting path into Text1, a search string in Text2 (like *.* or Myfile?.txt, and so forth) and then click Command2.


You see a list of the files found appear in List2 with the last modified date, the number of files found in Text5, and the total size of the files found under the starting directory in Text6. By combining these two methods on one form you can verify that both methods return matching information.

-From MSDN !!!!-
fannyfang79 2001-12-03
  • 打赏
  • 举报
回复
具体如何实现呢?
dadi2001 2001-12-03
  • 打赏
  • 举报
回复
dir语句

7,785

社区成员

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

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