Creating Unique Filename? 
Author Message
 Creating Unique Filename?

hi,
I am trying to create a temporary file and does anyone
know if there is any way to create a unique temporary filename?
Thanks
-Paul



Sun, 19 Jul 1998 03:00:00 GMT  
 Creating Unique Filename?

Quote:

>hi,
>I am trying to create a temporary file and does anyone
>know if there is any way to create a unique temporary filename?
>Thanks
>-Paul

Try using numbers in your temp file.  Set the first x letters of the
8.3 format to whatever you want (maybe the first x letters of your app
name).  Then, set the rest of the characters to 0's (e.g
write000.tmp).  Use dir to see if it exists.  If it does, increment
and try again.

Maybe not the best idea, but it's an idea, I guess.  Any other
opinions?

Seeya,
JC



Mon, 20 Jul 1998 03:00:00 GMT  
 Creating Unique Filename?
In DOS there are a INT21h routine that can give you a unique filename.
This is probably the safest way.

Have someone seen it in some VBX or DLL?

Stig



Mon, 20 Jul 1998 03:00:00 GMT  
 Creating Unique Filename?

infinite reservoirs of wisdom:

Quote:
>I am trying to create a temporary file and does anyone
>know if there is any way to create a unique temporary filename?

Use the GetTempFilename API and let Windows do the job.

Jens
--
Everything I said are the opinions of someone else.
I just cut-and-pasted.

Jens Balchen jr.       http://www.sn.no/~balchen



Mon, 20 Jul 1998 03:00:00 GMT  
 Creating Unique Filename?

Quote:

> hi,
> I am trying to create a temporary file and does anyone
> know if there is any way to create a unique temporary filename?

Declare Function GetTempFileName Lib "Kernel" (ByVal cDriveLetter As _
Integer, ByVal lpPrefixString As String, ByVal wUnique As Integer, ByVal _
lpTempFileName As String) As Integer

'create a string of Nulls

    TempFileName$ = String$(255, 0)

' Get a temp file name to expand into...

    a = GetTempFileName(0, "fax" + Chr$(0), 0, TempFileName$)

Where I used "fax" you can use your own string.

HTH

Jeff

--
This is a signature. Its purpose is to tell you who I am and how you can
get in touch with me. By being read, it has done its duty.  It will tell

at my web site: http://w3.gti.net/director/faqindex.html. Go there. Now.



Mon, 20 Jul 1998 03:00:00 GMT  
 Creating Unique Filename?

Quote:

> I am trying to create a temporary file and does anyone
> know if there is any way to create a unique temporary filename?

There is an API call that will do this.  However, I'm not at my office
and don't have access to my docs.  Sorry.

There is also a DOS interrupt that will generate a unique file name.

-chris



Mon, 20 Jul 1998 03:00:00 GMT  
 Creating Unique Filename?

Quote:

>> hi,
>> I am trying to create a temporary file and does anyone
>> know if there is any way to create a unique temporary filename?

>Declare Function GetTempFileName Lib "Kernel" (ByVal cDriveLetter As _
>Integer, ByVal lpPrefixString As String, ByVal wUnique As Integer, ByVal _
>lpTempFileName As String) As Integer

