Go to the first, previous, next, last section, table of contents.


Generating Unique File Names

Some programs need to write temporary files. Here is the usual way to construct a name for such a file:

(make-temp-name
 (expand-file-name name-of-application
                   temporary-file-directory))

The job of make-temp-name is to prevent two different users or two different jobs from trying to use the exact same file name. This example uses the variable temporary-file-directory to decide where to put the temporary file. All Emacs Lisp programs should use temporary-file-directory for this purpose, to give the user a uniform way to specify the directory for all temporary files.

Function: make-temp-name string
This function generates a string that can be used as a unique file name. The name starts with string, and contains a number that is different in each Emacs job.

(make-temp-name "/tmp/foo")
     => "/tmp/foo232J6v"

To prevent conflicts among different libraries running in the same Emacs, each Lisp program that uses make-temp-name should have its own string. The number added to the end of string distinguishes between the same application running in different Emacs jobs. Additional added characters permit a large number of distinct names even in one Emacs job.

Variable: temporary-file-directory
This variable specifies the directory name for creating temporary files. Its value should be a directory name (see section Directory Names), but it is good for Lisp programs to cope if the value is a directory's file name instead. Using the value as the second argument to expand-file-name is a good way to achieve that.

The default value is determined in a reasonable way for your operating system; on GNU and Unix systems it is based on the TMP and TMPDIR environment variables.

Even if you do not use make-temp-name to choose the temporary file's name, you should still use this variable to decide which directory to put the file in.


Go to the first, previous, next, last section, table of contents.