Jump to content
Sign in to follow this  
minder

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

Recommended Posts

minder

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

Share this post


Link to post
Share on other sites
Juvigy

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

Share this post


Link to post
Share on other sites
hannes08

_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]

Share this post


Link to post
Share on other sites
minder

Thank you so much :mellow:

Share this post


Link to post
Share on other sites
ASut

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

Share this post


Link to post
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
Sign in to follow this  

×