• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 144
  • Last Modified:

Reading through all files on HDD?


Anyone now how can I recursively read through all the directories on my HDD, using VB?

I also need to get on certain files the size, date and if available the extra info like version numbers (i.e on some DLL, EXE file)

Thanks for all help.

  • 2
1 Solution
Her's code that scans for the power point program, it reads all files and directories recursively.

Option Explicit
Dim sThePath As String

Private Function bIsHardDrive(ByVal sName As String)

    bIsHardDrive = False
    If InStr(sName, "[") <> 0 And InStr(sName, "]") <> 0 And InStr(sName, "\\") = 0 Then
        If InStr(sName, "[") < InStr(sName, "]") Then
            bIsHardDrive = True
        End If
    End If

End Function

Private Function bIsNetworkDrive(ByVal sName As String)

    bIsNetworkDrive = False
    If InStr(sName, "[") <> 0 And InStr(sName, "]") <> 0 And InStr(sName, "\\") <> 0 Then
        If InStr(sName, "[") < InStr(sName, "]") And _
           InStr(sName, "[") < InStr(sName, "\\") And _
           InStr(sName, "\\") < InStr(sName, "]") Then
            bIsNetworkDrive = True
        End If
    End If

End Function

Private Function scanners(ByVal sFile As String, ByRef bFound As Boolean, ByRef sFoundPath As String) As String

    Dim sPath As String
    Dim lSubDir As Long, lDirCount As Long
    Dim sResult As String, sBasePath As String
    sPath = DirAvail.Path
    lblPath(0).Caption = sPath
    sResult = Dir(sPath & "\" & sFile)
    If sResult <> "" Then
        bFound = True
        sFoundPath = sPath
        Exit Function
    End If
    lDirCount = DirAvail.ListCount - 1
    sBasePath = DirAvail.Path
    For lSubDir = 0 To lDirCount
        DirAvail.Path = DirAvail.List(lSubDir)
        Call scanners(sFile, bFound, sFoundPath)
        DirAvail = sBasePath
        If bFound Then Exit Function
End Function

Private Sub Search(sScanType As String, bFound As Boolean, _
                   bNetworkFound As Boolean, bFixed As Boolean)

        Dim i As Integer

        For i = 0 To drvAvail.ListCount - 1
            On Error Resume Next
            drvAvail.ListIndex = i
            If Err.Number = 0 Then
                If bIsHardDrive(drvAvail.Drive) And sScanType = "F" Or _
                   bIsNetworkDrive(drvAvail.Drive) And sScanType = "N" Or _
                   sScanType = "E" And Not bIsNetworkDrive(drvAvail.Drive) _
                   And LCase(Left(drvAvail.Drive, 1)) <> "a" _
                   And LCase(Left(drvAvail.Drive, 1)) <> "b" And _
                   Dir(Left(drvAvail.Drive, 1) & ":\data1.cab") = "" And _
                   Dir(Left(drvAvail.Drive, 1) & ":\data1.cab") = "" Then
                   If sScanType = "F" Then bFixed = True
                    If Err.Number = 0 Then
                        DirAvail.Path = Left(drvAvail.Drive, 1) & ":\"
                        Call scanners("ppview32.exe", bFound, sThePath)
                        If bFound Then Exit For
                    End If
                    If bIsNetworkDrive(drvAvail.Drive) And sScanType = "F" Then
                        bNetworkFound = True
                    End If
                End If
            End If
            On Error GoTo 0
            Err.Number = 0
End Sub

    Private Sub Main()

    Dim bResult As Boolean
    Dim bNetwork As Boolean
    Dim bFixed As Boolean
    Dim iResponse As Integer

    MousePointer = vbHourglass
    Call Search("F", bResult, bNetwork, bFixed)
    MousePointer = vbDefault

    If bFixed = False Then
        MousePointer = vbHourglass
        Call Search("E", bResult, bNetwork, bFixed)
        MousePointer = vbDefault

    End If
    If bResult = False And bNetwork = True Then
        iResponse = MsgBox("The Power Point Viewer could not be found on the fixed drives" & vbCrLf & vbCrLf & "Do you wish to search the network drives", vbYesNo)
        If iResponse = vbYes Then
            MousePointer = vbHourglass
            Call Search("N", bResult, bNetwork, bFixed)
            MousePointer = vbDefault
        End If
    End If
    If bResult Then
    End If

End Sub

Private Function UpdateIni()

    SaveSetting "Autograf", "Presentations", "ViewerPath", sThePath _
    & IIf(Left(sThePath, 1) = "\", "", "\")

End Function

antony061897Author Commented:
Thanks for the quick response, but drvAvail is not defined.

Where do I get this from?

Put a dirlistbox on your form and name it dirAvail
put a drivelistbox on your form and name it drvAvail

also create a control array lblPath(0) and lblPath(1) (or create your own labels and remove the references - or simply remove the references to labels from the code)

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now