1、如何获取某个文件夹下的所有某类型文件名称??2、如何获取某文件的属性??多谢。。

解决方案 »

  1.   

    2、取得当前路径
    Declare Function GetFileInformationByHandle Lib "kernel32" Alias "GetFileInformationByHandle" (ByVal hFile As Long, lpFileInformation As BY_HANDLE_FILE_INFORMATION) As Long
    'hFile file handle
    Type BY_HANDLE_FILE-INFORMATION ' 52 Bytes
    dwFileAttributes As Long
    ftCreationTime As FILETIME
    ftLastAccessTime As FILETIME
    ftLastWriteTime As FILETIME
    dwVolumeSerialNumber As Long
    nFileSizeHigh As Long
    nFileSizeLow As Long
    nNumberOfLinks As Long
    nFileIndexHigh As Long
    nFileIndexLow As Long
    End Type 
    说明 
    Used by the GetFileInformationByHandle function to retrieve information about a file. 
    字段表 
    字段 类型及说明 
    dwFileAttributes Long,Refer to the File Attribute Types table that follows. 
    ftCreationTime FILETIME,Time of file creation. Zero if file system does not support file creation time. 
    ftLastAccessTime FILETIME,Time of last access to this file. Zero if file system does not support file last access time. 
    ftLastWriteTime FILETIME,Time of last write operation to this file. 
    dwVolumeSerialNumber Long,Serial number of the volume that contains the file. 
    nFileSizeHigh Long,High-order word of the file size. 
    nFileSizeLow Long,Low-order word of the file size. 
    nNumberOfLinks Long,Number of links to this file. Always 1 for the FAT file system and HPFS. 
    nFileIndexHigh Long,High-order word of a unique identifier associated with the file. 
    nFileIndexLow Long,Low-order word of a unique identifier associated with the file. Use this element and the dwVolumeSerialNumber to uniquely identify a file. 
    File Attribute Types table 
    FILE_ATTRIBUTE_ARCHIVE    Archive file.
    FILE_ATTRIBUTE_COMPRESSED    The file or directory is compressed.
    FILE_ATTRIBUTE_DIRECTORY    File is a directory.
    FILE_ATTRIBUTE_HIDDEN    File is hidden.
    FILE_ATTRIBUTE_NORMAL    File is normal (no other file attributes are specified).
    FILE_ATTRIBUTE_READONLY    File is read-only.
    FILE_ATTRIBUTE_SYSTEM    File is a system file.
    FILE_ATTRIBUTE_NORMAL    File is a temporary file 
      

  2.   

    Private Sub Command1_Click()
        Dim fs As New FileSystemObject  ' 建立 FileSystemObject
        Dim fd As Folder    ' 定义 Folder 对象
        Dim sfd As Folder    Set fd = fs.GetFolder("c:\")
        Command1.Enabled = False
        Screen.MousePointer = vbHourglass
        FindFile fd, Text1.Text
        Command1.Enabled = True
        Screen.MousePointer = vbDefault
    End SubSub FindFile(fd As Folder, FileName As String)
        Dim sfd As Folder, f As File    ' Part I查找该文件夹的所有文件
        For Each f In fd.Files
            If UCase(f.Name) = UCase(FileName) Then '查找文件。
                Debug.Print f.Path
                List1.AddItem f.Path
            End If
            DoEvents
        Next    ' Part II递近查找所有子文件夹
        For Each sfd In fd.SubFolders
            FindFile sfd, FileName  ' 循环查找
        Next
    End Sub
      

  3.   

    1、
    转自...
    ''''''''
    用API函数遍历指定路径的文件 
     
    2001-06-15· ·土人 ··vbeden   以下代码演示了如何用Windows API函数遍历指定驱动器、目录的所有文件。其思路是:调出浏览文件夹窗口让用户指定所要搜索的起始路径,然后用查找文件的API函数遍历该目录下及其包含的子目录下的所有文件。本例需要:一个按钮,一个TextBox和一个ListBox,其中,TextBox应设置为多行。  核心代码参照API-Guide的两个例子程序,特此声明。 Option Explicit'查找第一个文件的API
    Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
    '查找下一个文件的API
    Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
    '获取文件属性的API
    Private Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long
    '关闭查找文件的API
    Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
    '以下为调用浏览文件夹窗口的API
    Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)
    Private Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long
    Private Declare Function SHBrowseForFolder Lib "shell32" (lpbi As BrowseInfo) As Long
    Private Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList As Long, ByVal lpBuffer As String) As Long '常量
    Const MAX_PATH = 260
    Const MAXDWORD = &HFFFF
    Const INVALID_HANDLE_VALUE = -1
    Const FILE_ATTRIBUTE_ARCHIVE = &H20
    Const FILE_ATTRIBUTE_DIRECTORY = &H10
    Const FILE_ATTRIBUTE_HIDDEN = &H2
    Const FILE_ATTRIBUTE_NORMAL = &H80
    Const FILE_ATTRIBUTE_READONLY = &H1
    Const FILE_ATTRIBUTE_SYSTEM = &H4
    Const FILE_ATTRIBUTE_TEMPORARY = &H100
    Const BIF_RETURNONLYFSDIRS = 1
    Private Type FILETIME
    dwLowDateTime As Long
    dwHighDateTime As Long
    End Type
     
    '定义类(用于查找文件)
    Private 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
     
    '定义类(用于浏览文件夹窗口)
    Private Type BrowseInfo
     hWndOwner As Long
     pIDLRoot As Long
     pszDisplayName As Long
     lpszTitle As Long
     ulFlags As Long
     lpfnCallback As Long
     lParam As Long
     iImage As Long
    End Type
     
    '自定义函数
    Function StripNulls(OriginalStr As String) As String
     If (InStr(OriginalStr, Chr(0)) > 0) Then
      OriginalStr = Left(OriginalStr, InStr(OriginalStr, Chr(0)) - 1)
     End If
     StripNulls = OriginalStr
    End Function
     
    '自定义函数
    Function FindFilesAPI(path As String, SearchStr As String, FileCount As Integer, _
    DirCount As Integer)
    Dim FileName As String ' 文件名
    Dim DirName As String ' 子目录名
    Dim dirNames() As String ' 目录数组
    Dim nDir As Integer ' 当前路径的目录数
    Dim i As Integer ' 循环计数器变量
    Dim hSearch As Long ' 搜索句柄变量
    Dim WFD As WIN32_FIND_DATA
    Dim Cont As Integer
    If Right(path, 1) <> "\" Then path = path & "\"
    '搜索子目录
     nDir = 0
     ReDim dirNames(nDir)
     Cont = True
     hSearch = FindFirstFile(path & "*", WFD)
     If hSearch <> INVALID_HANDLE_VALUE Then
      Do While Cont
       DirName = StripNulls(WFD.cFileName)
       If (DirName <> ".") And (DirName <> "..") Then
        If GetFileAttributes(path & DirName) And FILE_ATTRIBUTE_DIRECTORY Then
         dirNames(nDir) = DirName
         DirCount = DirCount + 1
         nDir = nDir + 1
         ReDim Preserve dirNames(nDir)
        End If
       End If
       Cont = FindNextFile(hSearch, WFD) '获取下一个子目录
      Loop
      Cont = FindClose(hSearch)
     End If
     ' 遍历目录并累计文件总数
     hSearch = FindFirstFile(path & SearchStr, WFD)
     Cont = True
     If hSearch <> INVALID_HANDLE_VALUE Then
      While Cont
       FileName = StripNulls(WFD.cFileName)
       If (FileName <> ".") And (FileName <> "..") Then
        FindFilesAPI = FindFilesAPI + (WFD.nFileSizeHigh * MAXDWORD) + WFD.nFileSizeLow
        FileCount = FileCount + 1
        List1.AddItem path & FileName
       End If
       Cont = FindNextFile(hSearch, WFD) ' 获取下一个文件
      Wend
      Cont = FindClose(hSearch)
     End If
     '如果子目录存在则遍历之
     If nDir > 0 Then
      For i = 0 To nDir - 1
       FindFilesAPI = FindFilesAPI + FindFilesAPI(path & dirNames(i) & "\", _
    SearchStr, FileCount, DirCount)
      Next i
     End If
    End Function
     
    '查找按钮代码
    Sub Command1_Click()
     Dim SearchPath As String, FindStr As String
     Dim FileSize As Long
     Dim NumFiles As Integer, NumDirs As Integer
     Dim iNull As Integer, lpIDList As Long, lResult As Long
     Dim sPath As String, udtBI As BrowseInfo
     With udtBI
      '设置浏览窗口
      .hWndOwner = Me.hWnd
      '返回选中的目录
      .ulFlags = BIF_RETURNONLYFSDIRS
     End With
     
     '调出浏览窗口
     lpIDList = SHBrowseForFolder(udtBI)
     If lpIDList Then
      sPath = String$(MAX_PATH, 0)
      '获取路径
      SHGetPathFromIDList lpIDList, sPath
      '释放内存
      CoTaskMemFree lpIDList
      iNull = InStr(sPath, vbNullChar)
      If iNull Then
       sPath = Left$(sPath, iNull - 1)
      End If
     End If
     Screen.MousePointer = vbHourglass
     List1.Clear
     SearchPath = sPath '选中的目录为搜索的起始路径
     FindStr = "*.*" '搜索所有类型的文件(此处可另作定义)
     FileSize = FindFilesAPI(SearchPath, FindStr, NumFiles, NumDirs)
     Text1.Text = "查找到的文件数:" & NumFiles & vbCrLf & "查找的目录数:" & _
    NumDirs + 1 & vbCrLf & "文件大小总共为:" & vbCrLf & _
    Format(FileSize, "#,###,###,##0") & "字节"
     Screen.MousePointer = vbDefault
    End Sub 
     
      

  4.   

    Private Sub Command1_Click()
        Dim fs As New FileSystemObject  ' 建立 FileSystemObject
        Dim fd As Folder    ' 定义 Folder 对象
        Dim sfd As Folder    Set fd = fs.GetFolder("c:\")
        Command1.Enabled = False
        Screen.MousePointer = vbHourglass
        FindFile fd,"*.txt" '查找所有TXT文件
        Command1.Enabled = True
        Screen.MousePointer = vbDefault
    End SubSub FindFile(fd As Folder, FileName As String)
        Dim sfd As Folder, f As File    ' Part I查找该文件夹的所有文件
        For Each f In fd.Files
            If UCase(f.Name) Like UCase(FileName) Then
                Debug.Print f.Path
                List1.AddItem f.Path
            End If
            DoEvents
        Next    ' Part II循环查找所有子文件夹
        For Each sfd In fd.SubFolders
            FindFile sfd, FileName  ' 循环查找
        Next
    End Sub
      

  5.   

    Option ExplicitPrivate Sub Command1_Click()
        Dim fs As New FileSystemObject
        Dim fd As Folder, f As File
        Dim attr As Long
        
        ' 组合文件属性值
        attr = IIf(chkReadOnly.Value = 1, ReadOnly, 0)
        attr = attr + IIf(chkArchive.Value = 1, Archive, 0)
        attr = attr + IIf(chkHidden.Value = 1, Hidden, 0)
        attr = attr + IIf(chkSystem.Value = 1, System, 0)    List1.Clear
        Set fd = fs.GetFolder("c:\")
        For Each f In fd.Files
            If (f.Attributes And attr) = attr Then
                List1.AddItem f.Name
            End If
        Next
    End Sub
      

  6.   

    Attributes 属性
             描述设置或者返回文件或文件夹的属性。读/写或只读,取决于属性。语法object.Attributes [= newattributes]Attributes 属性有下列几个部分:部分 描述 
    object 必需的。总是某个 File 或者 Folder 对象的名字。 
    newattributes 可选的。如果提供的话,newattributes 就是所指定 object 的新属性值。 
    设置newattributes 参数可以是具有下列值中的任意一个或任意的逻辑组合:常数 值 描述 
    Normal 0 一般文件。未设置属性。 
    ReadOnly 1 只读文件。属性为读/写。 
    Hidden 2 隐藏文件。属性为读/写。 
    System 4 系统文件。属性为读/写。 
    Volume 8 磁盘驱动器卷标。属性为只读。 
    Directory 16 文件夹或目录。属性为只读。 
    Archive 32 自上次备份后已经改变的文件。属性为读/写。 
    Alias 64 链接或快捷方式。属性为只读。 
    Compressed 128 压缩文件。属性为只读。 
    说明下面的代码用一个文件举例说明了 Attributes 属性的用法:Sub SetClearArchiveBit(filespec)
        Dim fs, f, r
        Set fs = CreateObject("Scripting.FileSystemObject")
        Set f = fs.GetFile(fs.GetFileName(filespec))
        If f.attributes and 32 Then
            r = MsgBox("The Archive bit is set, do you want to clear it?", vbYesNo, "Set/Clear Archive Bit")
            If r = vbYes Then 
                f.attributes = f.attributes - 32
                MsgBox "Archive bit is cleared."
            Else
                MsgBox "Archive bit remains set."
            End If
        Else
            r = MsgBox("The Archive bit is not set. Do you want to set it?", vbYesNo, "Set/Clear Archive Bit")
            If r = vbYes Then 
                f.attributes = f.attributes + 32
                MsgBox "Archive bit is set."
            Else
                MsgBox "Archive bit remains clear."
            End If
        End If
    End Sub
      

  7.   

    加一个FILE控件就可以了!!,
    属性可以用GetAttr函数,如下!!返回一个 Integer,此为一个文件、目录、或文件夹的属性。语法GetAttr(pathname)必要的 pathname 参数是用来指定一个文件名的字符串表达式。pathname 可以包含目录或文件夹、以及驱动器。返回值由 GetAttr 返回的值,是下面这些属性值的总和:常数 值 描述
       
    vbNormal 0 常规
    vbReadOnly 1 只读
    vbHidden 2 隐藏
    vbSystem 4 系统
    vbDirectory 16 目录或文件夹
    vbArchive 32 上次备份以后,文件已经改变
    注意   这些常数是由 VBA 指定的,在程序代码中的任何位置,可以使用这些常数来替换真正的值。说明若要判断是否设置了某个属性,在 GetAttr 函数与想要得知的属性值之间使用 And 运算符与逐位比较。如果所得的结果不为零,则表示设置了这个属性值。例如,在下面的 And 表达式中,如果档案 (Archive) 属性没有设置,则返回值为零:Result = GetAttr(FName) And vbArchive如果文件的档案属性已设置,则返回非零的数值。
      

  8.   

    Option Explicit
    Private Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long
    Private Const FILE_ATTRIBUTE_ARCHIVE = &H20
    Private Const FILE_ATTRIBUTE_COMPRESSED = &H800
    Private Const FILE_ATTRIBUTE_DIRECTORY = &H10
    Private Const FILE_ATTRIBUTE_HIDDEN = &H2
    Private Const FILE_ATTRIBUTE_NORMAL = &H80
    Private Const FILE_ATTRIBUTE_READONLY = &H1
    Private Const FILE_ATTRIBUTE_SYSTEM = &H4
    Private Const FILE_ATTRIBUTE_TEMPORARY = &H100Private Sub Command1_Click()
        Dim lngRet As Long
        
        lngRet = GetFileAttributes("c:\test.txt")
        If lngRet And FILE_ATTRIBUTE_ARCHIVE Then
            MsgBox "FILE_ATTRIBUTE_ARCHIVE", vbInformation
        End If
        
        If lngRet And FILE_ATTRIBUTE_COMPRESSED Then
            MsgBox "FILE_ATTRIBUTE_COMPRESSED", vbInformation
        End If
        
        If lngRet And FILE_ATTRIBUTE_DIRECTORY Then
            MsgBox "FILE_ATTRIBUTE_DIRECTORY", vbInformation
        End If
        
        If lngRet And FILE_ATTRIBUTE_HIDDEN Then
            MsgBox "FILE_ATTRIBUTE_HIDDEN", vbInformation
        End If
        
        If lngRet And FILE_ATTRIBUTE_NORMAL Then
            MsgBox "FILE_ATTRIBUTE_NORMAL", vbInformation
        End If
        
        If lngRet And FILE_ATTRIBUTE_READONLY Then
            MsgBox "FILE_ATTRIBUTE_READONLY", vbInformation
        End If
        
        If lngRet And FILE_ATTRIBUTE_SYSTEM Then
            MsgBox "FILE_ATTRIBUTE_SYSTEM", vbInformation
        End If
        
        If lngRet And FILE_ATTRIBUTE_TEMPORARY Then
            MsgBox "FILE_ATTRIBUTE_TEMPORARY", vbInformation
        End If
    End Sub
      

  9.   

    多谢各位了!!!
    呵呵,这是我在csdn上得到帮助最多,最有用的一次!!!
    感到社会的温暖呀!!
    不过
    非常的不好意思我少说了一点,我想要的属性是一个文件的创建时间,大小之类的了!!!再次感谢!!!…………………………
    BlueCrystal
    你相信有神吗?
    我信,因为你就是!