How to go thru all files

namliam

The Mailman - AWF VIP
Local time
Today, 08:16
Joined
Aug 11, 2003
Messages
11,695
Title is not all that but here goes.
I want to work thru all files in a particular folder, so...
Code:
Sub t2Process(FilterNR As String, FreeText As String)
    Dim myFile As String, myfile2 As String, aNumber As String
    myFile = Dir(t2ImportFolder & "*." & FileType)
    Do While myFile <> vbNullString
        myFile = Dir()
    Loop
End Sub
That works, but why then if I put stuff inbetween to handle the file(s) in the folder, i only get 1! file? While there are about 90 there....

I have a silly workaround in place but am wondering HOW WHEN WHY WHAT is going on? Does anyone know?

Greetz
 
The following code provides a function, fileList, which returns an array containing a list of files returned by the file path you specify.

Paste the following code into a new module and save the module as modfileList:
Code:
Private Declare Function GetExitCodeProcess Lib "kernel32" _
    (ByVal hProcess As Long, lpExitCode As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" _
    (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
    ByVal dwProcessId As Long) As Long

Private Const STILL_ACTIVE = &H103
Private Const PROCESS_QUERY_INFORMATION = &H400

Public Function fileList(filePath As String) As Variant

Dim fileIndex As Long
Dim fileArray() As String
Dim fileBuffer As String

Dim bInheritHandle As Long
Dim dwProcessId As Long
Dim lpExitCode As Long
Dim lpRetVal As Long

If Len(Dir(Environ$("TEMP") & "~ilelist.tmp", vbNormal)) > 0 Then _
    Kill Environ$("TEMP") & "~ilelist.tmp"

bInheritHandle = Shell(Environ$("COMSPEC") & " /c dir /b /s """ & filePath _
    & """ > """ & Environ$("TEMP") & "~ilelist.tmp""")
dwProcessId = OpenProcess(PROCESS_QUERY_INFORMATION, False, bInheritHandle)
Do
    lpRetVal = GetExitCodeProcess(dwProcessId, lpExitCode)
    DoEvents
Loop While lpExitCode = STILL_ACTIVE And lpRetVal > 0
If lpRetVal = 0 Then Debug.Print Err.LastDllError

Open Environ$("TEMP") & "~ilelist.tmp" For Input Shared As #1
    fileBuffer = Input(LOF(1), #1)
Close #1
Kill Environ$("TEMP") & "~ilelist.tmp"

Do While InStr(1, fileBuffer, vbCrLf, vbBinaryCompare) > 0
    ReDim Preserve fileArray(fileIndex)
    fileArray(fileIndex) = Left(fileBuffer, _
        InStr(1, fileBuffer, vbCrLf, vbBinaryCompare) - 1)
    fileBuffer = Mid(fileBuffer, _
        InStr(1, fileBuffer, vbCrLf, vbBinaryCompare) + 2)
    fileIndex = fileIndex + 1
Loop

If fileIndex Then fileList = fileArray

End Function

Then try modifying your code thus:
Code:
Sub t2Process(FilterNR As String, FreeText As String)
    Dim myFiles As Variant, myFileCount As Long
    myFile = fileList(t2ImportFolder & "*." & FileType)
    For myFileCount = 0 To Ubound(myFiles)
        Debug.Print myFiles(myFileCount)
    Next
End Sub
...to see how it works.
 
This also is but a workaround... i was hoping for a more constructive answer to the reason of this ....

Thanx for the response tho... :)

Greetz
 
Okay, try this answer then. The reason for the workaround that I supplied is that that is how the Dir() command works! So, you HAVE to use a workaround!
 
you could always go directly to use of the FSO (File System Object).
 
Just a little explanation of Dir()

namliam said:
This also is but a workaround... i was hoping for a more constructive answer to the reason of this ....

Just to specify, the Dir() command return the first file in the dir, not the entire list of files. You can call it multiple time to get different files until it returns an empty String.

VBA_Help said:
Dir returns the first file name that matches pathname. To get any additional file names that match pathname, call Dir again with no arguments. When no more file names match, Dir returns a zero-length string ("").
Once a zero-length string is returned, you must specify pathname in subsequent calls or an error occurs. You can change to a new pathname without retrieving all of the file names that match the current pathname. However, you can't call the Dir function recursively.
Calling Dir with the vbDirectory attribute does not continually return subdirectories.
 
Talk about bringing one back from the dead... Allmost 2 years old thread comes back to live...

I know how the Dir thing works, but for some reason at that time I do the looping thing and I find the first file, it got imported and manipulated into usefull info. Then the next Dir would return "". While files are still there.

Anyway its mute now...
 

Users who are viewing this thread

Back
Top Bottom