Jump to content

Text File, add line to the beginning of a text file?


 Share

Recommended Posts

New User

Sorry I can’t work this one out; I’m trying to add a new line of text to the beginning of a text file without deleting the original line, I got this example, but I don’t know how this can be achieve, I would be grateful if you point me in the right direction. Thanks for your help

FileWriteLine.au3

Link to comment
Share on other sites

#include <Constants.au3>
Local Const $sFile = "c:\hello.txt"
$hFile = FileOpen($sFile, 0)
$chars = FileRead($hFile)
FileClose($hFile)
$hFile = FileOpen($sFile, 2)
FileWrite($hfile, "Line1"& @CRLF & $chars)
FileClose($hfile)
ShellExecute("c:\hello.txt")

Link to comment
Share on other sites

_FileWriteLine_top("myfile.txt","My Line")

Func _FileWriteLine_top($file, $txt)
  $txt &= @CRLF & FileRead($file)
  $fh = FileOpen($file, 1)
  If $fh = -1 Then Return SetError(1, 0, 0)
  Local $iWriteFile = FileWriteLine($fh, $txt)
  Local $iRet = FileClose($fh)
  If $iWriteFile = -1 Then Return SetError(2, $iRet, 0)
  Return $iRet
EndFunc

Adapted from _FileWriteLog() :mellow:

Edited by Hannes123
Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]
Link to comment
Share on other sites

#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)

From the help file.

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...