Link to home
Create AccountLog in
Avatar of justinBoucher
justinBoucherFlag 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

ASKER CERTIFIED SOLUTION
Avatar of Ignatius_A
Ignatius_A
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of 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)
Avatar of justinBoucher

ASKER

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

Change the open of the output file to this:

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

~bp