Solved

VBScript Function to process file in -> file out

Posted on 2008-06-11
3
340 Views
Last Modified: 2010-04-16
I need a function in VBScript that accepts 3 parameters, an input filename, an output filename and a string.
I want the function to open the input file for reading and the output file for appending.
I want to write each line of the input file to the output file with the string prepended to the data, then close the files and return the number of lines written (or read)
Here is an example:
calling the function:
  lc = WriteOutput("c:\mydata\myfilein.txt", "c:\mydata\myfileout.txt", "123456,")
  MsgBox cstr(lc) & " lines written"

If the contents of c:\mydata\myfilein.txt was:
abc,def,xyz
tuv,mno,rts

the file c:\mydata\myfileout.txt would contain (after the call):
123456,abc,def,xyz
123456,tuv,mno,rts

IMPORTANT: If c:\mydata\myfileout.txt already contains data, I want the additional data appended to the file. Thus, if I called the function a second time, with a different input filename and string but same output file, the contents of the string + input file  would be added to the output file.
0
Comment
Question by:dbbishop
[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
  • 2
3 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 21761647
Please refine what you mean by "VBScript" as it is often used to refer to any code snippet in one of many VB environments...

VBScript (.vbs)
VBA (Office App)
VB6 (or below)
VB.Net 2003 (or below)
VB.Net 2005 (or above)
0
 
LVL 15

Author Comment

by:dbbishop
ID: 21761759
VBScript. Not VBA; not VB6; not VB.Net. VBScript.
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 21762428
Something like this should work...
Const ForReading = 1, ForWriting = 2, ForAppending = 8
 
Function WriteOut(inFile, outFile, prepend)
    Dim fso, f, f2
    Dim inputLine, lineCount
    lineCount = 0
    Set fso = CreateObject("Scripting.FileSystemObject")
    If fso.FileExists(inFile) Then
        Set f = fso.OpenTextFile(inFile, ForReading, False)
        Set f2 = fso.OpenTextFile(outFile, ForAppending, True)
        While Not f.AtEndOfStream
            inputLine= f.ReadLine
            lineCount = lineCount + 1
            f2.WriteLine prepend & inputLine
        Wend
        f.Close
        Set f = Nothing
        f2.Close
        Set f2 = Nothing
    Else
        MsgBox inFile, vbOKOnly, "inFile Not Found"
    End If
    Set fso = Nothing
    WriteOut = lineCount
End Function

Open in new window

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

707 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