Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question
    
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? –  xslittlegrass May 12 at 23:22
    
@xslittlegrass I think Kuba's question can be restated: Are you concerned with the internal workings behind CreateTemporary or merely with accomplishing the task? –  Mr.Wizard May 12 at 23:25
1  
@Mr.Wizard I guess I'm also concerned with the internal workings of CreateTemporary. Thanks for explaining. –  xslittlegrass May 12 at 23:32
    
Tagged accordingly. –  Mr.Wizard May 12 at 23:33

2 Answers 2

up vote 3 down vote accepted

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.

share|improve this answer
    
I plan to do this in parallel for many times, does it guarantee to be unique for each time I generate? –  xslittlegrass May 12 at 23:34
    
@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. –  halirutan May 12 at 23:38
    
halirutan Kuba I guess 100*numberOfKernels is enough for me. Thanks :) –  xslittlegrass May 12 at 23:48
    
@xslittlegrass See my update for the real unique parallel version. –  halirutan May 12 at 23:56
    
@halirutan That's really cool! Thank a lot. –  xslittlegrass May 13 at 0:02

Rather ugly but should work

Block[{m}, 
 While[FileExistsQ[
   m = FileNameJoin[{Directory[], FromCharacterCode[RandomInteger[25, 35] + 97]}]]];
 m]
share|improve this answer
    
unless you've got many strange files :) –  Kuba May 12 at 23:35
1  
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... –  Albert Retey May 13 at 14:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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