Hello all and a big thanks for reading,
I am wanting to open a folder and prosess all the files in it. The first two
lines of code works, but it reads just 1 file at a time and I have to enter
in the name of the file. Is there a way to have it open a folder then I can
pull the first file in and open it for input? I tried this (last line #5) to
open a folder but get errors.
Open "C:\tote\T000645925837-DVD5-BATF.dat1" For Input As #1
Open "c:\tote\new\T000645925837-DVD5-BATF.dat1" For Output As #2
Open "C:\tote" For Input As #5
Mark - 15 Jul 2005 19:50 GMT
Think I miss word this. Open will not work. What command do I used to have
vb read a folders content?
> Hello all and a big thanks for reading,
>
[quoted text clipped - 8 lines]
>
> Open "C:\tote" For Input As #5
Dirk Goldgar - 15 Jul 2005 21:24 GMT
> Think I miss word this. Open will not work. What command do I used
> to have vb read a folders content?
[quoted text clipped - 11 lines]
>>
>> Open "C:\tote" For Input As #5
For what you want, the Dir() function is simpler and more efficient than
the FileSearch object. Along these lines:
Dim strFolder As String
Dim strFile As String
Dim intFileNo As Integer
strFolder = "C:\tote\"
strFile = Dir(strFolder & "*.*")
Do Until Len(strFile) = 0
' Open and process file ...
intFileNo = FreeFile()
Open strFolder & strFile for Input As #intFileNo
' do something with the file ...
Close intFileNo
' Get next file from folder, if any.
strFile = Dir()
Loop

Signature
Dirk Goldgar, MS Access MVP
www.datagnostics.com
(please reply to the newsgroup)
Brian - 15 Jul 2005 19:59 GMT
Here is an clip from the Access VBA help on the FileName property. It shows
how to loop through a folder, looking at a filtered list of the files (in
this example, cmd*) one at a time.
Set fs = Application.FileSearch
With fs
.LookIn = "C:\My Documents"
.FileName = "cmd*.*"
If .Execute > 0 Then
MsgBox "There were " & .FoundFiles.Count & _
" file(s) found."
For i = 1 To .FoundFiles.Count
MsgBox .FoundFiles(i)
Next i
Else
MsgBox "There were no files found."
End If
End With
> Hello all and a big thanks for reading,
>
[quoted text clipped - 8 lines]
>
> Open "C:\tote" For Input As #5