Function Reference


_FileWriteToLine

Writes text to a specific line in a file

#include <File.au3>
_FileWriteToLine ( $sFilePath, $iLine, $sText [, $bOverWrite = False [, $bFill = False]] )

Parameters

$sFilePath The file containing the line to be written
$iLine The line number to which the text will be written
$sText The text to be written
$bOverWrite [optional]
    True - The text will overwrite the specified line
    False - (default) The text will be inserted as the specified line and all subsequent lines will move down
$bFill [optional]
    True - will add blank lines to file if necessary so line number exists
    False - (default) will not add lines. See remark below

Return Value

Success: 1
Failure: 0 and sets the @error flag to non-zero
@error: 1 - File has fewer lines than $iLine
2 - File does not exist
3 - Error when opening file
4 - $iLine is invalid
5 - $bOverWrite is invalid
6 - $sText is invalid
7 - $bFill is invalid

Remarks

A file may fail to open due to access rights or attributes.

If _FileWriteToLine() is called with $bOverWrite as 1 and $sText as "", it will remove the content of the line leaving it blank, but the line will still exist in the file.

By default, the function will return an error if the line does not exist in the file, but setting the $bFill parameter to True will force it will add blank lines to pad the file to a size allowing the line to be written.

Example

#include <File.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>

Example()

Func Example()
        ; Create a constant variable in Local scope of the filepath that will be read/written to.
        Local Const $sFilePath = _WinAPI_GetTempFileName(@TempDir)

        ; Create data to be written to the file.
        Local $sData = "Line 1: This is an example of using _FileWriteToLine()" & @CRLF & _
                        "Line 2: This is an example of using _FileWriteToLine()" & @CRLF & _
                        "Line 3: This is an example of using _FileWriteToLine()" & @CRLF & _
                        "Line 4: This is an example of using _FileWriteToLine()" & @CRLF & _
                        "Line 5: This is an example of using _FileWriteToLine()" & @CRLF

        ; Create a temporary file to read data from.
        If Not FileWrite($sFilePath, $sData) Then
                MsgBox($MB_SYSTEMMODAL, "", "An error occurred whilst writing the temporary file.")
                Return False
        EndIf

        ; Write to line 3 with overwriting set to true.
        _FileWriteToLine($sFilePath, 3, "Line 3: THIS HAS BEEN REPLACED", True)

        ; Read the contents of the file using the filepath.
        Local $sFileRead = FileRead($sFilePath)

        ; Display the contents of the file.
        MsgBox($MB_SYSTEMMODAL, "", "Contents of the file:" & @CRLF & $sFileRead)

        ; Write to line 3 with overwriting set to false.
        _FileWriteToLine($sFilePath, 3, "Line 3: THIS HAS BEEN INSERTED", False)

        ; Read the contents of the file using the filepath.
        $sFileRead = FileRead($sFilePath)

        ; Display the contents of the file.
        MsgBox($MB_SYSTEMMODAL, "", "Contents of the file:" & @CRLF & $sFileRead)

        ; Delete the temporary file.
        FileDelete($sFilePath)
EndFunc   ;==>Example