Link to home
Start Free TrialLog in
Avatar of mmitchell57
mmitchell57

asked on

VBScript, FileSystemObject, AtEndOfString Problems

Hello,

    I am sure this is something simple, but I haven't done this type of stuff in quite some time. I am trying to open one file, read it, put line by line info into an array. Then, I want to clean up the data and dump it to a new file.  The code I am including is my basic's that i'm starting with.  I cannot get the "f = fso.OpenTextFile" to cooperate at all. If I try to add the "ReadOnly, True" arguments, I get errors.  The "f.AtEndOfString" doesn't appear to be working either. Please offer some direction to get me going a bit further. I appreicate everything.

As a note, I'm using VBScripting. I would like to stay with that if at all possible. The originating data file is a txt file.  
Dim fso, f, f2, lineNum(), cnt, i, ii
ii = 1
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("c:\temp.txt")
 
Do while not f.AtEndOfString
  ReDim Preserve lineNum(ii)
  lineNum(ii) = ii
  ii = ii + 1
  Loop
 
f.Close
  
Set f2 = fso.CreateTextFile("C:\disabled.cvs")
  For each i in lineNum
   f2.WriteLine lineNum(i)
 Next
 
  f2.Close
  Set f = Nothing
  Set f2 = Nothing
  Set fso = Nothing

Open in new window

Avatar of mmitchell57
mmitchell57

ASKER

The first loop is just to make sure the loop is working, which it is. I just can't get the "F" object to work.  The "F2" works fine. If I change the "F" to create file, i can write to it all day long.  I'll try append as well.
Ok, i've mad it this far in the code. I now get an error message stating "Subscript out of range: "12"...

here's the code that's updated.
Dim fso, f, f2, ts, lineNum(), cnt, i, ii
ii = 1
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("c:\temp.txt", 1, true)
 
Do until ii = 10 
  ReDim Preserve lineNum(ii)
  lineNum(ii) = f.readline
  ii = ii + 1
  Loop
f.Close
  
Set f2 = fso.CreateTextFile("C:\disabled.cvs")
  For each i in lineNum
   f2.WriteLine lineNum(i)
  Next
  f2.Close
 
  Set f = Nothing
  Set f2 = Nothing
  Set fso = Nothing

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Excellent answer. :) I appreciate the info and help!
No problem. Thanks for the grade.

Regards,

Rob.