6
$\begingroup$

Function CreateTemporary can generate a unique file name like

file = CreateTemporary[]
(* "C:\Documents and Settings\kkouptsov\Local \Settings\Temp\m-263a0380-3a03-49ac-95cb-d21390b2d3fd" *)

How do we generate a unique file name such as above example? The reason I don't use CreateTemporary because I don't need to actually create the file, but only need the file path.

I plan to generate many of these names in parallel, and they should be unique to each other.

$\endgroup$
4
  • $\begingroup$ Yes, I need a unique file name such as the file name created by CreateTemporary. But I guess they are the same thing, because if we can create a unique string, we can use it as a file name. Sorry for the confusing, but how exactly what works? $\endgroup$ May 12, 2014 at 23:22
  • $\begingroup$ @xslittlegrass I think Kuba's question can be restated: Are you concerned with the internal workings behind CreateTemporary or merely with accomplishing the task? $\endgroup$
    – Mr.Wizard
    May 12, 2014 at 23:25
  • 1
    $\begingroup$ @Mr.Wizard I guess I'm also concerned with the internal workings of CreateTemporary. Thanks for explaining. $\endgroup$ May 12, 2014 at 23:32
  • $\begingroup$ Tagged accordingly. $\endgroup$
    – Mr.Wizard
    May 12, 2014 at 23:33

3 Answers 3

7
$\begingroup$

If it is merely about creating something unique, why don't you use the current time to build a readable file-name which is unique?

DateString[{"file", "Year", "-", "MonthNameShort", 
  "Day", "-", "Hour24", "Minute", "Second", ".txt"}]
(* "file2014-May13-013029.txt" *)

Since it contains the time it will be unique (in this case up to the second). When you create many files and additionally, do it in parallel, you can add a counter and the kernel id. This lets you identify the kernel which created the name and it makes the files unique in one run. When you start it again, the time will be different and the counter can start over.

ParallelEvaluate[i = 0];
ParallelTable[
 DateString[{ToString@$KernelID, "Year", "-", "MonthNameShort", "Day",
    "-", "Hour24", "Minute", "Second", "-", IntegerString[i++, 16, 5],
    ".txt"}], {10}]
(* {"82014-May13-015507-00000.txt", \
"82014-May13-015507-00001.txt", "72014-May13-015507-00000.txt", \
"72014-May13-015507-00001.txt", "62014-May13-015507-00000.txt", \
"52014-May13-015507-00000.txt", "42014-May13-015507-00000.txt", \
"32014-May13-015507-00000.txt", "22014-May13-015507-00000.txt", \
"12014-May13-015507-00000.txt"} *)

I tested this with 10^5 file-names and all were unique.

$\endgroup$
5
  • $\begingroup$ I plan to do this in parallel for many times, does it guarantee to be unique for each time I generate? $\endgroup$ May 12, 2014 at 23:34
  • $\begingroup$ @Kuba You can create (on my system) 100 file-names per second. When xslittergrass wants to do this in parallel, just include an identifier of the kernel and you can create 100*numberOfKernels files. $\endgroup$
    – halirutan
    May 12, 2014 at 23:38
  • $\begingroup$ halirutan Kuba I guess 100*numberOfKernels is enough for me. Thanks :) $\endgroup$ May 12, 2014 at 23:48
  • $\begingroup$ @xslittlegrass See my update for the real unique parallel version. $\endgroup$
    – halirutan
    May 12, 2014 at 23:56
  • $\begingroup$ @halirutan That's really cool! Thank a lot. $\endgroup$ May 13, 2014 at 0:02
3
$\begingroup$

Version 10.0 introduced CreateUUID

CreateUUID[]
(* "1aaeab6f-ad51-48d2-a939-366da47374a3" *)

The documentation says that

  • CreateUUID[] gives a UUID based on a 128-bit number, formatted as 32 hexadecimal digits grouped in the format 8-4-4-4-12.

  • CreateUUID[] makes use of detailed local system and session information, as well as absolute time.

  • Some Wolfram System features depend on assuming the UUIDs will not collide.

Just for fun I tested this with 100.000 UUIDs

Table[CreateUUID[], 100000] // Apply@UnsameQ
(* True *)
$\endgroup$
2
$\begingroup$

Rather ugly but should work

Block[{m}, 
 While[FileExistsQ[
   m = FileNameJoin[{Directory[], FromCharacterCode[RandomInteger[25, 35] + 97]}]]];
 m]
$\endgroup$
2
  • $\begingroup$ unless you've got many strange files :) $\endgroup$
    – Kuba
    May 12, 2014 at 23:35
  • 1
    $\begingroup$ it probably is worth noting that it is common practice to use such random strings as "global unique identifiers" (GUID). Of course they are not guaranteed to be unique, but only have probabilities of almost 1 to be so. See e.g. the wikipedia article on GUID for details. From the format of the strings I could well imagine that the conventions mentioned there are what Mathematica uses for CreateTemporary, or at least something very close... $\endgroup$ May 13, 2014 at 14:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.