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

TRYING TO ADD A LINE AT THE BEGINNING OF A TEXT FILE 1

Status
Not open for further replies.

faxpay

Programmer
Nov 30, 2004
119
US
I am stuck. Want to add a line of text at the beginning of a text file. I have explored the File Object, the FileSystemObject, and the TextStreamObject. It looks like I can append to the file easily but I can't find anything that will allow me to add a line at the beginning of a file.

Then I thought I would create a file with the line I need at the top and then append the the other file to that but I can't see how to append one file's content to another file's content.

Any help is greatly appreciated.

faxpay,
Tom
 
- Create a new file
- Copy the first line
- Open the original file and extract the data and append it to the new file
- Delete the original
- Rename the new file to the original name

You could also just read the original file into a variable(unless too large), tack on the first line, then delete and re-create the original file, appending the new data into it.
 
.Then I thought I would ...

Private Sub Command1_Click()

Dim f As Integer
Dim a As String

f = FreeFile
Open "TheExistingFile.txt" For Input As f
a = Input$(LOF(f), f)
Close f

f = FreeFile
Open "TheNewFirstLine.txt" For Append As f
Print f, a
Close f

Kill "TheExistingFile.txt"
Name "TheNewFirstLine.txt" As "TheExistingFile.txt"

End Sub
 
HughLerwill,

Input$ gives err when at end of file.
I am geting err "input past end of file".

Can't put in a loop and read one line at a time and look for EOF because Line Input will not read CR ad LF's.

faxpay,
Tom
 
I do find a problem with my 'Print f, a' which should have been 'Print #f, a'.

However 'a = Input$(LOF(f), f)' seems to be running fine.

Just to be sure the test files exist try popping the following in between my Dim a As String and f = FreeFile.

'create files for testing
f = FreeFile
Open "TheExistingFile.txt" For Output As f
Print #f, "This is the text from the existing file" & vbCrLf & "blah de blah"
Close f
f = FreeFile
Open "TheNewFirstLine.txt" For Output As f
Print #f, "This is the text from the new first line"
Close f



 
HughLerwill,

Here is the code I came up with that seems to work. I will look at your last suggestion also. My code:

f = FreeFile
Open "c:\BANK OF AMERICA DIRECT DEPOSIT FILE FOR TRANSMISSION\HTTP ACH PAYRLR CAFR1031" For Input As f
Do Until EOF(f) = True
Line Input #f, a
FinishedFile = FinishedFile & a & Chr(13) + Chr(10)
Loop
Close f

f = FreeFile
Open "c:\BANK OF AMERICA DIRECT DEPOSIT FILE FOR TRANSMISSION\TheNewFirstLine.txt" For Append As f
Print #f, "$$ADD ID=ZXUUSRF1 BID='NWFACH12046968'"
Print #f, FinishedFile
Close f

Kill "c:\BANK OF AMERICA DIRECT DEPOSIT FILE FOR TRANSMISSION\HTTP ACH PAYRLR CAFR1031"
Name "c:\BANK OF AMERICA DIRECT DEPOSIT FILE FOR TRANSMISSION\TheNewFirstLine.txt" As "c:\BANK OF AMERICA DIRECT DEPOSIT FILE FOR TRANSMISSION\HTTP ACH PAYRLR CAFR1031"

I have something I don't understand. At the end of the original file, sitting all by itself at the beginning of the last line, there is a small box and inside is "SUB". This does not appear in my new file. Any idea what that is??

faxpay,
Tom
 
HughLerwill,

When I copy it this is what it looks like:

820000002603576693510000000000000000040892221204696825 121108250000001
9000001000003000000260357669351000000000000000004089222


Just a little square.

faxpay,
Tom
 
HughLerwill,

It's no go. Here is my code:

f = FreeFile
Open "c:\BANK OF AMERICA DIRECT DEPOSIT FILE FOR TRANSMISSION\HTTP ACH PAYRLR CAFR1031" For Input As f
a = Input$(LOF(f), f)
Close f

Hangs up at the input statement, get err "Input past end of file" 'a' string remains empty. This is a valid err for the Input stmnt. Help's suggestion was to use EOF before stmnt.

faxpay,Tom
 
HughLerwill,

Hey, I want to say thanks. I think that last code I sent you is going to do it for me. I could not have gotton there if you had not pointed me in the right direction and given me some possibilities.

Still can't figure out what that is at the end of my original file.

Thanks again.

faxpay,
Tom
 
Thanks for the star.

With ref to thread222-1515154 I think you will find that it was the trailing chr$(26) which was causing Input$ to fail in my code.
 
Yes, I agree. Input$() ignores all text mode I/O delimiters such as commas, quotes, ends of lines, and the ^Z unlike Input# and Line Input# statements. They get treated as data characters on input.

It is sort of saying "don't process the text delimiters, I know best.
 
One more seeming oddity.

If you use the VB6 native I/O statements that respect text delimiters they will treat either CR or CR/LF (but not LF alone) as line delimiters:
Code:
Option Explicit

Private Sub Command1_Click()
    Dim f As Integer
    
    On Error Resume Next: Kill "data.txt": On Error GoTo 0
    
    f = FreeFile(0)
    Open "data.txt" For Output As #f
    Print #f, "Line 1"; vbCr;
    Print #f, "Line 2"; vbCr;
    Print #f, Chr$(26);
    Close #f
    
    MsgBox "Created"
End Sub

Private Sub Command2_Click()
    Dim f As Integer, l As String
    
    f = FreeFile(0)
    Open "data.txt" For Input As #f
    Do Until EOF(f)
        Line Input #f, l
    Loop
    Close #f
    
    MsgBox "Last line: " & l
End Sub
This is helpful for processing odd text files like those from an Apple computer. Doesn't help with *nix files though, which tend to use LF in general.

Some of this probably goes back to GWBasic and QBasic, VB6's distant ancestors. Back then it was much rarer to encounter a Unix file and Linux wasn't even born until around 1991.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top