请教各位高手一个问题.如何使用VB的FSO对象来遍历一个分区?或者整个硬盘.我举个例子.下面的代码举例说明了如何获得一个 Files 集合,以及如何用 For Each...Next 语句来访问这个集合中的每个File:Sub ShowFolderList(folderspec)
Dim fs, f, f1, fc, s
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(folderspec)
Set fc = f.Files
For Each f1 in fc
s = s & f1.name
s = s & vbCrLf
Next
MsgBox s
End Sub下面是使用SUBFOLDERS集合.显示文件夹Sub ShowFolderList(folderspec)
Dim fs, f, f1, fc, s
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.getfolder(folderspec)
Set fc = f.subfolders
For Each f1 In fc
s = s & f1.Name
s = s & vbCrLf
Next
MsgBox s
End Sub
那么请问如何结合上面的例子来遍历一个分区或整个硬盘呢?
望高手不吝赐教,谢谢~~
Dim fs, f, f1, fc, s
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(folderspec)
Set fc = f.Files
For Each f1 in fc
s = s & f1.name
s = s & vbCrLf
Next
MsgBox s
End Sub下面是使用SUBFOLDERS集合.显示文件夹Sub ShowFolderList(folderspec)
Dim fs, f, f1, fc, s
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.getfolder(folderspec)
Set fc = f.subfolders
For Each f1 In fc
s = s & f1.Name
s = s & vbCrLf
Next
MsgBox s
End Sub
那么请问如何结合上面的例子来遍历一个分区或整个硬盘呢?
望高手不吝赐教,谢谢~~
解决方案 »
- Visual Basic 中的交错数组主要在哪里用?
- 请问动态建立第三级菜单........
- 使用mediaplayer9控件播放MPG时,怎么调整显示画面大小?
- 有一个小问题请教。
- webbrowser在设计时如何让他装入一个默认页,就像ie中的找不到网页那样,不然总是显示一片白,谢谢!
- 进入有分
- [email protected]这个王八蛋还在给我发有木马的邮件,各位网友小心啊!!
- 我想在一个VB程序中输入一段c程序,怎样验证它的语法是否有错,并编译它,得出结果,如有错,输出错误,怎么实现啊
- vb适合用来做什么??谢绝灌水.
- 调用函数和过程的方法有什么?
- 重拾VB,先拜码头,各位大虾,不吝赐教,帮忙解决一个小问题,up有分。
- 求教VB用HTM打印报表的问题
Dim fs, f, f1, fc, s
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(folderspec)
Set fc = f.Files
For Each f1 in fc
s = s & f1.name
s = s & vbCrLf
Next
MsgBox s
End Sub
Sub ShowFolderList(folderspec)
Dim fs, f, f1, fc, s
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.getfolder(folderspec)
call ShowFileList(folderspec)
Set fc = f.subfolders
For Each f1 In fc
showfolderlist(f1.name)
s = s & f1.Name
s = s & vbCrLf
Next
MsgBox s
End Sub
你直接调用showfolderlist就行了。
用递归遍历一个路径下的所有文件(包含子目录)。结果显示在立即窗口。Option Explicit
Dim DirectStore() As String
Private Sub FindFile(strPath As String)
Dim StrCurPath As String, sPath As String
Dim sFileName As String, sModel As String, sLocal As String
Dim sDectory() As String '当前目录子目录
Dim intJlS As Integer, i As Integer
Dim intTemp As Integer
On Error GoTo ErrMsg
intJlS = 0
sPath = IIf(Right(strPath, 1) = "\", strPath, strPath & "\")
intTemp = UBound(DirectStore)
ReDim Preserve DirectStore(intTemp + 1)
DirectStore(intTemp) = sPath
sFileName = Dir(sPath, 31)
'31=vbNormal+vbReadOnly+vbHidden+vbSystem+vbVolume+vbDirectory
Do While sFileName <> ""
If sFileName <> "." And sFileName <> ".." Then
'如果是目录保存到数组
If GetAttr(sPath & sFileName) And vbDirectory Then
intJlS = intJlS + 1
ReDim Preserve sDectory(intJlS)
sDectory(intJlS - 1) = sPath & sFileName
Else
End If
End If
sFileName = Dir
Debug.Print strPath + sFileName
DoEvents
Loop
'递归遍历 For i = 0 To UBound(sDectory, 1) - 1
FindFile sDectory(i)
Next
Exit Sub
ErrMsg:
End SubPrivate Sub Command1_Click()
FindFile "C:\EBGStock"
End SubPrivate Sub Form_Load()
ReDim DirectStore(0)
End Sub