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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read from a file 1

Status
Not open for further replies.

omegabeta

Programmer
Aug 20, 2003
148
RO
Hi,

I have a .txt log file. From my script I want to add a new line that contains date, hour and location of backup.
I've tried with ForAppending and it works. But I want that the last information to be on top so, descending data.

My procedure is the following:

Sub LogFile(FileLoc)
Dim objFile, ts,SearchString, SearchChar
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Set objFile = fso.GetFile("LogFile.txt")
Set ts = objFile.OpenAsTextStream(ForReading, TristateUseDefault)
MsgBox Mid(ts,Instr(1, ts, "#", 1))
ts.WriteLine(" "& date & " " & time & " " & FileLoc)
ts.Close
End Sub

In my text file I have '#' sign after that is my util information
I've tried to isolate the util text and then edit. At the line with MsgBox I get the message "Object doesn't support this property or method".
How can I solve my problem ?
 
Open a temp file. Write the new line to the temp file. Open the old file for reading. Read each line from the old file and write to the temp file. Close all open files. delete the old file. Rename the temp file to the old file name.

[blue]"Well, once again my friend, we find that science is a two headed beast. One head is nice, it gives us aspirin and other modern conveniences,...but the other head of science is BAD! Oh, beware the other head of science, Arthur; it bites!!" - The Tick[/blue]
 
With FSO you can't insert lines in amiddle of a file.
The basic idea is to use a reading/writing loop playing with 2files and inserting your new line at the appropriate place.

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
My text file looks like the following:

------------------------------------------------------------
Data Hour Location
------------------------------------------------------------#
12.05.2004 16:57:57 Computer1
12.05.2004 17:22:58 Computer2
12.05.2004 17:23:18 Computer1

How can I store the text after "#" sign to copy then into the temporary file ?
 
Try this:
Code:
Option Explicit
Dim oFSO, oOldFile, oTempFile, i, strLine

Set oFSO = CreateObject("Scripting.FileSyatemObject")
Set oTempFile = oFSO.CreateTextFile("temp.txt")
Set oOldFile = oFSO.OpenTextFile("log.txt")

i = 1
While oOldFile.AtEndOfStream <> True
	 If i = 4 Then
	 	oTempFile.WriteLine "12.05.2004" & vbTab & "17:57:57" & vbTab & "Computer3"
	 End If
	 strLine = oOldFile.ReadLine
	 oTempFile.WriteLine strLine
	 i = i + 1
Wend

oOldFile.Close
oTempFile.Close
Set oOldFile = Nothing
Set oTempFile = Nothing

oFSO.DeleteFile "log.txt"
oFSO.CopyFile "temp.txt", "Log.txt"
oFSO.DeleteFile "temp.txt"

Set oFSO = Nothing

[blue]"Well, once again my friend, we find that science is a two headed beast. One head is nice, it gives us aspirin and other modern conveniences,...but the other head of science is BAD! Oh, beware the other head of science, Arthur; it bites!!" - The Tick[/blue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top