Does anyone have a method for creating a pseudo-random temporary file in Windows? Something like the Unix mktemp command?
You mean like the function GetTemporaryFolderItem()
?
I used the microseconds to form a filename.
believe it or not there is a very tiny chance that this is NOT unique
Unfortunately, GetTemporaryFolderItem() doesn’t allow me to specify where I want it. I need to create it within a controlled and contained set of folders.
microseconds with a random string of 4 characters prepended and the PID appended is a good place to start.
Pluck the filename from GetTemporaryFolderItem and use that to create the file wherever you want.
In that case, I may as well reinvent the wheel. Plus, there’s no guarantee that the unique name is unique for the new folder location.
Warning, wheel reinvention pseudo logic follows:
If passed parent folder is Nil
use system tmp
If passed folder doesn't exist and the "make folder flag" is specified
Check for the existence of a folder with the generated name
If folder exists
fail and raise "Unique Name Folder Exists Conflict" error
Get template and replace the magic character with randomized ASCII characters (a-Z + 0-9)
If file exists
generate new random name as above
Create file and set owner permissions
careful as things like mktemp make it impossible for some other process to create a file with the same name
its possible that between your checks for “if file exists” and “create file” that some other process creates a file with the same name
so check if the create fails because it already exists and DON’T allow it to overwrite an existing file
or declare into GetTempFileName on Windows
http://msdn.microsoft.com/en-us/library/windows/desktop/aa363875(v=vs.85).aspx