Avatar of justinBoucher
justinBoucher
Flag for United States of America asked on

Appending a text file in vbscript

Hi,
  I have a file comparison vbscript that writes an output file. I have marked the output file for appending in the vbscript but it keeps writing over the original output. I just had this script looked at on this site, but I showed the the person who needs the script and they wanted it to append the output underneath the first output. Does that make sense?

The outfile file looks like this:
test
test
test
================== Monday, February 22, 2010 ==================

Wnen it should look like this:
test
test
test
================== Sunday, February 21, 2010 ==================

test
test
test
================== Monday, February 22, 2010 ==================

Can someone help me out please. Thank you in advance.
Dim fileDiff
Dim compareDate
Const ForReading = 1, ForWriting = 2, ForAppending = 8

compareDate = FormatDateTime(Date(), 1)

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objInputFile2 = objFSO.OpenTextFile ("C:\file2.txt", ForReading, True)
Set objOutputFile = objFSO.createtextfile("c:\output_file.txt", ForAppending, True)

Do Until objInputFile2.AtEndOfStream

fileDiff = false
strNextLine2 = LCASE(objInputFile2.Readline)

Set objInputFile1 = objFSO.OpenTextFile ("C:\file1.txt", ForReading, True)
Do Until objInputFile1.AtEndOfStream
strNextLine1 = LCASE(objInputFile1.Readline)
If (strNextLine2 = strNextLine1) Then
fileDiff = true
End If
Loop

objInputFile1.Close
If (fileDiff = false) Then
objOutputFile.WriteLine strNextLine2
End If
Loop

objOutputFile.WriteLine "================== " & compareDate & " =================="

objInputFile2.Close
objOutputFile.Close
Set objInputFile1 = nothing
Set objInputFile2 = nothing
Set objOutputFile = nothing

Open in new window

VB ScriptWindows OS

Avatar of undefined
Last Comment
Bill Prew

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Ignatius_A

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Batuhan Cetin

Set objOutputFile = objFSO.createtextfile("c:\output_file.txt", ForAppending, True)

In this line you are actually re-creating the file everytime script runs. Create the file first, then open it forAppending:

Set objOutputFile = objFSO.OpenTextFile("c:\output_file.txt", ForAppending, True)
justinBoucher

ASKER
Thanks.
It worked. I didn't even see that. I guess I wasn't paying attention.
Bill Prew

Change the open of the output file to this:

Set objOutputFile = objFSO.OpenextFile("c:\output_file.txt", ForAppending, True)

~bp
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck