Sign in to follow this  
Followers 0
HansH

Adapted function _TempFile

4 posts in this topic

I changed the function _TempFile in FILE.au3 to accept some parameters to have more control where and how the tempfile is created.

The new parameters are optional, so previous use of the function will not break

New code:

;===============================================================================
;
; Function Name:    _TempFile([s_DirectoryName],[s_FilePrefix])
; Description:    Generate a name for a temporary file. The file is guaranteed
;                  not to already exist in the user's %TEMP% or given directory.
; Parameter(s): 
;    $s_DirName optional  Name of directory for filename, defaults to @TempDir
;    $s_FilePrefix optional  File prefixname, defaults to ""
; Requirement(s):   None.
; Return Value(s):  Filename of a temporary file which does not exist.
; Author(s):        Dale (Klaatu) Thompson and Hans Harder
; Notes:            None.
;
;===============================================================================
Func _TempFile($s_DirectoryName=@Tempdir, $s_FilePrefix="")
    Local $s_TempName

    ; add trailing \ for directory name
    If StringRight($s_DirectoryName,1) <> "\" Then $s_DirectoryName &= "\"
        
    Do
        $s_TempName = "~"
        While StringLen($s_TempName) < 8
            $s_TempName = $s_TempName & Chr(Round(Random(97, 122), 0))
        Wend
        $s_TempName = $s_DirectoryName & $s_FilePrefix & $s_TempName & ".tmp"
    Until Not FileExists($s_TempName)
    
    Return ($s_TempName)
EndFunc

New example:

#include <File.au3>

Dim $s_TempFile, $s_FileName

; generate unique filename in @TempDir
$s_TempFile = _TempFile()

; generate unique filename in given directory and starting with tst_
$s_FileName = _TempFile("C:\", "tst_")

MsgBox(4096, "Info", "Names suitable for new temporary file : " & @LF & $s_TempFile & @LF & $s_FileName)

Exit

If there any remarks, please let me know.

Share this post


Link to post
Share on other sites



Lookin' good to me ;)


"I thoroughly disapprove of duels. If a man should challenge me, I would take him kindly and forgivingly by the hand and lead him to a quiet place and kill him." - Mark TwainPatient: "It hurts when I do $var_"Doctor: "Don't do $var_" - Lar.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Hehe, there are so many ways to rewrite this function it's not even funny. I added the same extra parameters to my version of this function that you did (although to be fair, I actually wrote mine from scratch because well... I didn't know this function already existed). My parameters were actually: directory (which was required in mine, didn't think to have it default to tempdir), extension, prefix, and length parameters.

*Edit: Oops, submitted this before I was done...

Anyway, I was just going to say that in my function I used something like

$tempName = Hex(Random(0, 0xffff, 1), 4)

For the random name generation. Sure you don't end up with characters beyond F, but since it's just a random name anyway, does it really matter?

(Here's my current incarnation actually)

Func _UniqueFile($s_Directory = @Tempdir, $s_Extension = 'tmp', $s_Prefix = '~', $i_Len = 8)
    Local $s_FileName
    
    Do
        $s_FileName = $s_Prefix
        While StringLen($s_FileName) < $i_Len
            $s_FileName &= Hex(Random(0, 0xffff, 1), 4)
        WEnd
        If StringLen($s_FileName) > $i_Len Then
            $s_FileName = StringLeft($s_FileName, $i_Len)
        EndIf
        $s_FileName &= '.' & $s_Extension
    Until Not FileExists($s_Directory & '\' & $s_FileName)

    Return $s_FileName
EndFunc
Edited by Saunders

Share this post


Link to post
Share on other sites

Hehe, there are so many ways to rewrite this function it's not even funny. I added the same extra parameters to my version of this function that you did (although to be fair, I actually wrote mine from scratch because well... I didn't know this function already existed). My parameters were actually: directory (which was required in mine, didn't think to have it default to tempdir), extension, prefix, and length parameters.

$tempName = Hex(Random(0, 0xffff, 1), 4)

For the random name generation. Sure you don't end up with characters beyond F, but since it's just a random name anyway, does it really matter?

<{POST_SNAPBACK}>

Interesting, did some changes:

- added extension and length of the random generation to it

- changed the prefix defaulting to "~"

- default extension is including the .

- random generation is a little changed, I noticed it did float and then round, so I changed it to integers which saves a function call

Current random generation gives a little more randomize than the hex way, which has an advantage for shorter filenames.

I specifcally added a check for an ending \ in the directory name, to avoid users using it as _TempFile("c:\")

Func _TempFile($s_DirectoryName=@Tempdir, $s_FilePrefix="~", $s_FileExtension=".tmp", $i_RandomLength=6)
    Local $s_TempName

   ; add trailing \ for directory name
    If StringRight($s_DirectoryName,1) <> "\" Then $s_DirectoryName &= "\"
        
    Do
        $s_TempName = ""
        While StringLen($s_TempName) < $i_RandomLength
            $s_TempName &= Chr(Random(97, 122,1))
        Wend
        $s_TempName = $s_DirectoryName & $s_FilePrefix & $s_TempName & $s_FileExtension
    Until Not FileExists($s_TempName)
    
    Return ($s_TempName)
EndFunc

Better, or keep the first version ?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0