views:

476

answers:

6

Hi ,I would like to know if there are some in built functions for the scenario that is described below :

The input is the path of a parent folder. Wat the function must do is , it should list out all the .zip files inside that parent folder.The parent folder can contain any number of subfolders, and the same applies to the subfolders too .. Can anybody help me out with that ?

VB version is not a barricade. Any of the versions VB6 or VS2005 can do. Pls help me out. Also is there any other alternative way if there are no inbuilt functions as such. Thanks in advance.

+3  A: 

Using VB.NET you can do this with System.IO.Directory.GetFiles. There is a version that takes a pattern to search for and let's you search subdirectories as well.

For Each dir In Directory.GetFiles("path","*.zip",SearchOptions.AllDirectories)
    Console.WriteLine( _
        "{0,-25} {1,25}", dir.FullName, dir.LastWriteTime)
Next dir
tvanfosson
+1  A: 

In VB6 you want to use FileSystemObject of the Microsoft Scripting Runtime. You can access to the scripting runtime by setting a reference to it.

The .NET framework has a similar but more capable set of file/directory handling object in the System.IO namespace

The following an example of how to use FileSystemObject in VB6.

Dim FSO As FileSystemObject
Dim Folder As Folder
Dim SubFolder As Folder
Dim File As File

Set FSO = New FileSystemObject

Set Folder = FSO.GetFolder("C:\")
For Each File In Folder.Files
    Debug.Print File.Name
Next File

For Each SubFolder In Folder.SubFolders
    Debug.Print SubFolder.Name
Next SubFolder

Set Folder = Nothing
Set SubFolder = Nothing
Set FSO = Nothing
RS Conley
+2  A: 

For VB6.0 I would make use of the FileSystemObject and a small recursive function.

Sub test()
  Dim fso As New Scripting.FileSystemObject
  Dim files As New Collection
  Dim file As Scripting.file

  GetFilesRecursive fso.GetFolder("C:\YourFolder"), "zip", files, fso

  For Each file In files
    Debug.Print file.Name
  Next file
End Sub

Sub GetFilesRecursive(f As Scripting.Folder, filter As String, c As Collection, fso As Scripting.FileSystemObject)
  Dim sf As Scripting.Folder
  Dim file As Scripting.file

  For Each file In f.Files
    If InStr(1, fso.GetExtensionName(file.Name), filter, vbTextCompare) = 1 Then
      c.Add file, file.path
    End If
  Next file

  For Each sf In f.SubFolders
    GetFilesRecursive sf, filter, c, fso
  Next sf
End Sub

This will not be lightning fast, though. Maximum performance can only be gained by directly using Win32 API functions like FindFirstFile and FindNextFile.

Tomalak
+1  A: 

VB6 has nothing that will do this in one click, but it's straightforward to get a list of all ZIP files (for example).

The FSO is not recommended for a couple of reasons: one, it adds a dependency, and two, it depends on scripting, which may be disabled per policy.

Anyway, here's a bare minimum example you can flesh out:

 Dim Fils() As String
 Dim Counter As Long
 Dim CurrentFile As String

 Redim Fils(0 To 999) As String

 CurrentFile = Dir$(yourpath & "*.zip")

 Do While LenB(CurrentFile)
   Fils(Counter) = CurrentFile
   Counter = Counter + 1
   CurrentFile = Dir$()
 Loop

Of course, you want to add limit checking, etc, and redim as needed, but this is the basic idea.

Jim Mack
+1  A: 

Yet another stab at it:

Private Sub EnumSubfiles(ByVal ParentFolder As String, _
                         ByVal FilePattern As String)
    'Report back via Report subroutine.
    Dim SubFolders As Collection
    Dim Name As String
    Dim FQName As String
    Dim SubFolder As Variant

    Set SubFolders = New Collection
    Name = Dir$(ParentFolder & "\*", vbNormal Or vbDirectory)
    Do Until Len(Name) = 0
        FQName = ParentFolder & "\" & Name
        If (GetAttr(FQName) And vbDirectory) = vbDirectory Then
            If Not (Name = "." Or Name = "..") Then
                SubFolders.Add FQName
            End If
        Else
            If Name Like FilePattern Then Report FQName
        End If
        Name = Dir$()
    Loop
    For Each SubFolder In SubFolders
        EnumSubfiles SubFolder, FilePattern
    Next
End Sub
Bob
+1  A: 

Just drop-in a CDirDrill class that does it all for you, in full native VB6. Another excellent solution from Karl Peterson :)

BTW I also recommend avoiding the FileSystemObject. I've had bugs because some customer has managed to muck up scrrun.dll on their PC. Eliminate dependencies unless they're really helping you a lot.

MarkJ