FileAppend

Appends text to a file (first creating the file, if necessary).

FileAppend [, Text, Filename]

 

Parameters

Text

The text to append to the file. This text may include linefeed characters (`n) to start new lines. In addition, a single long line can be broken up into several shorter ones by means of a continuation section.

If Text is blank, Filename will be created as an empty file (but if the file already exists, its modification time will be updated).

If Text is %ClipboardAll% or a variable that was previously assigned the value of ClipboardAll, Filename will be unconditionally overwritten with the entire contents of the clipboard (i.e. FileDelete is not necessary).

Filename

The name of the file to be appended, which is assumed to be in %A_WorkingDir% if an absolute path isn't specified.

To append in binary mode rather than text mode, prepend an asterisk to the filename. This causes each linefeed character (`n) to be written as as a single linefeed (LF) rather than the Windows standard of CR+LF. For example: *C:\My Unix File.txt

In v1.0.25+, if the file is not already open (due to being inside a file-reading loop), the file is automatically opened in binary mode if Text contains any carriage return and linefeed pairs (`r`n). In other words, the asterisk option described in the previous paragraph is put into effect automatically. However, specifying the asterisk when Text contains `r`n improves performance because the program does not need to scan Text for `r`n.

In v1.0.28.01+, specifying an asterisk (*) for Filename causes Text to be sent to standard output (stdout). Such text can be redirected to a file, piped to another EXE, or captured by fancy text editors. For example, the following would be valid if typed at a command prompt:
"%ProgramFiles%\AutoHotkey\AutoHotkey.exe" "My Script.ahk" >"Error Log.txt"

However, text sent to stdout will not appear at the command prompt it was launched from. This can be worked around by downloading a utility such as pipesplit.exe (8 KB) and using a command line such as the following:
"%ProgramFiles%\AutoHotkey\AutoHotkey.exe" "My Script.ahk" |pipesplit.exe nul

 

ErrorLevel

ErrorLevel is set to 1 if there was a problem or 0 otherwise.

 

Remarks

To overwrite an existing file, delete it with FileDelete prior to using FileAppend.

 

Related

FileRead, file-reading loop, FileReadLine, IniWrite, FileDelete, OutputDebug, continuation sections

 

Example

FileAppend, Another line.`n, C:\My Documents\Test.txt

; The following example uses a continuation section to enhance readability and maintainability:
FileAppend,
(
A line of text.
By default, the hard carriage return (Enter) between the previous line and this one will be written to the file.
	This line is indented with a tab; by default, that tab will also be written to the file.
Variable references such as %Var% are expanded by default.
), C:\My File.txt


; The following example demonstrates how to automate FTP uploading using the operating ; system's built-in FTP command. This script has been tested on Windows XP and 98se. FTPCommandFile = %A_ScriptDir%\FTPCommands.txt FTPLogFile = %A_ScriptDir%\FTPLog.txt FileDelete %FTPCommandFile% ; In case previous run was terminated prematurely. FileAppend, ( open host.domain.com username password binary cd htdocs put %VarContainingNameOfTargetFile% delete SomeOtherFile.htm rename OldFileName.htm NewFileName.htm ls -l quit ), %FTPCommandFile% RunWait %comspec% /c ftp.exe -s:"%FTPCommandFile%" >"%FTPLogFile%" FileDelete %FTPCommandFile% ; Delete for security reasons. Run %FTPLogFile% ; Display the log for review.