I looked at GetTempFileName for a small project I was working on
several months ago, and got the details from Appleman's Visual Basic
Programmers Guide to the Windows API (or whatever it's called).

Trouble was, it also CREATED the file in the Windows temp directory.
Unfortunately, I wanted to create said file in a specific directory.

I could not find a way to get around this problem. Any offers?

PS: You could always temporarily change the temp directory to your
directory, but if other programs are runnning ... yuk.


 - 'Spock, what is it?'
   'Very bad poetry, Captain' - Catspaw.



Thu, 23 Jul 1998 03:00:00 GMT  
 Creating Unique Filename?

the infinite reservoirs of wisdom:

Quote:
>Trouble was, it also CREATED the file in the Windows temp directory.
>Unfortunately, I wanted to create said file in a specific directory.

So? You got the filename, right? Then what's the problem?

1. Kill the temp file in the \temp dir.
2. Create the file in whatever dir you want.

Jens
--
Everything I said are the opinions of someone else.
I just cut-and-pasted.

Jens Balchen jr.       http://www.sn.no/~balchen



Fri, 24 Jul 1998 03:00:00 GMT  
 Creating Unique Filename?


Quote:

>the infinite reservoirs of wisdom:

Ooh, sarcasm. You're not an IT professional by any chance, are you?

Quote:

>>Trouble was, it also CREATED the file in the Windows temp directory.
>>Unfortunately, I wanted to create said file in a specific directory.

>So? You got the filename, right? Then what's the problem?

>1. Kill the temp file in the \temp dir.
>2. Create the file in whatever dir you want.

Or if you're using 32 bit Visual Basic 4 you could simply use the Name
statement to move it to wherever you want to put it.  I haven't tried
this yet, but I'm reliably informed it ought to work. ;-)

My point is not 'Oh no, I've created a temporary file in the wrong
directory, now whatever can I do?'.

My point is, it would be a jolly useful little API call in it's own
right even if it only returned a name for a file that is unique, and
nothing else. If it goes and creates a zero-length file, why not call
it GetTempFile?

If it assumes you're going to use that file there and then, why not
just return a file handle?

Ciao.


 - 'Spock, what is it?'
   'Very bad poetry, Captain' - Catspaw.



Sat, 25 Jul 1998 03:00:00 GMT  
 Creating Unique Filename?


Quote:
>I looked at GetTempFileName for a small project I was working on
>several months ago, and got the details from Appleman's Visual Basic
>Programmers Guide to the Windows API (or whatever it's called).

>Trouble was, it also CREATED the file in the Windows temp directory.
>Unfortunately, I wanted to create said file in a specific directory.

>I could not find a way to get around this problem. Any offers?

Why not make the GetTempFileName call, parse just the filename out of the
qualified path it returns, and use that?

Then, write a small routine to triple check that *that* file doesn't exist in
your own directory. If it does, re-call GetTempFileName.  If it doesn't,
you're in bid'ness.

Or, since it is your util and your directory, do whatever you want, and keep a
unified naming strategy.  

Name all your temp files, ~0000000x.tmp and everytime you write a tempfile,
increment x by 1.  Then when your app is done, do a Kill c:\YourApp\~*.tmp and
be done with it.

Should your app have crashed and you have left over files, the next time
your app initilizes, check and see what # x was up to and start from there...

Just thinking aloud, haven't tried any of it...

Jeff

This is a signature. Its purpose is to tell you who I am and how you can
get in touch with me. By being here, its duty is accomplished. It tells

at my web site: http://w3.gti.net/director/faqindex.html. Go there. Now.



Sun, 26 Jul 1998 03:00:00 GMT  
 Creating Unique Filename?

Quote:
>I am trying to create a temporary file and does anyone
>know if there is any way to create a unique temporary filename?

Declare Function GetTempFileName% Lib "Kernel" (ByVal cDriveLetter%, ByVal lpPrefixString$, ByVal wUnique%, ByVal lpTempFileName$)
_
We have two seasons in PA: Winter and Construction.

+------------------------------------------------------------+


|Physically in Church Hill, TN - Logically Not Sure          |
+------------------------------------------------------------+

Quote:
>>SQUID - The ultimate database reader, and NO limits. #$737961

**Special Compile: 1.033B (Beta)


Sun, 26 Jul 1998 03:00:00 GMT  
 Creating Unique Filename?

the infinite reservoirs of wisdom:

Quote:

>>the infinite reservoirs of wisdom:
>Ooh, sarcasm. You're not an IT professional by any chance, are you?

No. Well, yes. Naah, that depends on what a pro is. Besides, it
isn't sarcasm, it's a standard header.

Quote:
>Or if you're using 32 bit Visual Basic 4 you could simply use the Name
>statement to move it to wherever you want to put it.  I haven't tried
>this yet, but I'm reliably informed it ought to work. ;-)

Name won't work across disks, so a kill and a filecopy is better.

Quote:
>My point is, it would be a jolly useful little API call in it's own
>right even if it only returned a name for a file that is unique, and
>nothing else. If it goes and creates a zero-length file, why not call
>it GetTempFile?

I agree, it's a stupid practice to create the file as well.

Jens
--
Everything I said are the opinions of someone else.
I just cut-and-pasted.

Jens Balchen jr.       http://www.sn.no/~balchen



Sun, 26 Jul 1998 03:00:00 GMT  
 Creating Unique Filename?


Quote:

>the infinite reservoirs of wisdom:


>>>the infinite reservoirs of wisdom:

Twice infinity, even. Gotta love it.

Quote:

>>Ooh, sarcasm. You're not an IT professional by any chance, are you?
>No. Well, yes. Naah, that depends on what a pro is. Besides, it
>isn't sarcasm, it's a standard header.

Okay, standard sarcastic/jokey header. Fair enough.

Quote:
>>Or if you're using 32 bit Visual Basic 4 you could simply use the Name
>>statement to move it to wherever you want to put it.  I haven't tried
>>this yet, but I'm reliably informed it ought to work. ;-)

>Name won't work across disks, so a kill and a filecopy is better.

Oh, don't believe me, eh? I feel a quote coming on ...

'Politically correct file operations

Deleting, copying, renaming, and moving files should be simple, right?
Basic provides the Kill, FileCopy, and Name statements for the first
three operations, and it's easy to write a FileMove using the other
three. In fact, you don't even need to do that in 32 bit mode because,
contrary to documentation, the Name statement can move files across
disks.'

Page 562, {*filter*} VISUAL BASIC by Bruce McKinney.
Microsoft Press, ISBN 1-55615-667-7

As I said - haven't tried it myself, but I'm reliably informed it
ought to work. ;-)

I might give it a go tonight, actually. See if the book is right.
Being an MS book, one would hope so, right?

Quote:
>>My point is, it would be a jolly useful little API call in it's own
>>right even if it only returned a name for a file that is unique, and
>>nothing else. If it goes and creates a zero-length file, why not call
>>it GetTempFile?

>I agree, it's a stupid practice to create the file as well.

I'm glad we agree.

Trouble with Kill and FileCopy, is that Windows only checks it is
unique by checking the Temp directory. As another poster points out,
for complete safety once you had the filename you would have to verify
that the temp file didn't exist in the directory you wanted to place
the temp file. If it did, you would have to either delete it (which
depending on the app you might not want to do), or you have to call
GetTempFilename again. I sense an unnecessary iterative process
here...

The best solution is to implement a unique filename generator
yourself.

But wouldn't it have been nice if that was what GetTempFileName
actually did?

 - 'Spock, what is it?'
   'Very bad poetry, Captain' - Catspaw.



Sun, 26 Jul 1998 03:00:00 GMT  
 Creating Unique Filename?

the infinite reservoirs of wisdom:

Quote:
>In fact, you don't even need to do that in 32 bit mode because,
>contrary to documentation, the Name statement can move files across
>disks.'

Sorry, didn't see the "32" in the last post.

Jens
Jens
--
Everything I said are the opinions of someone else.
I just cut-and-pasted.

Jens Balchen jr.       http://www.sn.no/~balchen



Mon, 27 Jul 1998 03:00:00 GMT  
 Creating Unique Filename?
What about the Win31 API function:

Declare Function GetTempFileName Lib "Kernel" (ByVal cDriveLetter as Integer, ByVal lpPrefixString As String, ByVal wUnique As Integer, ByVal lpTempFileName As String) As Integer

_________

Quote:


> >hi,
> >I am trying to create a temporary file and does anyone
> >know if there is any way to create a unique temporary filename?
> >Thanks
> >-Paul

> Try using numbers in your temp file.  Set the first x letters of the
> 8.3 format to whatever you want (maybe the first x letters of your app
> name).  Then, set the rest of the characters to 0's (e.g
> write000.tmp).  Use dir to see if it exists.  If it does, increment
> and try again.

> Maybe not the best idea, but it's an idea, I guess.  Any other
> opinions?

> Seeya,
> JC




Mon, 27 Jul 1998 03:00:00 GMT  
 
 [ 17 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Should GetTempFileName() always return a unique filename?

2. Unique Filename

3. HowTo ensure uploaded file filenames have unique names?

4. Use time for unique filename

5. Unique filename...

6. Unique filenames...

7. generating multiple unique filenames in a loop

8. Unique Problem, needing Unique Solution.

9. Create Unique Auto Number

10. Using SQL to create 2 Primary/Unique Keys in a Table

11. create unique index while linking server table

12. Creating and maintaining user defined unique IDs

 

 
Powered by phpBB® Forum Software