Results 1 to 18 of 18

Thread: How many files?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2001
    Location
    Lisbon, Portugal
    Posts
    69

    Question How many files?

    Hi,

    How can I know how many files there are in a directory?
    Rita Jaques

  2. #2
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,539
    you can use the FileSystemObject
    or you can loop through a dir and put thenumber of loops in a counter using DIR function

  3. #3
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    *peets looking nerveously around... is Chris or Filburt here??? mmm.. no... .* u can use the FSO (File System Object)


    VB Code:
    1. 'Add a reference to the 'Microsoft Scripting Runtime' (scrrun.dll)
    2. Option Explicit
    3. Dim fso As New FileSystemObject
    4. Dim fldr As Folder
    5.  
    6. Private Sub Command1_Click()
    7.   Set fldr = fso.GetFolder("c:\")
    8.   MsgBox fldr.Files.Count
    9. End Sub
    -= a peet post =-

  4. #4
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    *peets looking nerveously around... is Chris or Filburt here??? mmm.. no... .* u can use the FSO (File System Object)


    VB Code:
    1. 'Add a reference to the 'Microsoft Scripting Runtime' (scrrun.dll)
    2. Option Explicit
    3. Dim fso As New FileSystemObject
    4. Dim fldr As Folder
    5.  
    6. Private Sub Command1_Click()
    7.   Set fldr = fso.GetFolder("c:\")
    8.   MsgBox fldr.Files.Count
    9. End Sub
    -= a peet post =-

  5. #5
    Fanatic Member SeaHag's Avatar
    Join Date
    Jul 2001
    Location
    Lake Huron
    Posts
    901
    see if this helps.


    VB Code:
    1. Public Sub PopulateArray(DirToSearch As String)
    2.    
    3.     Dim current As String
    4.     Dim i As Integer
    5.     Dim j As Integer
    6.  i = 0
    7.  ReDim Ary(i)
    8.         current = Dir(DirToSearch)
    9.         Ary(i) = current
    10.        
    11.         While current <> ""
    12.             i = i + 1
    13.             ReDim Preserve Ary(i)
    14.             current = Dir
    15.             Ary(i) = current
    16.         Wend
    17.         Debug.Print i
    18. End Sub


    Seahag

  6. #6
    jim mcnamara
    Guest
    One way:

    Code:
    Function filecnt(strDir as String) as Integer
           dim i,tmp, loc
           i = 0
           tmp=dir(strDir & "\*.*")
           do While tmp >""
               i = i+1
               tmp = dir
            Loop
        
            filecnt = i
    End Function
    usage:
    filecount = filecnt("MyDir")

  7. #7
    Fanatic Member SeaHag's Avatar
    Join Date
    Jul 2001
    Location
    Lake Huron
    Posts
    901

    POUNCE!!


  8. #8

    Thread Starter
    Lively Member
    Join Date
    Mar 2001
    Location
    Lisbon, Portugal
    Posts
    69

    Smile

    Thanks a lot.

    I'm going to try with the fso, but I didn't know

    fso.files.count

    When I write the dot, it doesn't give the Files
    Rita Jaques

  9. #9
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    u have to have a folder ...

    VB Code:
    1. Set fldr = fso.GetFolder("c:\")
    2.   MsgBox fldr.Files.Count
    -= a peet post =-

  10. #10
    Fanatic Member SeaHag's Avatar
    Join Date
    Jul 2001
    Location
    Lake Huron
    Posts
    901
    eeek.. references

  11. #11
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    OR WITHOUT USING THE FSO ...
    Shove a filelistbox on your form (hide it if you need to), then
    • set the folder to look in
    • Return the file count
    Code:
    file1.path = "C:\myFolder\"
    Msgbox file1.listcount & " files found"
    Done - in 2 lines of code. DID I MENTION IT DOESN'T USE THE FSO ?

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  12. #12
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    Originally posted by alex_read
    OR WITHOUT USING THE FSO ...
    Shove a filelistbox on your form (hide it if you need to), then
    • set the folder to look in
    • Return the file count
    Code:
    file1.path = "C:\myFolder\"
    Msgbox file1.listcount & " files found"
    Done - in 2 lines of code. DID I MENTION IT DOESN'T USE THE FSO ?
    LOL and LAC (Load And Clear ) Chris and Filburt will be jumping up and down in excitement
    -= a peet post =-

  13. #13
    Frenzied Member
    Join Date
    Sep 1999
    Location
    Phoenix, az
    Posts
    1,517
    Lol

  14. #14
    Fanatic Member SeaHag's Avatar
    Join Date
    Jul 2001
    Location
    Lake Huron
    Posts
    901

    Why do you not have to add a reference for this

    VB Code:
    1. Function Test(SeverFolder As String, DestFolder As String)
    2. Dim oFileSystem
    3. Set oFileSystem = CreateObject("Scripting.FileSystemObject")
    4. oFileSystem.CopyFolder SeverFolder, DestFolder, True
    5. Set oFileSystem = Nothing
    6. End Function

    ?

  15. #15
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    Its called late binding.
    instead of adding a ref. u use createobject.

    when u do that, u don't need a ref.
    -= a peet post =-

  16. #16
    Fanatic Member SeaHag's Avatar
    Join Date
    Jul 2001
    Location
    Lake Huron
    Posts
    901
    I am confuse. Why do people hate FSO? they say its Bulky?


    Any hints why?

  17. #17
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    People don't hate it , sheeps and turtles do

    I do agree with them though. If u want to just count the number of files in a folder, u don't want to add yet another dll to u'r setup, making it even bigger

    Normally I post FSO samples just to annoy Filburt and Chris but hey I'm an evil, evil, eeeeevil person
    -= a peet post =-

  18. #18
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Yeah, I was just playing too, there's nothing really wrong with it, it just creates an extra reference in your code making your app a bit bigger & consuming.
    It's great if there's a load of work involved - i.e. looping through 5 folders & pulling the files back from each, but just one off procedures like grabbing the amount of files from a folder - normal vb can do this

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

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