Results 1 to 11 of 11

Thread: easy question,help me please quickly

  1. #1

    Thread Starter
    Addicted Member Mih_Flyer's Avatar
    Join Date
    Mar 2000
    Location
    some place there
    Posts
    241
    hi guys, i want to put all files under a folder into array, how do i do that???
    for example
    Code:
    dim array(0)
    for i to 0 to FileCount
    array(i) = folder.file(i)
    next i
    i don't know if its something like this or not, but please help me,,,thanx

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Here's one way of doing it:
    Code:
    Dim sFiles() As String
    Dim sFileName As String
    Const PATH = "C:\MyFolder" 'change this to whatever
    Dim iFileCount As Integer
    
    Redim sFiles(100)
    sFileName = Dir(PATH & "\*.*")
    Do While Len(sFileName)
        sFiles(iFileCount) = sFileName
        iFileCount = iFileCount + 1
        If iFileCount > UBound(sFiles) Then
            Redim Preserve sFiles(iFileCount + 100)
        End If
        sFileName = Dir
    Loop
    Redim Preserve sFiles(iFileCount - 1)
    Good luck!

  3. #3

    Thread Starter
    Addicted Member Mih_Flyer's Avatar
    Join Date
    Mar 2000
    Location
    some place there
    Posts
    241
    Thanx alot man

  4. #4
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    Option Explicit
    'why redim as 100....what if there were 1000 files
    Code:
    Private Sub Command1_Click()
    
    'access all files within a folder
    
        Dim stFile As String
        Dim stDir As String
        Dim myArr()
        Dim i As Integer
        
        stDir = "C:\Dirtbagdog\"
        stFile = Dir$(stDir & "*.*")
        
        Do While stFile <> ""
         
           ReDim Preserve myArr(i)
           myArr(i) = stFile
           i = i + 1
           stFile = Dir
        Loop
    'if you want to test it add a listbox to your form
    'and run this code as well...
    
        For i = LBound(myArr) To UBound(myArr)
           List1.AddItem myArr(i)
        Next i
    
    End Sub
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  5. #5

    Thread Starter
    Addicted Member Mih_Flyer's Avatar
    Join Date
    Mar 2000
    Location
    some place there
    Posts
    241
    thanx

  6. #6
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: <?>

    Originally posted by HeSaidJoe
    'why redim as 100....what if there were 1000 files
    Well if you have a closer look at the sample code I submitted in my previous post you will notice that I redim by the hundreds.
    When the file counter (iFileCount) is higher then the UBound of the sFiles array I redim it again to be 200 (or 300 or 400 and so on).
    This is because of the fact that it takes a lot or time to redim an array (and especially when you use the Preserve keyword).
    In my code I only redim the array if it contains more then 100 files and I redim it again if it contains more then 200 and so on.
    I then make a final redim (at the end of the code) to the array so it's not larger then it has to be.

    So if the folder contains 1000 files I redim the array 10 times but in your code you do the redim 1000 times.

  7. #7

    Thread Starter
    Addicted Member Mih_Flyer's Avatar
    Join Date
    Mar 2000
    Location
    some place there
    Posts
    241

    Question ReDim??

    ok guys, a quick question, Why do we use ReDim??? and when??? and How???

  8. #8
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    You use ReDim to alter the array size. You don't know how many files there is in a specific folder so you don't know how large the array should be.

  9. #9

    Thread Starter
    Addicted Member Mih_Flyer's Avatar
    Join Date
    Mar 2000
    Location
    some place there
    Posts
    241

    Thanx man

    ok thanx alot man, but give me a simple example on how to use it with explaining please...

  10. #10
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    You allready have a simple example. The first post I made was an example to add all files in a directory in to an array.
    This array changes in size if necassary.

  11. #11

    Thread Starter
    Addicted Member Mih_Flyer's Avatar
    Join Date
    Mar 2000
    Location
    some place there
    Posts
    241
    thanx man, i havn't noticed that,,,

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