Looping files

Discussion in 'AutoCAD' started by David, Nov 3, 2004.

  1. David

    David Guest

    Hi NG;

    Im trying to create a routine that would lop through a folder of
    files and sub-folders. The sub-folders colud in turn have files and sub-
    folders aso.

    I think I have to use some recursive programming but cant the idea.
    I thougth that someone already might have written a procedure like this.
    But despite all efforts I have put on trying to find the code on the
    internet
    I have had no luck.

    So if anyone have solution I would be glad to take part of that.

    TIA David
     
    David, Nov 3, 2004
    #1
  2. David

    Matt W Guest

    Here you go... This will do exactly what you're looking for.
    I don't remeber where I found this gem of a code, so whoever you are....
    Thanks!

    Hope this helps you out.

    Code:
    Option Explicit
    
    Sub Main()
    Dim colFiles As Collection
    Dim i As Integer
    Set colFiles = New Collection
    
    FindFile colFiles, "e:\work", "dwg"  ' Change the dir to suit your needs
    For i = 1 To colFiles.Count
    Debug.Print colFiles.Item(i)       ' Do something here with each
    drawing
    Next i
    End Sub
    
    
    Public Sub FindFile(ByRef files As Collection, doDir$, ext$)
    '-----------------------------------------------
    ' look through every file and directory for
    '   files with the specified extension.
    '-----------------------------------------------
    Dim dirs As Collection
    Set dirs = New Collection
    Dim file$
    '-----------------------------------------------
    ' add the trailing backslash, if it's not there.
    '-----------------------------------------------
    If (Right(doDir$, 1) <> "\") Then
    doDir$ = doDir$ & "\"
    End If
    
    '-----------------------------------------------
    ' go find the files!
    '-----------------------------------------------
    file$ = Dir(doDir$ & "*.*", vbDirectory)
    Do While (file$ <> "")
    '-----------------------------------------------
    ' check to see if this file is actually a
    '   directory.  ignore the special "." and ".."
    '   files.
    '-----------------------------------------------
    If ((file$ <> ".") And (file$ <> "..")) Then
    If ((GetAttr(doDir$ & file$) And vbDirectory) = vbDirectory)
    Then
    '-----------------------------------------------
    ' this is a directory, so save it so we can
    '   recurse into it later.
    '-----------------------------------------------
    dirs.Add doDir$ & file$
    Else
    '-----------------------------------------------
    ' this is a file, so check its extension.  if
    '   the extension matches what we're looking
    '   for, save the filename.
    '-----------------------------------------------
    Dim thisExt$
    If (Len(file$) > 4) Then
    If (UCase(Right(file$, 3)) = UCase(ext$)) Then
    files.Add doDir$ & file$
    End If
    '                Debug.Print files.Count
    End If
    End If
    End If
    '-----------------------------------------------
    ' find the next file.
    '-----------------------------------------------
    file$ = Dir
    Loop
    
    '-----------------------------------------------
    ' now, recurse into any directories we found.
    '-----------------------------------------------
    If (dirs.Count > 0) Then
    Dim curDir%
    For curDir% = 1 To dirs.Count
    FindFile files, dirs(curDir%), ext$
    Next curDir%
    End If
    
    Set dirs = Nothing
    End Sub
    
     
    Matt W, Nov 3, 2004
    #3
  3. David

    Mikko Guest

    A slightly more condensed VB.NET version.

    This sub will fill a listbox with *.dwg names that you can process as you want.

    Sub ProcessDirectory(ByVal targetDirectory As String)
    Dim fileEntries As String() = System.IO.Directory.GetFiles(targetDirectory)
    Dim fileName As String
    For Each fileName In fileEntries
    If LCase(System.IO.Path.GetExtension(fileName)) = ".dwg" Then
    ListBox1.Items.Add(fileName)
    End If
    Next fileName
    Dim subdirectoryEntries As String() = System.IO.Directory.GetDirectories(targetDirectory)
    Dim subdirectory As String
    For Each subdirectory In subdirectoryEntries
    ProcessDirectory(subdirectory)
    Next subdirectory
    End Sub


    HOW TO USE:

    ProcessDirectory(InputBox("Enter the path to your drawings. ", "Ball-n-Stock", ""))
     
    Mikko, Nov 3, 2004
    #4
  4. David

    David Guest

    Some of that should do the trick
     
    David, Nov 4, 2004
    #5
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.