mfoster711
10 years agoRegular Contributor
Insert into TXT file
I am trying to insert text into a TXT file and having issues.
I have a process currently that reads and updates TXT files using ReadLine and WriteLine and it works fine as long as I format the lines to always be exactly the same length.
Now, I am trying to create a new process where I need to read through the same TXT, find a specific line, and then insert text at the end of that line. This causes the lines to now be different lengths. When I look at the TXT file afterwards, the new text was inserted but the very next line is missing characters. If I inserted 10 characters on line 5 then line 6 is missing the first 10 characters.
Am I trying to do something that is not practical? Here is my code:
Set myFile = aqFile.OpenTextFile(FilePath, aqFile.faReadWrite, aqfile.ctANSI, False) myFile.Cursor = 0 Line = -1 Do While Not myfile.IsEndOfFile CurrentLine = myFile.ReadLine Line = Line + 1 If ThisIsTheLineIamLookingFor Then CurrentLine = CurrentLine & Trim(NoteValue) Call myFile.SetPosition(Line, 0) myFile.WriteLine(CurrentLine) Exit Do End If Loop myFile.Close