Solved

VBScript Function to process file in -> file out

Posted on 2008-06-11
3
294 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
  • 2
3 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
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
Comment Utility
VBScript. Not VBA; not VB6; not VB.Net. VBScript.
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Welcome to part one of a multi-part tutorial series, VBScript for Windows System Administrators.  The goal of this series is to teach non-programmers how to write useful VBS code to automate their environment, and perform tasks faster, and in a more…
This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now