Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

OpenTextFile Appending not working.....

Status
Not open for further replies.

BoostMR2

IS-IT--Management
Jul 20, 2007
33
0
0
US
I have a script that cycles through log files. As it reads each log file, it runs the sub below to open the output file, write a single line, then close the file.


Sub logStatus(message, account, sourcelog)
strFile = strBackupDir & strBackupFile
Set objFile = objFSO.OpenTextFile(strFile, 8, true)
objFile.WriteLine(Date & ";" & Time & ";" & account & ";" & sourcelog & ";" & message)
objFile.Close
End Sub


In my test, This sub runs twice during the script. In the file, it actually does open the file, write a line, close, and do that again, and BOTH lines are there, so i know it appended. If I run the script again, it overwrites what the previous script wrote, but still "appends" for each sub executed during the same script process. I cannot figure out why it overwrites everytime the script is run. I have made sure the file is not being recreated, by viewing the created date property of the file. I'm stumped! From what I understand, if a file is opened up with appending, it should always leave what is in there alone, and just add whatever write or writeline arguments to the end of the file.

Note: I already tried using const ForAppending = 8, = 3, and using "false" with the same combinations.
 
Are you opening the file in any other part of your script?

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
I do not open the same file anywhere else. At the very beginning of the script, I do have the following:

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objDir = objFSO.GetFolder(strDir)

If Not objFSO.FolderExists(strBackupDir) Then
objFSO.CreateFolder(strBackupDir)
End If

If Not objFSO.FileExists(strBackupFile) Then
objFSO.CreateTextFile(strBackupDir & strBackupFile)
End If


I use objFile to open the file to read, then close. Then I reuse objFile to open the write file, then close. This occurs for every file that starts with "backup" and ends with "log" in a loop that iterates through directories.

I checked the file create and modify dates. The create date/time is exactly the first time i ran the script several days ago, so I'm pretty sure it sees the file and does NOT recreate it.
 
BoostMR2,
what is that 3rd parameter that you are passing to the opentextfile? i usually only pass 2 - the var containing the file name and a constant var set to either read or write.
try removing the ', true' and see what you get.
regards,
longhair
 
The culprit is here:
If Not objFSO.FileExists(strBackupFile) Then

use this instead:
If Not objFSO.FileExists(strBackupDir & strBackupFile) Then

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
OMG! I haven't even made that change yet, but I can already tell, you are absolutely correct. Without the directory, the file never exists, and it always creates a new file. Sigh, I cannot believe I missed that! Sharp eyes, sir, thank you very much.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top