Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VBScript, FileSystemObject, AtEndOfString Problems

Posted on 2008-10-09
5
Medium Priority
?
399 Views
Last Modified: 2010-04-21
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

0
Comment
Question by:mmitchell57
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 

Author Comment

by:mmitchell57
ID: 22680670
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.
0
 

Author Comment

by:mmitchell57
ID: 22680919
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

0
 
LVL 65

Accepted Solution

by:
RobSampson earned 200 total points
ID: 22682352
Hi, I've included a shorter way to put the contents of a file into an array.  Then when you are outputting the lines to File2, you can do any validation on the lines to clean it up.

Regards,

Rob.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const intForReading = 1
strFile1 = "C:\Temp\Text.txt"
strFile2 = "C:\Temp\Disabled.cvs"
 
Set objFile1 = objFSO.OpenTextFile(strFile1, intForReading, False)
' This is a short way of reading the entire contents into an array
arrContents = Split(objFile1.ReadAll, VbCrLf)
objFile1.Close
 
Set objFile2 = objFSO.CreateTextFile(strFile2, True)
For Each strLine In arrConents
	objFile2.WriteLine strLine
Next
objFile2.Close
 
MsgBox "Done"

Open in new window

0
 

Author Closing Comment

by:mmitchell57
ID: 31504837
Excellent answer. :) I appreciate the info and help!
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 22683607
No problem. Thanks for the grade.

Regards,

Rob.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question