justinBoucher
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.
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks.
It worked. I didn't even see that. I guess I wasn't paying attention.
It worked. I didn't even see that. I guess I wasn't paying attention.
Change the open of the output file to this:
Set objOutputFile = objFSO.OpenextFile("c:\out put_file.t xt", ForAppending, True)
~bp
Set objOutputFile = objFSO.OpenextFile("c:\out
~bp
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:\ou