Re: Create unique temporary file [message #40669] |
Wed, 25 August 2004 06:54  |
Paul Van Delst[1]
Messages: 1157 Registered: April 2002
|
Senior Member |
|
|
Michael Wallace wrote:
>> Here is how I create a unique journal file name:
>>
>> Print, String('journal_', Bin_Date(SysTime()), '.pro', $
>> Format='(A, I4, 5I2.2, A)')
>
>
> Thanks for the idea. I had thought of using a current system time
> stamp, but it totally slipped my mind that I could concatenate the
> number together so easily. It's little details like that which I'm
> still learning. When I finally wrote my code, not only did I take the
> date as you did, but I further appended a random number and I did a
> check of file existence to ensure that nothing would ever get clobbered.
> The only way this could fail would be if there were some insane
> once-in-a-lifetime freakish occurrence. Now that I've said this, this
> will happen tomorrow. ;-)
I do a similar thing in some shell scripts I have, but I append a sequential number rather
than a random number. E.g. I start with my time (and username!) stamped file and append a
"_1". I check if that file exists. If it does I change the suffix to "_2". Rinse and
repeat as necessary. If the file counter gets larger than 100, I stop with an error.
I've had the number suffix get as high as 15 - i.e. the line of code in my script that
compiles the time stamped filename was executed simultaneously 14 times. Ahh, the joys of
having 100's of processors to play with. :o)
paulv
|
|
|
Re: Create unique temporary file [message #40674 is a reply to message #40669] |
Tue, 24 August 2004 23:47   |
Michael Wallace
Messages: 409 Registered: December 2003
|
Senior Member |
|
|
> Here is how I create a unique journal file name:
>
> Print, String('journal_', Bin_Date(SysTime()), '.pro', $
> Format='(A, I4, 5I2.2, A)')
Thanks for the idea. I had thought of using a current system time
stamp, but it totally slipped my mind that I could concatenate the
number together so easily. It's little details like that which I'm
still learning. When I finally wrote my code, not only did I take the
date as you did, but I further appended a random number and I did a
check of file existence to ensure that nothing would ever get clobbered.
The only way this could fail would be if there were some insane
once-in-a-lifetime freakish occurrence. Now that I've said this, this
will happen tomorrow. ;-)
> P.S. Bin_Date!? What the heck is that!
The kind of code you get when you give coyotes caffeine late at night?
-Mike
|
|
|
Re: Create unique temporary file [message #40676 is a reply to message #40674] |
Tue, 24 August 2004 22:04   |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
Michael Wallace writes:
> I need some robust way to create a temporary file. The filepath
> function will get you part of the way toward this goal since it can
> prepend your system's temporary directory to the file name. But you
> still have to come up with a good filename.
>
> filename = FilePath('myfile.txt', /TMP)
>
> The above command will do the job if you only have one copy of the
> program running. If there are two or more copies of the program
> running, or even a single program running in multiple threads, there's a
> possibility of collision when two processes or two threads attempt to
> use the same temporary filename at the same time.
>
> What I'd like is some way to generate a completely unique filename
> except for the extension to reduce the probability of a collision. Any
> bright ideas?
Here is how I create a unique journal file name:
Print, String('journal_', Bin_Date(SysTime()), '.pro', $
Format='(A, I4, 5I2.2, A)')
Cheers,
David
P.S. Bin_Date!? What the heck is that!
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
|
|
|
Re: Create unique temporary file [message #40789 is a reply to message #40674] |
Wed, 25 August 2004 17:10  |
Craig Markwardt
Messages: 1869 Registered: November 1996
|
Senior Member |
|
|
Michael Wallace <mwallace.no.spam@no.spam.swri.edu.invalid> writes:
>> Here is how I create a unique journal file name:
>>
>> Print, String('journal_', Bin_Date(SysTime()), '.pro', $
>> Format='(A, I4, 5I2.2, A)')
>
> Thanks for the idea. I had thought of using a current system time
> stamp, but it totally slipped my mind that I could concatenate the
> number together so easily. It's little details like that which I'm
> still learning. When I finally wrote my code, not only did I take the
> date as you did, but I further appended a random number and I did a
> check of file existence to ensure that nothing would ever get clobbered.
> The only way this could fail would be if there were some insane
> once-in-a-lifetime freakish occurrence. Now that I've said this, this
> will happen tomorrow. ;-)
Hey Michael, I'm coming into this late, but I've developed a function
called CMUNIQUE_ID() which generates unique identifiers. I use it all
the time to avoid name clashes. I bases the unique ID on the time,
some randomness, and any other bits you can throw at it. Each time you
ask for another ID, you get a different one. [ It works even if you
ask for two IDs within the same second, which might not work if one
uses SYSTIME() alone. ]
Good luck,
Craig
http://cow.physics.wisc.edu/~craigm/idl/idl.html (under Misc.)
--
------------------------------------------------------------ --------------
Craig B. Markwardt, Ph.D. EMAIL: craigmnet@REMOVEcow.physics.wisc.edu
Astrophysics, IDL, Finance, Derivatives | Remove "net" for better response
------------------------------------------------------------ --------------
|
|
|