We help IT Professionals succeed at work.
Get Started

Appending a text file in vbscript

justinBoucher
on
1,212 Views
Last Modified: 2012-05-09
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

Comment
Watch Question
This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE