file tempfile

Creates a temporary file and returns an open read-write channel to it.

file tempfile ?varName? ?template?

If varName is given, set that variable to the name of the temporary file (otherwise tries to delete the file at the first opportunity). If template is given, use it to guide the filename generated. Template is in form of a file path and may include one or more of the following <directory>, <prefix>, <extension>. Extension must be specified with a leading '.'. The exception is directory by itself. See examples below

This command was introduced by TIP # 210 in Tcl 8.6.


% # <directory><prefix><extension>
% file tempfile x /tmp/dingo.txt
% set x
% # <prefix><extension> 
% file tempfile x dingo.txt
% set x
%# <prefix>
% file tempfile x txt
% set x
%# <extension>
% file tempfile x .txt
% set x
% # Cant use directory by itself as shown below it takes the directory as / and prefix as tmp
% file tempfile x /tmp 
can't create temporary file: permission denied

AMG: How do I make a uniquely-named temporary directory?

aspect: TIP #431 Discussion has some discussion and code on this question.

AMG: Here's how to get comparable functionality in Tcl 8.4:

AMG: The template filename extension is ignored in Windows [1 ]. This is a problem because Windows uses extensions to determine how to "open" files.