Using the "Open" Command - Need fast

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
Think I miss word this. Open will not work. What command do I used to have
vb read a folders content?
 
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
 
Mark said:
Think I miss word this. Open will not work. What command do I used
to have vb read a folders content?

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
 
Back
Top