TIP 431: Add 'tempdir' Subcommand to 'file'

Author:         Kevin Pasko <[email protected]>
State:          Final
Type:           Project
Tcl-Version:    8.7
Vote:           Done
Created:        10-Sep-2014
Keywords:       Tcl, directory, file
Tcl-Branch:     tip-431
Votes-For:      DKF, FV, KBK, JN, SL
Votes-Against:  none
Votes-Present:  none


This TIP proposes adding a new tempdir subcommand to the file command, simplifying the effort required in creating uniquely named temporary directories at the scripting level.


Due to the non-atomic nature of the file mkdir command it is currently impossible to create uniquely named temporary directories at the script level without the possibility of race conditions.


The file tempdir command shall implement the functionality of the POSIX standard mkdtemp() function. With no arguments file tempdir shall create a uniquely named temporary directory in the native operating system's temporary directory, with naming convention "tcl_XXXXXX" where each X is a randomly selected character (following the file tempfile naming convention). Successful completion of file tempdir shall return the absolute path of the created directory, otherwise an error shall be thrown. (The number of X characters shall be system specific.)

file tempdir shall have an optional argument, template, to modify the created directory's path and name. The template shall be decomposed into (up to) two parts: the directory's path and rootname. If either part is absent, relevant defaults (e.g., according to the native operating system) shall be used. The entire temporary name shall then be formed from the path, the root, and a generated unique string of (typically) six characters (eight on Windows, due to case-insensitive filesystems reducing the size of portable random alphabet). If the directory part of the template is supplied, it should refer to a directory on the native filesystem.

The command syntax should be defined as:

file tempdir ?template?


Reference Implementation

An example of temporary directory creation has already been developed in the Tcl core, at the C level, within the Windows-specific layers of the load command. The principal work remaining is to expose this via a Tcl command and building equivalent functionality on other platforms.


This document has been placed in the public domain.