Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
Write Additional Data within a txt File
I want to open a txt file, read it until I find a line that contains certain
info and then append additional data on to that line. The line may be in the middle of the file. How can I do this? |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
Write Additional Data within a txt File
<<I want to open a txt file, read it until I find a line that contains
certain info and then append additional data on to that line. The line may be in the middle of the file. How can I do this? Depends on how big the file is. For a small file, use the OpenText method to import into a worksheet, find the line that needs to be appended or changed, then SaveAs to a new text file. For a file that is too big to fit on a worksheet, then you will have to use either "Microsoft Scripting Runtime" or "Windows Script Host Object Model". Set a reference to one of them in the Tools|References dialog box. Both of them have FileSystemObject and TextStream objects available. Open the file, copy lines to a new file until you come to the line to be changed, change it, write it out, and continue copying lines. Download the Microsoft Windows Script Help file (SCRIPT56.CHM) for additional details. -- Regards, Bill Renaud |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
Write Additional Data within a txt File
Here is another way to do it. First, use this code to read the entire file
into a String variable (named TotalFile for this example), Split the text into individual Lines, find the line you are interested in, Join the individual lines into a single text string and save it back out... Dim X As Long Dim FileNum As Long Dim TotalFile As String Dim Lines() As String FileNum = FreeFile Open "C:\TEMP\Test.txt" For Binary As #FileNum TotalFile = Space(LOF(FileNum)) Get #FileNum, , TotalFile Close #FileNum Lines = Split(TotalFile, vbNewLine) For X = 0 To UBound(Lines) If Lines(X) = <<text you are interested in Then Lines(X) = Lines(X) & "<<text to add to end of line" End If Next TotalFile = Join(Lines, vbNewLine) FileNum = FreeFile Open "C:\TEMP\Test.txt" For Output As #FileNum Print #FileNum, TotalFile Close #FileNum Fill in the parts unique to your program (the Filename with Path, and the info inside the For-Next loop. Rick "Mike H." wrote in message ... I want to open a txt file, read it until I find a line that contains certain info and then append additional data on to that line. The line may be in the middle of the file. How can I do this? |
#4
Posted to microsoft.public.excel.programming
|
|||
|
|||
Write Additional Data within a txt File
I should probably include a standard warning... try you code out on a test
file first... when you write out the text back to the same file you read it in from (what I assumed you want to do), the original file is totally overwritten... if you did not write back the correct info (maybe you have a typo in a variable name or you got creative and changed my code in a way that isn't right), the data in the original file will be lost for good. Rick "Rick Rothstein (MVP - VB)" wrote in message ... Here is another way to do it. First, use this code to read the entire file into a String variable (named TotalFile for this example), Split the text into individual Lines, find the line you are interested in, Join the individual lines into a single text string and save it back out... Dim X As Long Dim FileNum As Long Dim TotalFile As String Dim Lines() As String FileNum = FreeFile Open "C:\TEMP\Test.txt" For Binary As #FileNum TotalFile = Space(LOF(FileNum)) Get #FileNum, , TotalFile Close #FileNum Lines = Split(TotalFile, vbNewLine) For X = 0 To UBound(Lines) If Lines(X) = <<text you are interested in Then Lines(X) = Lines(X) & "<<text to add to end of line" End If Next TotalFile = Join(Lines, vbNewLine) FileNum = FreeFile Open "C:\TEMP\Test.txt" For Output As #FileNum Print #FileNum, TotalFile Close #FileNum Fill in the parts unique to your program (the Filename with Path, and the info inside the For-Next loop. Rick "Mike H." wrote in message ... I want to open a txt file, read it until I find a line that contains certain info and then append additional data on to that line. The line may be in the middle of the file. How can I do this? |
#5
Posted to microsoft.public.excel.programming
|
|||
|
|||
Write Additional Data within a txt File
Rick, I like your response. I'll implement it. I think I will also rename
the original file as I do this in the potential event that someone else writes to this file in the meantime. then I can check for the existence of the file when I am ready to "put it back" and if there is one, take those contents and append to the bottom of my "new file". Then check again and so on and finally rename the whole thing to the original file name. This is for a log file of activity. I suppose I could have used an access db to write the stuff too also, but just now thought of that. I like the text file for something like this because it is so fast and then provides an easy avenue for reading the data back in without having the users having to have anything else installed on their pc's. The appending is to append an unload time for a file so I can track logins and logouts. One issue I have noticed. I put the "logout" procedure in before close event on the worksheet "area" but if a users clicks the red X and then changes his mind about exiting, I still get the event action it seems. Is there a "later" event that I should monitor for exiting that can't be fooled. "Rick Rothstein (MVP - VB)" wrote: I should probably include a standard warning... try you code out on a test file first... when you write out the text back to the same file you read it in from (what I assumed you want to do), the original file is totally overwritten... if you did not write back the correct info (maybe you have a typo in a variable name or you got creative and changed my code in a way that isn't right), the data in the original file will be lost for good. Rick "Rick Rothstein (MVP - VB)" wrote in message ... Here is another way to do it. First, use this code to read the entire file into a String variable (named TotalFile for this example), Split the text into individual Lines, find the line you are interested in, Join the individual lines into a single text string and save it back out... Dim X As Long Dim FileNum As Long Dim TotalFile As String Dim Lines() As String FileNum = FreeFile Open "C:\TEMP\Test.txt" For Binary As #FileNum TotalFile = Space(LOF(FileNum)) Get #FileNum, , TotalFile Close #FileNum Lines = Split(TotalFile, vbNewLine) For X = 0 To UBound(Lines) If Lines(X) = <<text you are interested in Then Lines(X) = Lines(X) & "<<text to add to end of line" End If Next TotalFile = Join(Lines, vbNewLine) FileNum = FreeFile Open "C:\TEMP\Test.txt" For Output As #FileNum Print #FileNum, TotalFile Close #FileNum Fill in the parts unique to your program (the Filename with Path, and the info inside the For-Next loop. Rick "Mike H." wrote in message ... I want to open a txt file, read it until I find a line that contains certain info and then append additional data on to that line. The line may be in the middle of the file. How can I do this? |
#6
Posted to microsoft.public.excel.programming
|
|||
|
|||
Write Additional Data within a txt File
One issue I have noticed. I put the "logout" procedure in before close
event on the worksheet "area" but if a users clicks the red X and then changes his mind about exiting, I still get the event action it seems. Is there a "later" event that I should monitor for exiting that can't be fooled. There the UserForm events QueryClose and Terminate that you can put code in to be executed when a UserForm is closed. Rick |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Write data in a closed file? | Excel Programming | |||
Write data in a closed file? | Excel Programming | |||
Open CSV file, format data and write output to a text file. | Excel Programming | |||
Push/write data into an unopened file | Excel Worksheet Functions | |||
Write data to access file through EXCEL | Excel Programming |