Separate this function?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Separate this function?

  1. #1
    Join Date
    Feb 2009
    Posts
    44

    Separate this function?

    Code:
    Private Sub Form_Load()
    MsgBox GetFiles("c:\")
    End Sub
    Function GetFiles(ByVal MyPath As String) As String
    Dim sFiles As String
       MyName = Dir(MyPath)
       Do While MyName <> ""
            Select Case UCase(Right(MyName, 3))
                Case "TXT", "CSV", "CHM"
                    sFiles = sFiles & MyName & vbCr
                Case Else
            End Select
            MyName = Dir()   '
       Loop
       GetFiles = sFiles & vbCrLf
    End Function
    Right now if i msgbox Getfiles()then it msgboxs all the names under one msgbox instead of multiple msgboxs.

  2. #2
    Join Date
    Aug 2004
    Location
    Orange, California
    Posts
    1,263
    This should work!

    Code:
    Private Sub Form_Load()
       Dim I As Long
       Dim Files() As String
       
       Files = Split(GetFiles("c:\"), vbCr)
       For I = Lbound(Files) to UBound(Files) - 1
          MsgBox Files(I)
       Next I
    
       Erase Files
    End Sub
    
    Function GetFiles(ByVal MyPath As String) As String
       Dim sFiles As String
       MyName = Dir(MyPath)
       Do While MyName <> ""
          Select Case UCase(Right(MyName, 3))
          Case "TXT", "CSV", "CHM"
             sFiles = sFiles & MyName & vbCr
          End Select
          MyName = Dir()   '
       Loop
       GetFiles = sFiles & vbCrLf
    End Function

  3. #3
    Join Date
    Feb 2009
    Posts
    44
    instead of doing it like is there anyway to split it within the GetFiles() func?

  4. #4
    Join Date
    Aug 2004
    Location
    Orange, California
    Posts
    1,263
    Sure!
    Code:
    Private Sub Form_Load()
       Dim I As Long
       Dim Files() As String
       
       Files = GetFiles("c:\")
       For I = LBound(Files) To UBound(Files)
          MsgBox Files(I)
       Next I
       Erase Files
    End Sub
    
    Function GetFiles(ByVal MyPath As String) As String()
       Dim MyName As String
       Dim Files() As String
       Dim fc As Long
       
       MyName = Dir(MyPath)
       Do While MyName <> ""
          Select Case UCase(Right(MyName, 3))
          Case "TXT", "CSV", "CHM"
             fc = fc + 1
             ReDim Preserve Files(1 To fc)
             Files(fc) = MyName
          End Select
          MyName = Dir()
       Loop
       GetFiles = Files()
       Erase Files
    End Function

  5. #5
    Join Date
    Aug 2004
    Location
    Orange, California
    Posts
    1,263
    Quote Originally Posted by WeightOver2001 View Post
    it works alright when the path is just c:\.

    but i get a subscript range error if i change it,
    Files = GetFiles("c:\" + "Programming Files")
    That is because you selected a folder that did not have any files that matched your criteria. Here is a version that looks for that:
    Code:
    Private Sub Form_Load()
       Dim I As Long
       Dim Files() As String
       
       Files = GetFiles("c:\programming files")
       For I = LBound(Files) To UBound(Files)
          MsgBox Files(I)
       Next I
       Erase Files
    End Sub
    
    Function GetFiles(ByVal MyPath As String) As String()
       Dim MyName As String
       Dim Files() As String
       Dim fc As Long
       
       MyName = Dir(MyPath)
       Do While MyName <> ""
          Select Case UCase(Right(MyName, 3))
          Case "TXT", "CSV", "CHM"
             fc = fc + 1
             ReDim Preserve Files(1 To fc)
             Files(fc) = MyName
          End Select
          MyName = Dir()
       Loop
       If fc = 0 Then Files = Split("")
       GetFiles = Files()
       Erase Files
    End Function

Similar Threads

  1. Replies: 8
    Last Post: 03-08-2003, 06:25 PM
  2. calling ping thru vb
    By Michael Shutt in forum VB Classic
    Replies: 6
    Last Post: 04-05-2001, 02:25 AM
  3. verify local admin
    By Patrick Comeau in forum VB Classic
    Replies: 6
    Last Post: 03-22-2001, 10:50 PM
  4. How do I detect an FTP timeout?
    By Julian Milano in forum VB Classic
    Replies: 2
    Last Post: 08-11-2000, 12:11 PM
  5. Trying to print a PDF File from VB
    By Kunal Sharma in forum VB Classic
    Replies: 2
    Last Post: 04-25-2000, 03:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center