[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 318
  • Last Modified:

Get default drive and list files and files in subfolders

Hi,

Without using a DriveBox or FileList box, and also without using Microsoft's new filesysinfo dll (or whatever it's called, which ships with VB6) I want to be able to ascertain the users default drive, and get the name of each *.bmp file in that drive and in all subfolders.

Many thanks,
Dave
0
dgb001
Asked:
dgb001
  • 5
  • 3
  • 2
1 Solution
 
SiM99Commented:
use the Dir$() function... look it up in the help and it will show you what to do with it.

Sorry for the lack of input (no example) but the computer i'm on doesn't have VB installed on it.

The idea is to do a loop, using Dir() to get a filename, checking the return type to see ie it's a DIR or FILE, then if it's a file, do

if (right(filename, 4) = ".bmp") then

again, sorry that i cannot give an example, but i could do if you want me to, just say so :)
0
 
dgb001Author Commented:
OK, thx, I'll take a look and let you know.

Cheers,
Dave
0
 
VbmasterCommented:
Here's a function you can use.. you call it using code like


  Dim File As New clsFile
  Dim x() As String
  Dim a As Integer
 
  Call File.ListFiles("C:\", x, True, True, "*.bmp")
  Debug.Print ""
  Debug.Print Now
  For a = 1 To UBound(x)
    Debug.Print x(a)
  Next


And the function is..

Function ListFiles(ByVal Path As String, ByRef av_Array() As String, Recursive As Boolean, Optional IncludePaths As Boolean = True, Optional Prefix As String = "*.*", Optional ByRef x As Double = 0, Optional StartDir As Boolean = True)

  Dim MyName  As String
  Dim MyDir() As String
  Dim MyDirNr As Integer
  Dim a       As Integer
 
  MyDirNr = 0
  If StartDir Then
    x = 0
    Path = IIf(Right$(Path, 1) = "\", Path, Path & "\")
    Prefix = UCase$(Prefix)
  End If
 
  On Error GoTo Errorhandler
 
  MyName = Dir$(Path & "*.*", vbDirectory + vbArchive + vbHidden + vbReadOnly + vbSystem)
  Do While (Len(MyName) > 0)
    If (MyName <> ".") And (MyName <> "..") Then
      If (GetAttr(Path & MyName) And vbDirectory) = vbDirectory Then
        ReDim Preserve MyDir(MyDirNr + 1)
        MyDirNr = MyDirNr + 1
        MyDir(MyDirNr) = MyName
      ElseIf (UCase$(MyName) Like Prefix) Then
        If (x Mod 10 = 0) Then ReDim Preserve av_Array(x + 10)
        x = x + 1
        If IncludePaths Then
          av_Array(x) = Path & MyName
        Else
          av_Array(x) = MyName
        End If
      End If
    End If
    MyName = Dir$
  Loop
 
  If Recursive Then
    For a = 1 To MyDirNr
      If Not ListFiles(Path & MyDir(a) & "\", av_Array(), True, IncludePaths, Prefix, x, False) Then GoTo Errorhandler
    Next
  End If
  If StartDir Then ReDim Preserve av_Array(x)
  ListFiles = True
  Exit Function
 
Errorhandler:
  ListFiles = False

End Function

0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
dgb001Author Commented:
Ok, let me have a read and play with it and I'll get back to you.

Many thx,
Dave
0
 
dgb001Author Commented:
Hi,

Just a couple of things:

a) I seem to get a "User-defined type not defined" on the first line when it tries to Dim clsFile

b) I notice that you defaulted it to use C:\ - is there a way to find out if that is indeed the default drive ?

Many thx,
Dave
0
 
SiM99Commented:
you need the class file called clsFile
0
 
VbmasterCommented:
Oops remove the line..

  Dim File As New clsFile

and change the function call from..

  Call File.ListFiles("C:\", x, True, True, "*.bmp")

to..

  Call ListFiles("C:\", x, True, True, "*.bmp")
0
 
VbmasterCommented:
To get the current drive you can use the CurDir variable, where the current drive is Left$(CurDir, 3)
0
 
dgb001Author Commented:
Okey dokey - let me play around and I'll get back to you.

Thx for your help,
Dave
0
 
dgb001Author Commented:
Thx,
Dave
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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