help for filei 

Write lines to end or beginning of text files

filei + [`"|"]text["|"'] ["]filename["]

filei - [`"|"]text["|"'] ["]filename["]


filei is an immediate command (hence the suffix i) that can be used to add extra lines to text files.

The simple forms filei + "text" filename and filei - "text" filename add a line including text at the end (+) or at the beginning (-) of filename. The quote marks " " are not essential if text is a single 'word' (meaning, it contains no embedded spaces: Stata and 42 are both words). Conversely, quote marks around filename are essential if it includes embedded spaces. Compound double quotes `" "' around text are essential if text includes one or more embedded quote marks.

There is asymmetry: if filename does not exist, the + syntax may be used to create a one-line file, but the - syntax returns an error.


filei is deliberately minimal and designed only to fill a very small gap, as (in the author's view) this minimal task should be minimally easy to do in Stata. More elaborate problems require more elaborate commands or programs, which exist in abundance. This sentence confirms that filei includes no syntax to delete lines or to insert lines in the middle of files.

filei may be used repeatedly to write successive lines to files. That is the user's call, but be warned that it is very inefficient, as files will be opened and closed repeatedly. You should consider using (e.g.) file instead, or even a text editor.


. filei + "stuff" newfile.txt

. filei - "earlier stuff" newfile.txt

. filei + "a longer line is possible" "filename with spaces.txt"


Nicholas J. Cox, Durham University, U.K. n.j.cox@durham.ac.uk

Also see

Manual: [P] file

Online: help for file