Set objFSO = CreateObject("Scripting.FileSystemObject")Set fd = objFSO.GetFolder("c:\11") For Each f1 In fd.Files msgbox f1.Name Next
刚才的有点错,应该是MsgBox f1.Path
Dim fso Dim fn As StringPrivate Sub Command1_Click() Dim fd As String Set fso = CreateObject("Scripting.FileSystemObject") fd = "d:\a" Call getFilenm(fd) MsgBox fn End SubFunction getFilenm(fdnm As String) Dim obFd, fl, sfd Set obFd = fso.GetFolder(fdnm) For Each fl In obFd.Files fn = fn & fl.Name & Chr(10) Next If obFd.SubFolders.Count > 0 Then For Each sfd In obFd.SubFolders Call getFilenm(sfd.Path) Next End If End Function
Option Explicit' 获得指定目录下的所有文件的文件名列表 ' Path 为目录的绝对路径, FileName() 为返回的文件名数组 Function GetDirectoryFileList(ByVal Path As String, ByRef FileName() As String) As Boolean Dim fName As String, i As Long If Right$(Path, 1) <> "\" Then Path = Path & "\" fName = Dir$(Path & "*.*") i = 0 Do While fName <> "" ReDim Preserve FileName(i) As String FileName(i) = fName fName = Dir$ i = i + 1 Loop If i <> 0 Then ReDim Preserve FileName(i - 1) As String GetDirectoryFileList = True Else GetDirectoryFileList = False End If End FunctionPrivate Sub Command1_Click() Dim FileName() As String, i As Long GetDirectoryFileList "c:\", FileName For i = 0 To UBound(FileName) Debug.Print FileName(i) Next i End Sub
filename=dir("c:\*.*") i = 0 do while filename = "" array1 (i)=filename i = i + 1 filename = dir("c:\*.*") loop :)
如何在VB中读取目录下所有文件及子目录 列出下列源代码。win98,vb5编译通过Private Function ListDirs(ByVal path As String, ByRef Dirs() As String, Recursive As Boolean, Optional Dircount As Long = 0) As Boolean Dim Dirname As String Dim Dirstart As Long Dim a As Long' On Error GoTo ErrorHandlerDirstart = Dircount + 1 If (Dircount = 0) Then ReDim Dirs(0) path = IIf(Right$(path, 1) = "\", path, path + "\") End IfDirname = Dir$(path + "*.*", vbDirectory) Do While (Dirname <> "") If (Dirname <> ".") And (Dirname <> "..") And ((GetAttr(path + Dirname) And vbDirectory) = vbDirectory) Then Dircount = Dircount + 1 ReDim Preserve Dirs(Dircount) Dirs(Dircount) = path & Dirname & "\" 'add to listbox 'list1.additem Dirs(Dircount) End If Dirname = Dir LoopIf Recursive Then For a = Dirstart To Dircount If Not ListDirs(Dirs(a), Dirs, Recursive, Dircount) Then ListDirs = False Exit Function End If Next End If ListDirs = True Exit FunctionErrorHandler: 'Any error message(s) can be placed here ListDirs = FalseEnd Function
filename=dir("c:\*.*") i = 0 do while filename = "" array1 (i)=filename i = i + 1 filename = dir("c:\*.*") loop 学习ing……
下面是偶一直用的!送给你! '00005.遍历目录中所有文件或文件夹 '***************************************************************************************** ' Function:ZZG_collListDirectory '***************************************************************************************** ' 名称 属性 说明 ' 参数: zzg_strPath String 需要遍历的文件夹路径 ' zzg_TypeOpt Integer 要取的文件或文件夹类型参数 ' 参数值:0.常规 ' 2.隐藏 ' 4.系统文件 ' 8.磁盘卷标;如果指定,则其它属性都会忽略 ' 16.目录或文件夹 ' zzg_blPointDirectory Boolean 是否要"."或".."[可选参数,默认为“False”] ' 返回: ZZG_collListDirectory Collection 文件或文件夹集合 '***************************************************************************************** ' 引用: '***************************************************************************************** ' 说明: 参数值可以参数累加起来的和。如既要隐藏又要系统文件,可以参数为6。 ' 常规参数总是有效的,因为参数为"0"。 ' 也可以用来查某个文件是否存在,方法就是把该文件的路径代入到zzg_strPath参数中 '***************************************************************************************** ' 作者:XXX(此入删去) ' 电邮:[email protected] ' 时间:11/27/2003 '***************************************************************************************** Public Function ZZG_collListDirectory(zzg_strPath As String, zzg_TypeOpt As Integer, Optional zzg_blPointDirectory As Boolean = False) As Collection On Error GoTo ZZG_collListDirectory_Err '容错 '定义临时变量 Dim tmpColl As New Collection '存放文件 Dim tmpPath As String '路径 Dim tmpDir As String '取得的文件或文件夹名 '定义部分临时参数值 tmpPath = zzg_strPath '路径 '遍历所有文件或文件夹 tmpDir = Dir(tmpPath, zzg_TypeOpt) '取第一个文件 Do While tmpDir <> "" '当文件取得成功 If zzg_blPointDirectory Then '是否要带"."或".."的文件夹 tmpColl.Add tmpDir '把内容添加到集合中 Else If tmpDir <> "." And tmpDir <> ".." Then '判断是否是"."或".." tmpColl.Add tmpDir '把内容添加到集合中 End If End If tmpDir = Dir '取文件 Loop Set ZZG_collListDirectory = tmpColl '返回取得的文件或文件夹集合 Set tmpColl = Nothing Exit Function '退出函数 ZZG_collListDirectory_Err: '标号 Set tmpColl = Nothing '撤销tmpColl Dim tmpColl_Err As New Collection '产生一个空集合 Set ZZG_collListDirectory = tmpColl_Err '返回这个空集合 Set tmpColl_Err = Nothing '撤销tmpColl_Err Exit Function '退出该函数 End Function
这个比较好的处理了出错的情况 因为如果某个文件windows自身删除不了,那么程序也无法删除 Private Type SHFILEOPSTRUCT hwnd As Long wFunc As Long '对文件的操作指令 pFrom As String '源文件或路径 pTo As String '目的文件或路径 fFlags As Integer '操作标志 fAnyOperationsAborted As Long hNameMappings As Long lpszProgressTitle As String End TypePrivate Declare Function SHFileOperation Lib _ "shell32" _ (lpFileOp As SHFILEOPSTRUCT) As LongConst FO_COPY = &H2 Const FO_DELETE = &H3 Const FO_MOVE = &H1 Const FO_RENAME = &H4 Const FOF_ALLOWUNDO = &H40 Const FOF_NOCONFIRMATION = &H10 Dim DirString As StringPrivate Sub Command2_Click() Dim xFile As SHFILEOPSTRUCT
'删除根目录下的temp.txt文件 xFile.pFrom = "c:\bmp\*.*" 'xFile.pTo = "c:\" xFile.wFunc = FO_DELETE xFile.hwnd = Me.hwnd '将fFlags设置为FOF_ALLOWUNDO '允许被删除的文件放置到回收站中 xFile.fFlags = FOF_ALLOWUNDO If SHFileOperation(xFile) Then Debug.Print "Success" End If End Sub
For Each f1 In fd.Files
msgbox f1.Name
Next
Dim fn As StringPrivate Sub Command1_Click()
Dim fd As String
Set fso = CreateObject("Scripting.FileSystemObject")
fd = "d:\a"
Call getFilenm(fd)
MsgBox fn
End SubFunction getFilenm(fdnm As String)
Dim obFd, fl, sfd
Set obFd = fso.GetFolder(fdnm)
For Each fl In obFd.Files
fn = fn & fl.Name & Chr(10)
Next
If obFd.SubFolders.Count > 0 Then
For Each sfd In obFd.SubFolders
Call getFilenm(sfd.Path)
Next
End If
End Function
Option Explicit' 获得指定目录下的所有文件的文件名列表
' Path 为目录的绝对路径, FileName() 为返回的文件名数组
Function GetDirectoryFileList(ByVal Path As String, ByRef FileName() As String) As Boolean
Dim fName As String, i As Long
If Right$(Path, 1) <> "\" Then Path = Path & "\"
fName = Dir$(Path & "*.*")
i = 0
Do While fName <> ""
ReDim Preserve FileName(i) As String
FileName(i) = fName
fName = Dir$
i = i + 1
Loop
If i <> 0 Then
ReDim Preserve FileName(i - 1) As String
GetDirectoryFileList = True
Else
GetDirectoryFileList = False
End If
End FunctionPrivate Sub Command1_Click()
Dim FileName() As String, i As Long
GetDirectoryFileList "c:\", FileName
For i = 0 To UBound(FileName)
Debug.Print FileName(i)
Next i
End Sub
i = 0
do while filename = ""
array1 (i)=filename
i = i + 1
filename = dir("c:\*.*")
loop :)
列出下列源代码。win98,vb5编译通过Private Function ListDirs(ByVal path As String, ByRef Dirs() As String, Recursive As Boolean, Optional Dircount As Long = 0) As Boolean
Dim Dirname As String
Dim Dirstart As Long
Dim a As Long' On Error GoTo ErrorHandlerDirstart = Dircount + 1
If (Dircount = 0) Then
ReDim Dirs(0)
path = IIf(Right$(path, 1) = "\", path, path + "\")
End IfDirname = Dir$(path + "*.*", vbDirectory)
Do While (Dirname <> "")
If (Dirname <> ".") And (Dirname <> "..") And ((GetAttr(path + Dirname) And vbDirectory) = vbDirectory) Then
Dircount = Dircount + 1
ReDim Preserve Dirs(Dircount)
Dirs(Dircount) = path & Dirname & "\"
'add to listbox
'list1.additem Dirs(Dircount)
End If
Dirname = Dir
LoopIf Recursive Then
For a = Dirstart To Dircount
If Not ListDirs(Dirs(a), Dirs, Recursive, Dircount) Then
ListDirs = False
Exit Function
End If
Next
End If
ListDirs = True
Exit FunctionErrorHandler:
'Any error message(s) can be placed here
ListDirs = FalseEnd Function
i = 0
do while filename = ""
array1 (i)=filename
i = i + 1
filename = dir("c:\*.*")
loop
学习ing……
'00005.遍历目录中所有文件或文件夹
'*****************************************************************************************
' Function:ZZG_collListDirectory
'*****************************************************************************************
' 名称 属性 说明
' 参数: zzg_strPath String 需要遍历的文件夹路径
' zzg_TypeOpt Integer 要取的文件或文件夹类型参数
' 参数值:0.常规
' 2.隐藏
' 4.系统文件
' 8.磁盘卷标;如果指定,则其它属性都会忽略
' 16.目录或文件夹
' zzg_blPointDirectory Boolean 是否要"."或".."[可选参数,默认为“False”]
' 返回: ZZG_collListDirectory Collection 文件或文件夹集合
'*****************************************************************************************
' 引用:
'*****************************************************************************************
' 说明: 参数值可以参数累加起来的和。如既要隐藏又要系统文件,可以参数为6。
' 常规参数总是有效的,因为参数为"0"。
' 也可以用来查某个文件是否存在,方法就是把该文件的路径代入到zzg_strPath参数中
'*****************************************************************************************
' 作者:XXX(此入删去)
' 电邮:[email protected]
' 时间:11/27/2003
'*****************************************************************************************
Public Function ZZG_collListDirectory(zzg_strPath As String, zzg_TypeOpt As Integer, Optional zzg_blPointDirectory As Boolean = False) As Collection
On Error GoTo ZZG_collListDirectory_Err '容错
'定义临时变量
Dim tmpColl As New Collection '存放文件
Dim tmpPath As String '路径
Dim tmpDir As String '取得的文件或文件夹名
'定义部分临时参数值
tmpPath = zzg_strPath '路径
'遍历所有文件或文件夹
tmpDir = Dir(tmpPath, zzg_TypeOpt) '取第一个文件
Do While tmpDir <> "" '当文件取得成功
If zzg_blPointDirectory Then '是否要带"."或".."的文件夹
tmpColl.Add tmpDir '把内容添加到集合中
Else
If tmpDir <> "." And tmpDir <> ".." Then '判断是否是"."或".."
tmpColl.Add tmpDir '把内容添加到集合中
End If
End If
tmpDir = Dir '取文件
Loop
Set ZZG_collListDirectory = tmpColl '返回取得的文件或文件夹集合
Set tmpColl = Nothing
Exit Function '退出函数
ZZG_collListDirectory_Err: '标号
Set tmpColl = Nothing '撤销tmpColl
Dim tmpColl_Err As New Collection '产生一个空集合
Set ZZG_collListDirectory = tmpColl_Err '返回这个空集合
Set tmpColl_Err = Nothing '撤销tmpColl_Err
Exit Function '退出该函数
End Function
因为如果某个文件windows自身删除不了,那么程序也无法删除
Private Type SHFILEOPSTRUCT
hwnd As Long
wFunc As Long '对文件的操作指令
pFrom As String '源文件或路径
pTo As String '目的文件或路径
fFlags As Integer '操作标志
fAnyOperationsAborted As Long
hNameMappings As Long
lpszProgressTitle As String
End TypePrivate Declare Function SHFileOperation Lib _
"shell32" _
(lpFileOp As SHFILEOPSTRUCT) As LongConst FO_COPY = &H2
Const FO_DELETE = &H3
Const FO_MOVE = &H1
Const FO_RENAME = &H4
Const FOF_ALLOWUNDO = &H40
Const FOF_NOCONFIRMATION = &H10
Dim DirString As StringPrivate Sub Command2_Click()
Dim xFile As SHFILEOPSTRUCT
'删除根目录下的temp.txt文件
xFile.pFrom = "c:\bmp\*.*"
'xFile.pTo = "c:\"
xFile.wFunc = FO_DELETE
xFile.hwnd = Me.hwnd
'将fFlags设置为FOF_ALLOWUNDO
'允许被删除的文件放置到回收站中
xFile.fFlags = FOF_ALLOWUNDO
If SHFileOperation(xFile) Then
Debug.Print "Success"
End If
End Sub