Product SiteDocumentation Site

8.72. SysTempFileName


>>-SysTempFileName(template-+---------+-)----------------------><
                            +-,filter-+

Returns a unique name for a file or directory that does not currently exist. If an error occurs or SysTempFileName cannot create a unique name from the template, it returns a null string (""). SysTempFileName is useful when a program requires a temporary file.
Parameters:
template
The location and base form of the temporary file or directory name. The template is a valid file or directory specification with up to five filter characters.
SysTempFileName generates the filter character replacements with a random number algorithm. If the resulting file or directory already exists, SysTempFileName increments the replacement value until all possibilities have been exhausted.
There are slight differences in how the template works on Windows and Unix/Linux.
On Windows: The file name part of the template can be any length. However, the template must contain at least 1 filter character or the empty string is returned. The unique name is created solely by substituting random numbers for the filter character(s). If there is a directory name part of the template in addition to the file name part, and the directory name part contains a non-existent directory, the function will succeed. However, the temporary file will not be writable unless the user first creates the non-existent directory, or directories. When the function succeeds, the result is always the same length as the template.
On Unix/Linux: Only the first five characters of the file name part of the template are used. Any characters after the first five in the file name part are ignored. The unique name is generated by the operating system adding a random string of characaters to the end of the template. The template does not have to include any filter characters. If the template does include filter characters, then ooRexx replaces those filter characters with random numbers. Because the operating system adds a random string to the end of the template, the result will always be longer than the template. In general, if the template does not contain a directory part, or if the directory part is not writable by the user, a writable directory part is added by the operating system (most likely /tmp.) This last behavior may vary depending on the exact version of the Unix/Linux operating system.
filter
The filter character used in template. SysTempFileName replaces each filter character in template with a numeric value. The resulting string represents a file or directory that does not exist. The default filter character is ?.

Example 8.41. RexxUtil - SysTempfilename

/* Windows Code */
say SysTempFileName("C:\TEMP\MYEXEC.???")
say SysTempFileName("C:\TEMP\MYEXEC.tmp")  -- produces the empty string.
say SysTempFileName("C:\TEMP\??MYEXEC.???")
say SysTempFileName("C:\MYEXEC@.@@@", "@")

/* Output */
C:\TEMP\MYEXEC.251

C:\TEMP\10MYEXEC.392
C:\MYEXEC6.019

/* Unix/Linux Code. mydir is an existing directory. */

say SysTempFileName("/mydir/MYEXEC.???")    -- filter characters are ignored
say SysTempFileName("/mydir/MYEXEC.tmp")    -- produces a unique name
say SysTempFileName("/mydir/??MYEXEC.???")
say SysTempFileName("/bogusdir/??MYEXEC.???")
say SysTempFileName("MYEXEC@.@@@", "@")

/* Output */
/mydir/MYEXEYqY2Hd
/mydir/MYEXET4dwdz
/mydir/77MYELnI0IU
/tmp/77MYEzjoweg
/tmp/MYEXEJNj3JB