Create a new non-existing file name

From Cuis CookBook
Jump to navigation Jump to search

Problem It often happens it is necessary to make a new filename. For example a new log file name, or a new temporary file to contain the output of a Unix program. How to do it?

Solution. The method to use use DirectoryEntry >> nexNameFor: extension:. In the example below I create a new filename to be into the directory /tmp. Observe well the output is a String, not a FileEntry, the file is not created, just a name for it.

('/tmp' asDirectoryEntry ) nextNameFor: 'stdout' extension: 'txt'.        "=>  'stdout.001.txt' "

Solution 2. Use #assureExistence on the FileEntry or DirectoryEntry.

'/tmp/stdout.001.txt' asFileEntry assureExistence.`"=>  /tmp/stdout.001.txt"