Private Sub seachfile(ByVal MyPath As String) MyName = Dir(MyPath, vbNormal + vbDirectory + vbSystem + vbReadOnly) Do While MyName <> "" If MyName <> "." And MyName <> ".." Then Debug.Print MyName End If MyName = Dir Loop End SubPrivate Sub Command1_Click() seachfile "D:\My Documents\" End Sub
最好就不要用到FSO对象 ??? FSO 代码简单明了, 既然排斥俺也不贴代码了
Option Explicit'指定文件夹,搜索相关文件 '你变通一下符合你的要求 '/* VBAdvisor 13/Jan/2010 择自我在另外论坛的回复Private Type FILE_INFO Filename As String FileDate As String FileSize As Long End TypePrivate Const LOCALE_USER_DEFAULT As Long = &H400 Private Const LOCALE_NOUSEROVERRIDE As Long = &H80000000 Private Const DATE_SHORTDATE As Long = &H1Private Type SYSTEMTIME wYear As Integer wMonth As Integer wDayOfWeek As Integer wDay As Integer wHour As Integer wMinute As Integer wSecond As Integer wMilliseconds As Integer End TypePrivate Type FILETIME dwLowDateTime As Long dwHighDateTime As Long End Type Private Type WIN32_FIND_DATA 'NT/XP Unicode Support 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(0 To 519) As Byte cAlternate(0 To 27) As Byte End TypePrivate Type SHFILEINFO hIcon As Long iIcon As Long dwAttributes As Long szDisplayName(0 To 519) As Byte szTypeName(0 To 159) As Byte End TypePrivate Const MAX_PATH As Long = 260 Private Const INVALID_HANDLE_VALUE As Long = -1 Private Const FILE_ATTRIBUTE_DIRECTORY As Long = &H10 Private Const FILE_ATTRIBUTE_READONLY As Long = &H1 Private Const FILE_ATTRIBUTE_RODIRECTORY As Long = FILE_ATTRIBUTE_DIRECTORY + FILE_ATTRIBUTE_READONLY'// Private Const SHGFI_SMALLICON As Long = &H1 Private Const SHGFI_PIDL As Long = &H8 Private Const SHGFI_DISPLAYNAME As Long = &H200 Private Const SHGFI_SYSICONINDEX As Long = &H4000Private Const FILE_ATTRIBUTE_NORMAL = &H80 Private Const SHGFI_ICON As Long = &H100Private Const SI_FOLDER_CLOSED As Long = &H3Private Declare Function FindFirstFileW Lib "kernel32" (ByVal lpFileName As Long, lpFindFileData As WIN32_FIND_DATA) As Long Private Declare Function FindNextFileW Lib "kernel32" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long Private Declare Function SHGetFileInfoW Lib "shell32.dll" (ByVal pszPath As Long, ByVal dwAttributes As Long, psfi As SHFILEINFO, ByVal cbSizeFileInfo As Long, ByVal uFlags As Long) As Long Private Declare Function SHGetSpecialFolderPathW Lib "shell32.dll" (ByVal hWnd As Long, ByVal lpszPath As String, ByVal nFolder As Integer, ByVal fCreate As Boolean) As Boolean Private Declare Function GetWindowsDirectoryW Lib "kernel32.dll" (ByVal lpBuffer As Long, ByVal nSize As Long) As Long Private Declare Function ExtractIconW Lib "shell32.dll" (ByVal hinst As Long, ByVal lpszExeFileName As Long, ByVal nIconIndex As Long) As LongPrivate Declare Function FileTimeToLocalFileTime Lib "kernel32" (lpFileTime As FILETIME, lpLocalFileTime As FILETIME) As Long Private Declare Function FileTimeToSystemTime Lib "kernel32" (lpFileTime As FILETIME, lpSystemTime As SYSTEMTIME) As LongPrivate Declare Function GetDateFormat Lib "kernel32" Alias "GetDateFormatA" (ByVal Locale As Long, ByVal dwFlags As Long, lpDate As SYSTEMTIME, ByVal lpFormat As String, ByVal lpDateStr As String, ByVal cchDate As Long) As Long Private Declare Function GetTimeFormat Lib "kernel32" Alias "GetTimeFormatA" (ByVal Locale As Long, ByVal dwFlags As Long, lpTime As SYSTEMTIME, ByVal lpFormat As String, ByVal lpTimeStr As String, ByVal cchTime As Long) As LongDim uFiles() As FILE_INFOPrivate Function StripNulls(ByVal sString As String) As StringDim lPos As Long lPos = InStr(sString, vbNullChar) If (lPos = 1) Then StripNulls = vbNullString ElseIf (lPos > 1) Then StripNulls = Left$(sString, lPos - 1) Exit Function End If StripNulls = sStringEnd FunctionPrivate Function PathIsValid(ByVal path As String) As BooleanDim uWFD As WIN32_FIND_DATA Dim hSearch As Long path = path & IIf(Right$(path, 1) <> "\", "\", vbNullString) hSearch = FindFirstFileW(StrPtr(path & "*.*" & vbNullChar), uWFD) PathIsValid = (hSearch <> INVALID_HANDLE_VALUE) Call FindClose(hSearch)End IfEnd FunctionPublic Function QualifyPath(ByVal sPath As String) As StringDim Delimiter As String ' segmented path delimiter If InStr(sPath, "://") > 0 Then ' it's a URL path Delimiter = "/" ' use URL path delimiter Else ' it's a disk based path Delimiter = "\" ' use disk based path delimiter End If Select Case Right$(sPath, 1) ' whats last character in path? Case "/", "\" ' it's one of the valid delimiters QualifyPath = sPath ' use the supplied path Case Else ' needs a trailing path delimiter QualifyPath = sPath & Delimiter ' append it End SelectEnd FunctionPrivate Static Function GetFileDateTimeStr(uFileTime As FILETIME) As StringDim uFT As FILETIME Dim uST As SYSTEMTIME Call FileTimeToLocalFileTime(uFileTime, uFT) Call FileTimeToSystemTime(uFT, uST) GetFileDateTimeStr = GetFileDateStr(uST) & " " & GetFileTimeStr(uST)End FunctionPrivate Static Function GetFileDateStr(uSystemTime As SYSTEMTIME) As StringDim sDate As String * 32 Dim lLen As Long lLen = GetDateFormat(LOCALE_USER_DEFAULT, LOCALE_NOUSEROVERRIDE Or DATE_SHORTDATE, uSystemTime, vbNullString, sDate, 64) If (lLen) Then GetFileDateStr = Left$(sDate, lLen - 1) End IfEnd FunctionPrivate Static Function GetFileTimeStr(uSystemTime As SYSTEMTIME) As StringDim sTime As String * 32 Dim lLen As Long lLen = GetTimeFormat(LOCALE_USER_DEFAULT, LOCALE_NOUSEROVERRIDE, uSystemTime, vbNullString, sTime, 64) If (lLen) Then GetFileTimeStr = Left$(sTime, lLen - 1) End IfEnd FunctionPrivate Function GetFiles(ByVal sFoldName As String, ByVal sMask As String, uFile() As FILE_INFO) As BooleanDim uFileTmp() As FILE_INFO Dim sExt As String Dim lExtSep As Long Dim lCount As Long Dim lc As LongDim uWFD As WIN32_FIND_DATA Dim hSearch As Long Dim hNext As Long Dim sMaskExt As String'-- Initial storage ReDim uFileTmp(100) '-- Start searching files (all) hNext = 1 hSearch = FindFirstFileW(StrPtr(QualifyPath(sFoldName) & "*.*" & vbNullChar), uWFD) If (hSearch <> INVALID_HANDLE_VALUE) Then Do While hNext If (uWFD.dwFileAttributes <> FILE_ATTRIBUTE_DIRECTORY) Then '-- Get file name, date and size With uFileTmp(lCount) .Filename = StripNulls(uWFD.cFileName) .FileDate = GetFileDateTimeStr(uWFD.ftLastWriteTime) .FileSize = uWFD.nFileSizeHigh * &HFFFF0000 + uWFD.nFileSizeLow End With lCount = lCount + 1 '-- Resize array [?] If ((lCount Mod 100) = 0) Then ReDim Preserve uFileTmp(UBound(uFileTmp()) + 100) End If End If hNext = FindNextFileW(hSearch, uWFD) Loop hNext = FindClose(hSearch) End If ReDim Preserve uFileTmp(lCount - -(lCount > 0)) '-- Filter files If (lCount > 0) Then lCount = 0 ReDim uFile(100) sMaskExt = UCase$(sMask) '-- Check all files For lc = 0 To UBound(uFileTmp()) '-- Extension ? lExtSep = InStrRev(uFileTmp(lc).Filename, ".") If (lExtSep) Then '-- Get extension sExt = UCase$("|" & Mid$(uFileTmp(lc).Filename, lExtSep + 1) & "|") '-- Supported file If (InStr(1, sMaskExt, sExt)) Then '-- Get this file uFile(lCount) = uFileTmp(lc) lCount = lCount + 1 '-- Resize array [?] If ((lCount Mod 100) = 0) Then ReDim Preserve uFile(UBound(uFile()) + 100) End If End If End If Next lc ReDim Preserve uFile(lCount - -(lCount > 0)) End If '-- Success GetFiles = (lCount > 0)End FunctionPrivate Sub Command1_Click()Dim sMask As String Dim sFold As String Dim i As Long sMask = "|BMP|JPG|PNG|" sFold = "C:\Windows\" GetFiles sFold, sMask, uFiles For i = LBound(uFiles) To UBound(uFiles) Debug.Print sFold & uFiles(i).Filename NextEnd Sub
http://topic.csdn.net/u/20100121/11/999C029E-B9D2-4FA0-A2A4-A5F4ED2513F5.html
MyName = Dir(MyPath, vbNormal + vbDirectory + vbSystem + vbReadOnly)
Do While MyName <> ""
If MyName <> "." And MyName <> ".." Then
Debug.Print MyName
End If
MyName = Dir
Loop
End SubPrivate Sub Command1_Click()
seachfile "D:\My Documents\"
End Sub
'你变通一下符合你的要求
'/* VBAdvisor 13/Jan/2010 择自我在另外论坛的回复Private Type FILE_INFO
Filename As String
FileDate As String
FileSize As Long
End TypePrivate Const LOCALE_USER_DEFAULT As Long = &H400
Private Const LOCALE_NOUSEROVERRIDE As Long = &H80000000
Private Const DATE_SHORTDATE As Long = &H1Private Type SYSTEMTIME
wYear As Integer
wMonth As Integer
wDayOfWeek As Integer
wDay As Integer
wHour As Integer
wMinute As Integer
wSecond As Integer
wMilliseconds As Integer
End TypePrivate Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Private Type WIN32_FIND_DATA 'NT/XP Unicode Support
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(0 To 519) As Byte
cAlternate(0 To 27) As Byte
End TypePrivate Type SHFILEINFO
hIcon As Long
iIcon As Long
dwAttributes As Long
szDisplayName(0 To 519) As Byte
szTypeName(0 To 159) As Byte
End TypePrivate Const MAX_PATH As Long = 260
Private Const INVALID_HANDLE_VALUE As Long = -1
Private Const FILE_ATTRIBUTE_DIRECTORY As Long = &H10
Private Const FILE_ATTRIBUTE_READONLY As Long = &H1
Private Const FILE_ATTRIBUTE_RODIRECTORY As Long = FILE_ATTRIBUTE_DIRECTORY + FILE_ATTRIBUTE_READONLY'//
Private Const SHGFI_SMALLICON As Long = &H1
Private Const SHGFI_PIDL As Long = &H8
Private Const SHGFI_DISPLAYNAME As Long = &H200
Private Const SHGFI_SYSICONINDEX As Long = &H4000Private Const FILE_ATTRIBUTE_NORMAL = &H80
Private Const SHGFI_ICON As Long = &H100Private Const SI_FOLDER_CLOSED As Long = &H3Private Declare Function FindFirstFileW Lib "kernel32" (ByVal lpFileName As Long, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindNextFileW Lib "kernel32" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
Private Declare Function SHGetFileInfoW Lib "shell32.dll" (ByVal pszPath As Long, ByVal dwAttributes As Long, psfi As SHFILEINFO, ByVal cbSizeFileInfo As Long, ByVal uFlags As Long) As Long
Private Declare Function SHGetSpecialFolderPathW Lib "shell32.dll" (ByVal hWnd As Long, ByVal lpszPath As String, ByVal nFolder As Integer, ByVal fCreate As Boolean) As Boolean
Private Declare Function GetWindowsDirectoryW Lib "kernel32.dll" (ByVal lpBuffer As Long, ByVal nSize As Long) As Long
Private Declare Function ExtractIconW Lib "shell32.dll" (ByVal hinst As Long, ByVal lpszExeFileName As Long, ByVal nIconIndex As Long) As LongPrivate Declare Function FileTimeToLocalFileTime Lib "kernel32" (lpFileTime As FILETIME, lpLocalFileTime As FILETIME) As Long
Private Declare Function FileTimeToSystemTime Lib "kernel32" (lpFileTime As FILETIME, lpSystemTime As SYSTEMTIME) As LongPrivate Declare Function GetDateFormat Lib "kernel32" Alias "GetDateFormatA" (ByVal Locale As Long, ByVal dwFlags As Long, lpDate As SYSTEMTIME, ByVal lpFormat As String, ByVal lpDateStr As String, ByVal cchDate As Long) As Long
Private Declare Function GetTimeFormat Lib "kernel32" Alias "GetTimeFormatA" (ByVal Locale As Long, ByVal dwFlags As Long, lpTime As SYSTEMTIME, ByVal lpFormat As String, ByVal lpTimeStr As String, ByVal cchTime As Long) As LongDim uFiles() As FILE_INFOPrivate Function StripNulls(ByVal sString As String) As StringDim lPos As Long lPos = InStr(sString, vbNullChar) If (lPos = 1) Then
StripNulls = vbNullString
ElseIf (lPos > 1) Then
StripNulls = Left$(sString, lPos - 1)
Exit Function
End If StripNulls = sStringEnd FunctionPrivate Function PathIsValid(ByVal path As String) As BooleanDim uWFD As WIN32_FIND_DATA
Dim hSearch As Long path = path & IIf(Right$(path, 1) <> "\", "\", vbNullString) hSearch = FindFirstFileW(StrPtr(path & "*.*" & vbNullChar), uWFD) PathIsValid = (hSearch <> INVALID_HANDLE_VALUE) Call FindClose(hSearch)End IfEnd FunctionPublic Function QualifyPath(ByVal sPath As String) As StringDim Delimiter As String ' segmented path delimiter If InStr(sPath, "://") > 0 Then ' it's a URL path
Delimiter = "/" ' use URL path delimiter
Else ' it's a disk based path
Delimiter = "\" ' use disk based path delimiter
End If Select Case Right$(sPath, 1) ' whats last character in path?
Case "/", "\" ' it's one of the valid delimiters
QualifyPath = sPath ' use the supplied path
Case Else ' needs a trailing path delimiter
QualifyPath = sPath & Delimiter ' append it
End SelectEnd FunctionPrivate Static Function GetFileDateTimeStr(uFileTime As FILETIME) As StringDim uFT As FILETIME
Dim uST As SYSTEMTIME Call FileTimeToLocalFileTime(uFileTime, uFT)
Call FileTimeToSystemTime(uFT, uST) GetFileDateTimeStr = GetFileDateStr(uST) & " " & GetFileTimeStr(uST)End FunctionPrivate Static Function GetFileDateStr(uSystemTime As SYSTEMTIME) As StringDim sDate As String * 32
Dim lLen As Long lLen = GetDateFormat(LOCALE_USER_DEFAULT, LOCALE_NOUSEROVERRIDE Or DATE_SHORTDATE, uSystemTime, vbNullString, sDate, 64)
If (lLen) Then
GetFileDateStr = Left$(sDate, lLen - 1)
End IfEnd FunctionPrivate Static Function GetFileTimeStr(uSystemTime As SYSTEMTIME) As StringDim sTime As String * 32
Dim lLen As Long lLen = GetTimeFormat(LOCALE_USER_DEFAULT, LOCALE_NOUSEROVERRIDE, uSystemTime, vbNullString, sTime, 64)
If (lLen) Then
GetFileTimeStr = Left$(sTime, lLen - 1)
End IfEnd FunctionPrivate Function GetFiles(ByVal sFoldName As String, ByVal sMask As String, uFile() As FILE_INFO) As BooleanDim uFileTmp() As FILE_INFO
Dim sExt As String
Dim lExtSep As Long
Dim lCount As Long
Dim lc As LongDim uWFD As WIN32_FIND_DATA
Dim hSearch As Long
Dim hNext As Long
Dim sMaskExt As String'-- Initial storage ReDim uFileTmp(100) '-- Start searching files (all)
hNext = 1
hSearch = FindFirstFileW(StrPtr(QualifyPath(sFoldName) & "*.*" & vbNullChar), uWFD) If (hSearch <> INVALID_HANDLE_VALUE) Then Do While hNext If (uWFD.dwFileAttributes <> FILE_ATTRIBUTE_DIRECTORY) Then '-- Get file name, date and size
With uFileTmp(lCount)
.Filename = StripNulls(uWFD.cFileName)
.FileDate = GetFileDateTimeStr(uWFD.ftLastWriteTime)
.FileSize = uWFD.nFileSizeHigh * &HFFFF0000 + uWFD.nFileSizeLow
End With
lCount = lCount + 1 '-- Resize array [?]
If ((lCount Mod 100) = 0) Then
ReDim Preserve uFileTmp(UBound(uFileTmp()) + 100)
End If
End If
hNext = FindNextFileW(hSearch, uWFD)
Loop
hNext = FindClose(hSearch)
End If ReDim Preserve uFileTmp(lCount - -(lCount > 0)) '-- Filter files
If (lCount > 0) Then
lCount = 0
ReDim uFile(100) sMaskExt = UCase$(sMask)
'-- Check all files
For lc = 0 To UBound(uFileTmp())
'-- Extension ?
lExtSep = InStrRev(uFileTmp(lc).Filename, ".")
If (lExtSep) Then '-- Get extension
sExt = UCase$("|" & Mid$(uFileTmp(lc).Filename, lExtSep + 1) & "|") '-- Supported file
If (InStr(1, sMaskExt, sExt)) Then '-- Get this file
uFile(lCount) = uFileTmp(lc)
lCount = lCount + 1 '-- Resize array [?]
If ((lCount Mod 100) = 0) Then
ReDim Preserve uFile(UBound(uFile()) + 100)
End If
End If
End If
Next lc
ReDim Preserve uFile(lCount - -(lCount > 0))
End If '-- Success
GetFiles = (lCount > 0)End FunctionPrivate Sub Command1_Click()Dim sMask As String
Dim sFold As String
Dim i As Long sMask = "|BMP|JPG|PNG|"
sFold = "C:\Windows\" GetFiles sFold, sMask, uFiles For i = LBound(uFiles) To UBound(uFiles)
Debug.Print sFold & uFiles(i).Filename
NextEnd Sub
findfirstfile()
findnextfile()
以后要编个啥木马病毒了的,都可以直接创建调用,多好?(咳咳...不能去做李俊第二)
我也不贴代码了。
...:我说店小二酱油打好了没?我要走了
1、FSO
2、API
3、dos命令dir /s输出到文本再处理。
4、VB的dir
shell("cmd /c dir /o-d /b /s /ad c:\mydir\*.* >c:\dirs.txt")
'从c:\files.txt和c:\dirs.txt文件中按行读
[/O[[:]sortorder]] [/P] [/Q] [/S] [/T[[:]timefield]] [/W] [/X] [/4] [drive:][path][filename]
指定要列出的驱动器、目录和/或文件。 /A 显示具有指定属性的文件。
attributes D 目录 R 只读文件
H 隐藏文件 A 准备存档的文件
S 系统文件 - 表示“否”的前缀
/B 使用空格式(没有标题信息或摘要)。
/C 在文件大小中显示千位数分隔符。这是默认值。用 /-C 来
停用分隔符显示。
/D 跟宽式相同,但文件是按栏分类列出的。
/L 用小写。
/N 新的长列表格式,其中文件名在最右边。
/O 用分类顺序列出文件。
sortorder N 按名称(字母顺序) S 按大小 (从小到大)
E 按扩展名(字母顺序) D 按日期/时间(从早到晚)
G 组目录优先 - 颠倒顺序的前缀
/P 在每个信息屏幕后暂停。
/Q 显示文件所有者。
/S 显示指定目录和所有子目录中的文件。
/T 控制显示或用来分类的时间字符域。
timefield C 创建时间
A 上次访问时间
W 上次写入的时间
/W 用宽列表格式。
/X 显示为非 8dot3 文件名产生的短名称。格式是 /N 的格式,
短名称插在长名称前面。如果没有短名称,在其位置则
显示空白。
/4 用四位数字显示年