Jump to content

Append Text Line


onestcoder
 Share

Recommended Posts

I'm trying to add text to the end of line one of a text file. I want to be about to write to the same line multiple times without deleting the previous text, by append the new text to the end of it.

I'm willing to use an ini file if needed.

Ultimately i want to write a list to a text file that I can call into a combo box in another program.

Edited by onestcoder

Need a website: http://www.iconixmarketing.com

Link to comment
Share on other sites

I'm trying to add text to the end of line one of a text file. I want to be about to write to the same line multiple times without deleting the previous text, by append the new text to the end of it.

I'm willing to use an ini file if needed.

Ultimately i want to write a list to a text file that I can call into a combo box in another program.

Try this:

#include <File.au3>

Func _FileAppendToLine($PATH,$LINE,$TEXT)
    $FILE = FileOpen($PATH,0)
    $TEMP_LINE = FileReadLine($FILE,$LINE)
    FileClose($FILE)
    _FileWriteToLine($PATH,$LINE,$TEMP_LINE & $TEXT,1)
EndFunc

$PATH = @ScriptDir & "\MyFile.txt"
_FileAppendToLine($PATH,3,"New Text")
Edited by Andreik

When the words fail... music speaks.

Link to comment
Share on other sites

Andreik

Why not just:

#include <File.au3>

$sFile = "c:\test.txt"

_FileWriteToLine($sFile, 2, "New string", 0) ;If set to 1 will overwrite the old line. If set to 0 will not overwrite

For example:

Test.txt

LINE1

LINE2

LINE3

If you use your code will be:

LINE1

NewString

LINE2

LINE3

And onestcoder want to write the same line (in his case the last line).

When the words fail... music speaks.

Link to comment
Share on other sites

Andreik

OK, now I understand. Your example good, but not for a big file, because the FileReadLine() function always count strings from first string :)

_FileAppendToLine("c:\test.txt", 3, "New Text")

Func _FileAppendToLine($sFile, $sLine, $sString)
    Local $iRead, $aString, $iResult, $hFile, $i
    
    $iRead = FileRead($sFile)
    
    $aString = StringSplit(StringStripCR($iRead), @LF)
    If $aString[0] < $sLine Then Return SetError(1, 0, 0)
    
    $aString[$sLine] &= $sString
    
    For $i = 1 To $aString[0]
        $iResult &= $aString[$i] & @CRLF
    Next
    
    $hFile = FileOpen($sFile, 2)
    FileWrite($hFile, $iResult)
    FileClose($hFile)
EndFunc   ;==>_FileAppendToLine

Link to comment
Share on other sites

Andreik

OK, now I understand. Your example good, but not for a big file, because the FileReadLine() function always count strings from first string :)

_FileAppendToLine("c:\test.txt", 3, "New Text")

Func _FileAppendToLine($sFile, $sLine, $sString)
    Local $iRead, $aString, $iResult, $hFile, $i
    
    $iRead = FileRead($sFile)
    
    $aString = StringSplit(StringStripCR($iRead), @LF)
    If $aString[0] < $sLine Then Return SetError(1, 0, 0)
    
    $aString[$sLine] &= $sString
    
    For $i = 1 To $aString[0]
        $iResult &= $aString[$i] & @CRLF
    Next
    
    $hFile = FileOpen($sFile, 2)
    FileWrite($hFile, $iResult)
    FileClose($hFile)
EndFunc   ;==>_FileAppendToLine

Your code work fine. Just edit _FileAppendToLine("C:\test.txt", 3, "New Text"). >_<

When the words fail... music speaks.

Link to comment
Share on other sites

THAT IS AWESOME GUYS!!!!! Thank you.

FYI: I'm writing a program were I have a combo box "Customer's Name". The combo box gets it's content from a text file I manually wrote the customers name's in (ex: John Marks|Jane Marks....) Then I added to the file menu "Add New Customer" which I want to open the text file and add the customer's name to the list, thus adding them to the combo box list.

Thank for the help :)

When the program is complete i plan to add it the the examples forum.

Edited by onestcoder

Need a website: http://www.iconixmarketing.com

Link to comment
Share on other sites

Why not this.... it's been passed the review and is an included UDF

#include <File.au3>
;Example: Write to line 3 of  c:\test.txt REPLACING line 3
_FileWriteToLine("c:\test.txt", 3, "my replacement for line 3", 1)
;Example: Write to line 3 of c:\test.txt NOT REPLACING line  3
_FileWriteToLine("c:\test.txt", 3, "my insertion",  0)

.... straight from help

8)

NEWHeader1.png

Link to comment
Share on other sites

THAT IS AWESOME GUYS!!!!! Thank you.

FYI: I'm writing a program were I have a combo box "Customer's Name". The combo box gets it's content from a text file I manually wrote the customers name's in (ex: John Marks|Jane Marks....) Then I added to the file menu "Add New Customer" which I want to open the text file and add the customer's name to the list, thus adding them to the combo box list.

Thank for the help :)

When the program is complete i plan to add it the the examples forum.

might want to take a look at this, it does what you are after, but uses a listview to show other info for the customer too.

Posted Image

Located here.............

http://www.autoitscript.com/forum/index.ph...st&p=282067

8)

Edited by Valuater

NEWHeader1.png

Link to comment
Share on other sites

Why not this.... it's been passed the review and is an included UDF

#include <File.au3>
;Example: Write to line 3 of  c:\test.txt REPLACING line 3
_FileWriteToLine("c:\test.txt", 3, "my replacement for line 3", 1)
;Example: Write to line 3 of c:\test.txt NOT REPLACING line  3
_FileWriteToLine("c:\test.txt", 3, "my insertion",  0)

.... straight from help

8)

Because "my replacement for line 3" and "my insertion" will not be on the same line.

When the words fail... music speaks.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...