ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Opening many files sequentially (https://www.excelbanter.com/excel-programming/272642-opening-many-files-sequentially.html)

Robin Clay[_2_]

Opening many files sequentially
 
Greetings !

In a certain Folder I have a dozen text files all ending
with .ali. These files all have an identical format, but
different values.

I occassionally need to change one particular field in all
the files.

How do I perform the "FOR" loop to address each file in
turn ?

What I did as a stop-gap was to open a DOS window in that
folder, enter

DIR *.ali Junk

and then cut and paste the names from "Junk" into the
module code, each on a separate line.

I then put

myFile(x) = "

before each, and then changed the x to consecutive
numbers.

Then I put

For N = 1 to 12
ThisFile = myFile(N)
etc....

Tedious, I know, but it worked.

But "there must be a better way" .... ?

Bob Kilmer

Opening many files sequentially
 
Robin,
BTW, you don't have to collect the file names into a collection. I just
included that as an example in case you want to keep a reference to the
files around in code. I used a collection because, unlike an array, it does
not require that I know ahead of time how many files there will be to size
it and it avoids repeatedly resizing an array to accomodate additional file
names.

Bob Kilmer

"Bob Kilmer" wrote in message
...
Example:
Option Explicit

Sub Main()
Dim c As Collection
Set c = New Collection

Dim f As String
f = Dir("X:\ACertainFolder\*.ali")
Do While Len(f) 0
c.Add f
f = Dir()
Loop

Dim v As Variant
For Each v In c
Debug.Print CStr(v)
Next v
Set c = Nothing
End Sub


Also, in DOS, the Dir switch /b (as in "bare") is good for listing files
for batch applications. It omits the time, date, size, etc. and just lists
the files and paths.

Dir *.ali /b

Dir *.ali /b/s (to include subdirectories)

"Robin Clay" wrote in message
...
Greetings !

In a certain Folder I have a dozen text files all ending
with .ali. These files all have an identical format, but
different values.

I occassionally need to change one particular field in all
the files.

How do I perform the "FOR" loop to address each file in
turn ?

What I did as a stop-gap was to open a DOS window in that
folder, enter

DIR *.ali Junk

and then cut and paste the names from "Junk" into the
module code, each on a separate line.

I then put

myFile(x) = "

before each, and then changed the x to consecutive
numbers.

Then I put

For N = 1 to 12
ThisFile = myFile(N)
etc....

Tedious, I know, but it worked.

But "there must be a better way" .... ?






Robin Clay[_2_]

Opening many files sequentially
 
Keith Willshaw responded, 7/25/2003 5:48:10 AM
for which, many thanks - it seem just waht I want !

But I'm afraid I have a query -

... use the DIR function
Dim myfile as String, myfilecount as Integer
Dim myfilelist(1000) As String
myfilecount =0
myfile = Dir(mydirectory & "\*.ali", vbNormal) _
' gets first entry
Do While myfile < ""
myfilelist(myfilecount) = _
mydirlist(i) & "\" & myfile

~~~~~~~~~~~~
What is mydirlist ?
What is i ?
Should they be declared ?

myfilecount = myfilecount + 1
myfile = Dir ' Get Next Entry
Loop



Keith Willshaw

Opening many files sequentially
 

"Robin Clay" wrote in message
...
Keith Willshaw responded, 7/25/2003 5:48:10 AM
for which, many thanks - it seem just waht I want !

But I'm afraid I have a query -

... use the DIR function
Dim myfile as String, myfilecount as Integer
Dim myfilelist(1000) As String
myfilecount =0
myfile = Dir(mydirectory & "\*.ali", vbNormal) _
' gets first entry
Do While myfile < ""
myfilelist(myfilecount) = _
mydirlist(i) & "\" & myfile

~~~~~~~~~~~~
What is mydirlist ?


A typo, I snipped this excerpt from a piece of code I use, essentially
I iterate through multiple subdirectories rather than just 1
directory. Just replace it with your directory name

What is i ?
Should they be declared ?


All variables should be declared

Keith




All times are GMT +1. The time now is 02:58 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com