Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Read text file split lines for csv output

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,29586
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Read text file split lines for csv output
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Read text file split lines for csv output - 1/8/2006 6:18:23 AM   
  ziminski

 

Posts: 79
Score: 2
Joined: 1/8/2006
Status: offline
How can I read a textfile and spllit each line based on a given format? Much the same way bcp uses a fmt file for importing data into a database.
For example each line is 80 characters long, field 1 = 8, feild 2 =  2, feild 3 =20, field 4 = 15, feild 5 =15, feild 6 = 20
After spliting each line I need to insert a comma to make the file csv. So
sample data in:
1234567812John jones           At someplace soBeingsomthing   Finalfeild lengh20

output
12345678,12,John jones          , At someplace so,Beingsomthing  , Finalfeild lengh20 ,

I need to loop thru every line in the file .

Any ideas?
 
 
Post #: 1
 
 RE: Read text file split lines for csv output - 1/8/2006 7:50:04 AM   
  ziminski

 

Posts: 79
Score: 2
Joined: 1/8/2006
Status: offline
This now works

'ON ERROR RESUME NEXT
Const ForReading = 1,ForAppending = 8
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFileSystem = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile  ("C:\data.txt", ForReading)
strLogFile = "C:\" & Split(WScript.ScriptName, ".")(0) & ".csv"
Set objLogFile = objFileSystem.CreateTextFile(strLogFile, TRUE)

Do Until objTextFile.AtEndOfStream
strCharacters = objTextFile.Read(8)
objLogFile.Write strCharacters & ","
strCharacters = objTextFile.Read(2)
objLogFile.Write strCharacters & ","
strCharacters = objTextFile.Read(15)
objLogFile.Write strCharacters & ","
strCharacters = objTextFile.Read(20)
objLogFile.Write strCharacters & VbCrLf
Loop




< Message edited by ziminski -- 1/8/2006 8:37:48 AM >

(in reply to ziminski)
 
 
Post #: 2
 
 RE: Read text file split lines for csv output - 1/8/2006 1:36:32 PM   
  Cybex


Posts: 412
Score: 0
Joined: 9/14/2005
From: Florida
Status: offline
I would like to know how...

You never close the files you open, your example does not match the chatacter numbering in you script, you are not even reading the all of the sections you said you needed (there are 6, you are reading 4), and you are wirting "strCharacters" four times followed by a final "strCharacters" and a vbCrLF.  If by "It works", you mean it does not error out then yeah it will work. Once at least, you may not be able to open that file again though.


Try using this and see if it works...  This on also trims off the extra spaces on the output.

      


Cybex

EDIT*** Typo's

< Message edited by Cybex -- 1/8/2006 1:39:38 PM >


_____________________________

Common sense is not so common.

(in reply to ziminski)
 
 
Post #: 3
 
 RE: Read text file split lines for csv output - 1/8/2006 2:55:15 PM   
  kirrilian


Posts: 628
Score: 3
Joined: 3/15/2005
From:
Status: offline
the split() function is much easier and faster for parsing csv files


reference:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/fb2bbb28-85bc-42fc-85fb-ccc7da8abe8c.asp

_____________________________

Have you searched here ?
VBScript Fundamentals
My Site

(in reply to Cybex)
 
 
Post #: 4
 
 RE: Read text file split lines for csv output - 1/8/2006 3:21:42 PM   
  Cybex


Posts: 412
Score: 0
Joined: 9/14/2005
From: Florida
Status: offline
Right, but he is creating CSV files not reading them.


Cybex

_____________________________

Common sense is not so common.

(in reply to kirrilian)
 
 
Post #: 5
 
 RE: Read text file split lines for csv output - 1/8/2006 4:58:33 PM   
  kirrilian


Posts: 628
Score: 3
Joined: 3/15/2005
From:
Status: offline
my apologies!

_____________________________

Have you searched here ?
VBScript Fundamentals
My Site

(in reply to Cybex)
 
 
Post #: 6
 
 RE: Read text file split lines for csv output - 1/10/2006 10:56:46 AM   
  ziminski

 

Posts: 79
Score: 2
Joined: 1/8/2006
Status: offline
Cybex thank you, you are right that my example was sloppy and did not meet what I had said, I wrote the example on the web at one place, and the working code else where. I did at least close the file in my code. What I didn't do that yo did that  I like was use an array to store the values and then loop thru it.

This piece is sweet!!
CNum = Array(8,2,20,15,15,20)
Do Until objTextFile.AtEndOfStream
For Each x In CNum
     strCharacters = objTextFile.Read(x)
     objLogFile.Write Trim(strCharacters) & ","
Next
Loop


Thanks again
Rick

(in reply to Cybex)
 
 
Post #: 7
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Read text file split lines for csv output Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts