高分求救,想弄个VBA的宏,取得目录下的所有文件的文件名,该怎么办!!!

xbyue 2003-08-22 10:06:52
用EXCEL的宏,谢过了!
...全文
219 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
changechange 2003-08-22
  • 打赏
  • 举报
回复
如何实现遍历文件夹中的所有文件

作者:ec
2003-4-20 9:22:06

〖摘自 access911.net 文章区〗


如何实现遍历文件夹中的所有文件

--------------------------------------------------------------------------------
把下面放到模块中
Option Explicit


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" _
(ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
Public Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long


Public Const MAX_PATH = 260
Public Const FILE_ATTRIBUTE_ARCHIVE = &H20
Public Const FILE_ATTRIBUTE_COMPRESSED = &H800
Public Const FILE_ATTRIBUTE_DIRECTORY = &H10
Public Const FILE_ATTRIBUTE_HIDDEN = &H2
Public Const FILE_ATTRIBUTE_NORMAL = &H80
Public Const FILE_ATTRIBUTE_READONLY = &H1
Public Const FILE_ATTRIBUTE_SYSTEM = &H4
Public Const FILE_ATTRIBUTE_TEMPORARY = &H100

'自定义数据类型FILETIME和WIN32_FIND_DATA的定义
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
----------------------
'--------------------------------------------------------------------------------
' 把当前文件夹路径下的所有文件入到listview中
'--------------------------------------------------------------------------------
Private Sub finfiles(tCurrentdir As String)
Dim itmX As ListItem
Dim tFindData As WIN32_FIND_DATA
Dim strFileName As String
Dim lHandle As Long
Dim CountFolder As Integer
Dim CountFiles As Integer
CountFolder = 0
CountFiles = 0
ListView1.ListItems.Clear
lHandle = FindFirstFile(tCurrentdir & "\*.*", tFindData)
If lHandle = 0 Then
Set itmX = ListView1.ListItems.Add(, , strFileName & "找不到文件")
Exit Sub
End If
strFileName = fDelInvaildChr(tFindData.cFileName)
Do While True
tFindData.cFileName = ""
If FindNextFile(lHandle, tFindData) = 0 Then
FindClose (lHandle)
Exit Do
Else
strFileName = fDelInvaildChr(tFindData.cFileName)
If tFindData.dwFileAttributes = &H10 Then
If strFileName <> "." And strFileName <> "." Then
Set itmX = ListView1.ListItems.Add(, , strFileName)
itmX.SmallIcon = 1
CountFolder = CountFolder + 1
End If
Else
Debug.Print InStr(LCase(Right(strFileName, 3)), ExtendFileName)
If InStr(ExtendFileName, LCase(Right(strFileName, 3))) > 0 Then
Set itmX = ListView1.ListItems.Add(, , strFileName)
itmX.SubItems(1) = CStr(FileLen(tCurrentdir & "\" & strFileName))
itmX.SmallIcon = 2
itmX.SubItems(2) = FileDateTime(tCurrentdir & "\" & strFileName)
CountFiles = CountFiles + 1
End If
End If
End If
Loop
ListView1.Sorted = True
ListView1.SortKey = 1
StatusBar1.Panels(2).Text = CurrentDir
StatusBar1.Panels(3).Text = "文件夹:" & CountFolder & " 文件:" & CountFiles
End Sub



changechange 2003-08-22
  • 打赏
  • 举报
回复
用Dir函数即可:
Sub ListTheFilesName(strPath As String)
Dim strFileName As String
Dim i As Integer

strFileName = Dir(strPath)
i = 1
Do While strFileName <> ""
Sheet1.Cells(i, 1) = strFileName
i = i + 1
strFileName = Dir
Loop

End Sub
TechnoFantasy 2003-08-22
  • 打赏
  • 举报
回复
上面的代码获得目录吓所有的文件并在传递过来的worksheet中列出来,使用范例:

Private Sub Worksheet_Activate()
getFiles ActiveSheet
End Sub
TechnoFantasy 2003-08-22
  • 打赏
  • 举报
回复
Public Function getFiles(addsheet As Worksheet) As Integer
Dim x As Object
Dim f
Dim fs
Dim i
Dim SrcFile

Set x = CreateObject("Scripting.FileSystemObject")
Set f = x.GetFolder("C:\savepath")
Set fs = f.Files
i = 1
For Each SrcFile In fs
'Debug.Print SrcFile.Name
addsheet.Cells(1, i) = SrcFile.Name
i = i + 1
Next

Set fs = Nothing
Set f = Nothing
Set x = Nothing
End Function
superxmz 2003-08-22
  • 打赏
  • 举报
回复
up Dir 查一下帮助
xingnup 2003-08-22
  • 打赏
  • 举报
回复
用Dir函数即可:
Sub ListTheFilesName(strPath As String)
Dim strFileName As String
Dim i As Integer

strFileName = Dir(strPath)
i = 1
Do While strFileName <> ""
Sheet1.Cells(i, 1) = strFileName
i = i + 1
strFileName = Dir
Loop

End Sub

2,462

社区成员

发帖
与我相关
我的任务
社区描述
VBA(Visual Basic for Applications)是Visual Basic的一种宏语言,是在其桌面应用程序中执行通用的自动化(OLE)任务的编程语言。
社区管理员
  • VBA
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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