
help with a file array problem
Quote:
> using the following VBS (thanks Tom) to do a simple
> search and replace with one file...
> File = "D:\FILE1.TXT"
> const ForReading = 1, ForWriting = 2
> Set oFS = CreateObject("Scripting.FileSystemObject")
> Set oInFile = oFS.openTextFile(File, ForReading, True)
> sText = oInFile.ReadAll
> oInFile.Close
> Set oOutFile = oFS.openTextFile(File, ForWriting, True)
> oOutFile.Write replace(sText, "2000 ", "00 ")
> oOutFile.Close
> i am trying to put the steps that it executes inside
> of an array, so that it will handle: file1.txt, file2.txt,
> and file3.txt...
> however, i have not been successful in getting it to work.
> i have looked at Clarence's site and two others and cannot
> find anything on multiple file arrays.
> the syntax i'm using is something like
> dim file, files
> dim file1, file2, file3
> file1 = "D:\file1.txt"
> file2 = "D:\file2.txt"
> file3 = "D:\file3.txt"
> for each file in files
> <search and replace code>
> next file
> i've obviously reached a point where something i don't know
> or realise is holding me back.
> can anyone pls help?
> many thanks...
> Hank
Yup, you're definitely confused. The FOR construct you have requires
the 'files' variable in your example to be a 'collection' of files,
which it is not (as written). And a collection is not an array.
Assuming the file names are just examples of arbitrary names (i.e. that
can't be indexed), you could do something like this ...
files = Array("D:\file1.txt")
files = Array(files, "D:\file2.txt")
files = Array(files, "D:\file3.txt")
Set oFS = CreateObject("Scripting.FileSystemObject")
for i = 0 to UBound(files)
file = files(i)
if oFS.FileExists(file) then
<search and replace code>
End if
next
If the file names do in fact differ only by the index number, the
following would also work ...
Upper = 3
Set oFS = CreateObject("Scripting.FileSystemObject")
for i = 1 to Upper
file = "file" & i & ".txt"
if oFS.FileExists(file) then
<search and replace code>
End if
next
If you want to process a number of files in a particular folder, then
you want to create a collection of files to process similar to the
example you posted ...
Set oFS = CreateObject("Scripting.FileSystemObject")
Set files = oFS.GetFolder("C:\Some\Folder).Files
for each file in files
<search and replace code>
next
The FilesExist() test isn't needed in this case, because only existing
files can be part of the 'files' collection.
Tom Lavedas
-----------
http://www.pressroom.com/~tglbatch/