hi guys, i want to put all files under a folder into array, how do i do that???
for example
i don't know if its something like this or not, but please help me,,,thanxCode:dim array(0)
for i to 0 to FileCount
array(i) = folder.file(i)
next i
Printable View
hi guys, i want to put all files under a folder into array, how do i do that???
for example
i don't know if its something like this or not, but please help me,,,thanxCode:dim array(0)
for i to 0 to FileCount
array(i) = folder.file(i)
next i
Here's one way of doing it:
Good luck!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)
Thanx alot man
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
thanx
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.Quote:
Originally posted by HeSaidJoe
'why redim as 100....what if there were 1000 files
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.
ok guys, a quick question, Why do we use ReDim??? and when??? and How???
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.
ok thanx alot man, but give me a simple example on how to use it with explaining please...
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.
thanx man, i havn't noticed that,,,