Link to home
Start Free TrialLog in
Avatar of computech1
computech1

asked on

scanning Files On C Drive

hi There,

I need to scan drive C and store my files location in a DB. (something like dir /s)
but i can't run the dir function 2 times together, i'm getting an error.


Private Sub Form_Load()

Dim A As String
Dim B As String

A = Dir("C:\2\", vbDirectory)
Do While A <> ""

If A <> "." And A <> ".." Then

    B = Dir("C:\2\" & A, vbDirectory)
       
        Do While B <> ""
        If B <> "." And B <> ".." Then
 
            MsgBox B
               
        End If
        B = Dir()   ' Get next entry.
    Loop
   
End If
A = Dir()
Loop

End Sub

After scanning the location of B and return to "A = Dir()" I'm geting the error.

What can I do ? I need to scan all the way down to the lowest directory, get the file's name and go up one level to next directory.

Dir() function can't handel it.

Any alternative ?

Nir.


Avatar of Cimperiali
Cimperiali
Flag of Italy image

Do you want to use Dir?
Then Balena did it this way (else, you could use FileSystemObject):
GetAllFiles - Search files in a directory or directory tree
Date: 11/18/2000
Versions: VB6 VBS Level: Intermediate
Author: The VB2TheMax Team

' Returns a collection with the names of all the files
' that match a file specification
'
' The file specification can include wildcards; multiple
' specifications can be provided, using a semicolon-delimited
' list, as in "*.tmp;*.bat"
' If RECURSEDIR is True the search is extended to all subdirectories
'
' It raises no error if path is invalid
'

Function GetAllFiles(ByVal path As String, ByVal filespec As String, _
    Optional RecurseDirs As Boolean) As Collection
    Dim spec As Variant
    Dim file As Variant
    Dim subdir As Variant
    Dim subdirs As New Collection
    Dim specs() As String
   
    ' initialize the result
    Set GetAllFiles = New Collection
   
    ' ensure that path has a trailing backslash
    If Right$(path, 1) <> "\" Then path = path & "\"
   
    ' get the list of provided file specifications
    specs() = Split(filespec, ";")
   
    ' this is necessary to ignore duplicates in result
    ' caused by overlapping file specifications
    On Error Resume Next
               
    ' at each iteration search for a different filespec
    For Each spec In specs
        ' start the search
        file = Dir$(path & spec)
        Do While Len(file)
            ' we've found a new file
            file = path & file
            GetAllFiles.Add file, file
            ' get ready for the next iteration
            file = Dir$
        Loop
    Next
   
    ' first, build the list of subdirectories to be searched
    If RecurseDirs Then
        ' get the collection of subdirectories
        ' start the search
        file = Dir$(path & "*.*", vbDirectory)
        Do While Len(file)
            ' we've found a new directory
            If file = "." Or file = ".." Then
                ' exclude the "." and ".." entries
            ElseIf (GetAttr(path & file) And vbDirectory) = 0 Then
                ' ignore regular files
            Else
                ' this is a directory, include the path in the collection
                file = path & file
                subdirs.Add file, file
            End If
            ' get next directory
            file = Dir$
        Loop
       
        ' parse each subdirectory
        For Each subdir In subdirs
            ' use GetAllFiles recursively
            For Each file In GetAllFiles(subdir, filespec, True)
                GetAllFiles.Add file, file
            Next
        Next
    End If
   
End Function

 
Avatar of GivenRandy
GivenRandy

>scan drive C and store my files location in a DB. (something like dir /s)
Why not just use the dir/s ? :

shell(environ("comspec") & " /c dir c: /s /b > c:\AllFiles.txt")

then when done, just open the file ("c:\AllFiles.txt") and move each line into the database as needed.

Avatar of computech1

ASKER

Cimperiali's code is great but with one problem.

collection object can store only 256 items, I need to scan a full drive (much more then 256 files .... )

Nir.

ASKER CERTIFIED SOLUTION
Avatar of Cimperiali
Cimperiali
Flag of Italy image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Great Code !! thanks.