回 帖 发 新 帖 刷新版面

主题:VB关于扫描文件的问题

我将网上面找的一个关于VB的扫描硬盘所有文件的示例修改了一下贴到这里来,我需要实现的功能基本能实现了,但是只能扫描指定盘的所有文化,怎样才能做到扫描硬盘所有分区的所有文件呢?并且要将文件的总数能统计显示出来就最好,我修改的代码已经差不多能实现了,怎样才能将窗体的代码全部放到模块里去而不影响编译运行.

回复列表 (共4个回复)

沙发

写成函数,在窗体中调用

板凳

Public Declare Function GetLogicalDriveStrings Lib "kernel32" Alias "GetLogicalDriveStringsA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
Public Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" (ByVal nDrive As String) As Long
Public Const DRIVE_FIXED = 3

用第一个函数取得当前系统中的所有有效盘符,然后用第二个函数分别判断每个盘符是否是硬盘分区,最后将硬盘分区盘符变为路径做参数循环调用磁盘扫描程序(自己编制).

3 楼

还不是很明白两位的解答

4 楼

'以下代码是我自己写的一个程序中的一部分,你可以参考一下
Private Const FILE_EXT = "MP3/WAV"
'**** Private const FILE_EXT="*"  ***** 全部文件

Private Sub GetUDiskList()
    On Error Resume Next
    Dim lDrive As Long
    Dim lIndex As Long
    Dim strDrive As String
    Dim lBit As Long
    Dim NewNode As Nodes
    'List1.Clear
    Dim strDriveVolume As String
    
    Set NewNode = TreeView1.Nodes
    lDrive = GetLogicalDrives()
    If lDrive <> 0 Then
        For lIndex = 0 To 25
            lBit = 2 ^ lIndex
            If (lDrive And lBit) = lBit Then
                strDrive = Chr(Asc("A") + lIndex)
                If GetDriveType(strDrive & ":") = DRIVE_FIXED Then
                    
                END IF
            END IF
         NEXT
     END IF

End Sub


Private Sub FindDirectory(strPath As String)
    Dim hFile As Long
    Dim hFileInfo As WIN32_FIND_DATA
    Dim hFirstFile As Long
    Dim strDirectory As String
    
    hFile = FindFirstFile(strPath & "\*.*", hFileInfo)

    FindFile strPath
    
    Do
        If hFileInfo.dwFileAttributes And vbDirectory Then
            
            strDirectory = Left(hFileInfo.cFileName, InStr(1, hFileInfo.cFileName, vbNullChar, vbTextCompare) - 1)
            If strDirectory <> "." And strDirectory <> ".." Then
                 FindDirectory strPath & "\" & strDirectory
            End If
        End If
        DoEvents
    Loop While FindNextFile(hFile, hFileInfo)
    
    FindClose hFile
    
End Sub
Private Sub FindFile(strPath As String)
    Dim hFile As Long
    Dim hFileInfo As WIN32_FIND_DATA
    Dim hFirstFile As Long
    Dim strFileName As String
    Dim lFileSize As Long
    Dim lIndex As Long
    Dim strExt() As String
    
    strExt = Split(FILE_EXT, "/")
    
    For lIndex = 0 To UBound(strExt)
        hFile = FindFirstFile(strPath & "\*." & strExt(lIndex), hFileInfo)
        If hFile = -1 Then
            Exit Sub
        End If
        Do
            If hFileInfo.dwFileAttributes <> vbDirectory Then
                   '在这里处理搜索到的文件
            END IF
             DoEvents
        Loop While FindNextFile(hFile, hFileInfo)
        
        FindClose hFile
    Next
End Sub

我来回复

您尚未登录,请登录后再回复。点此登录或注册