Solved

VBScript:  How to Read Text File and Output into CSV File

Posted on 2004-08-19
6
5,078 Views
Last Modified: 2008-01-09
I am looking for VBScript code to read contents of text file look for
location: and capture 10char from "location: " the subsequent line is "date booked: " capture date, the next line is "datetravel: " capture date of travel - output these three columns as Comma delimited into output.csv file. The script should only look for these three fields, Location, Date booked, Date Travel and capture values  associated to these three fields.

Please advise,

Thanks.

Merc
0
Comment
Question by:mercury2003
  • 2
  • 2
  • 2
6 Comments
 
LVL 7

Expert Comment

by:vnvk
ID: 11854778
You may not get it easily in the form of code, but you can make one easily, if you are well versed with code.

VB SCript is  miniature version of VB and if you know VB well, then this is easy.

I can give you the logic here:

declare a FileStream object
open the text file that object
read line by line
capture the details and write to a string
when there is a change, append this string to the output.csv file.
0
 
LVL 7

Expert Comment

by:vnvk
ID: 11854790
0
 
LVL 15

Accepted Solution

by:
Colosseo earned 100 total points
ID: 11856188
Hi mercury

I hope this is what you were after

I wasnt sure if location date booked and datetravel were the only three fields in the txt file file. If they are i can modify the script to remove some checking. Also if they are always always in the same order I can change the code to make it more straight forward.

As it is it assumes the filelds are in the order location, date booked and datetravel but the code allows for other lines before after and in between

Dim oFSO
Dim str_Input
Dim str_Output
Dim str_CurrentLine
Dim str_CSV

' the path to the input and output file
str_Input = "c:\input.txt"
str_Output = "c:\output.csv"

Set oFSO = CreateObject("Scripting.FileSystemObject")

' If the file exists
If oFSO.FileExists(str_Input) Then
  ' open the text file for input
  Set oInFile = oFSO.OpenTextFile(str_Input)
  ' create the csv file for output
  Set oOutFile = oFSO.CreateTextFile(str_Output)

 ' for each line in the input file
  While Not oInFile.AtEndOfStream
    ' read the line
    str_CurrentLine = oInFile.ReadLine()

    ' if the string location: is found in the file then remove it and trim of any white spaces
    If inStr(str_CurrentLine,"location:") > 0 Then
      str_CSV = str_CSV & Trim(replace(str_CurrentLine,"location:","")) & ","

    ' if the string date booked: is found in the file then remove it and trim of any white spaces
    ElseIf inStr(str_CurrentLine,"date booked:") > 0 Then
      str_CSV = str_CSV & Trim(replace(str_CurrentLine,"date booked:","")) & ","

    ' if the string datetravel: is found in the file then remove it and trim of any white spaces
    ElseIf inStr(str_CurrentLine,"datetravel:") > 0 Then
      str_CSV = str_CSV & Trim(replace(str_CurrentLine,"datetravel:",""))
    End If

  WEnd

  ' output the data to the csv file
  oOutFile.WriteLine(str_CSV)

  ' Close the input and output file
  oOutFile.Close
  oInFile.Close
End IF

' Tidy up
Set oFSO = Nothing

Regards

Scott
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:mercury2003
ID: 11856505
I added &VBCRlf to output each set in next line

' if the string datetravel: is found in the file then remove it and trim of any white spaces
    ElseIf inStr(str_CurrentLine,"datetravel:") > 0 Then
      str_CSV = str_CSV & Trim(replace(str_CurrentLine,"datetravel:","")) &VBCrLF

    End If
0
 

Author Comment

by:mercury2003
ID: 11856507
Thanks Though - Worked 100% per expecatation.

Scott - u get full 100 points.
0
 
LVL 15

Expert Comment

by:Colosseo
ID: 11856538
Hi mercury thanks for the grade

Sorry I didnt realise there was more than one set of location, date booked and datetravel in the text file.

At least you were able to easily update the code to cope with that which is great :)

Regards

Scott
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

It's sometimes a bit tricky to use date functions in Oracle BPEL. I'll explain quickly how you can add N days to the current date. In a BPEL process this can be useful, and you can adapt it to fit your needs. First of all, let's see how to add 1 …
I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

831 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