Results 1 to 9 of 9

Thread: Help with retrieving file names.

  1. #1
    Guest

    Question

    I am having a hard time finding some information on how to get file names into strings.

    What I want is to search a folder on my hard drive (ie...C:\programs\training\) to find all the files of a certain extension (ie... *.cor). As the files are found, I want to set the file name to a string variable array and count the amount of files with that extension.

    Can anyone help me?

  2. #2
    Fanatic Member RealisticGraphics's Avatar
    Join Date
    Jul 1999
    Location
    Arkansas
    Posts
    655

    Thumbs up

    Here is a simple function that would extract the file name & extension from the file path that I use often:

    Code:
    Dim fName
    dim fPath as string
    fName = Split(fPath, "\")
    List1.AddItem fName(UBound(fName))
    Basically this takes the file path (fPath) and creates an array by splitting fPath at every backslash. Then just pull the upper bound (the last item in the array) and gives you to this name & extension.

  3. #3
    Guest
    Thanks, but will this go through all the files in the folder?

  4. #4
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    What about plain old DIR

  5. #5
    Guest
    Care to explain a little more Frans C? I am fairly inexperienced.

    I have found a work around, I am using a file list box. As long as I set the pattern and path, I get what I want.


  6. #6
    New Member
    Join Date
    May 2000
    Posts
    6
    May this will help :

    Code:
    Sub Main
       Dim result as Long
       result = extCounter("C:\Programs\Training\*.COR")
       Msgbox "Count result : " & str(result)
    End Sub
    
    '-- File with specific Extension counter --
    '-- Pattern will accept wildcards ( ? and * ) --
    Function extCounter(Pattern as String) as long
       Dim ctr as Long
       Dim itm as String
    
       ctr = 0                           '-- Initialize counter --
       
       itm = dir(Pattern, vbNormal)      '-- Find first occurence --
       Do Until itm = vbNullString       
          ctr = ctr + 1                  '-- Increment counter --
          itm = Dir()                    '-- Find next occurence --
       Loop
    
       extCounter = ctr                  '-- Return count --
    End Function
    I'm sure this will go thru all the files in the folder.
    Shinned
    Email : [email protected]
    Home : http://www.geocities.com/shinned

    VB Ver : 6.0

  7. #7
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339

    Lightbulb

    Here is another way:

    Code:
    Dim fs, fldr
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set fldr = fs.GetFolder("c:\")  'gets the folder you want to search
    Dim fle As File
    For Each fle In fldr.Files
        If Right(fle.Name, 3) = "bat" Then 'checks to see if it is the extension you want
            List1.AddItem fle.Name 'adds it to a list box
            'here is where you put what you want it to do when it finds a file of that type
        End If
    Next
    [Edited by Edneeis on 05-30-2000 at 02:25 AM]

  8. #8
    Guest
    Thanks a lot Edneeis, your way is exactly what I was looking for. I appreciate everyones help.

  9. #9
    Fanatic Member
    Join Date
    Mar 2000
    Location
    That posh bit of England known as Buckinghamshire
    Posts
    658
    Much though i personally like the file scripting object, the Dir function is remarkably easy to use.

    Code:
    Dim myFile As String
    
    myFile = Dir$("c:\*.Bat")
        
    Do While myFile <> ""
      List1.AddItem myFile
      myFile = Dir
    Loop
    You don't need the scripting runtime library either.

    Iain, thats with an i by the way!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width