Announcement

Collapse
No announcement yet.

Unique Temp File creation using WIndows API

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Scott Turchin
    replied
    Point well taken

    I did set my TEMP and TMP in my autoexec to \\TNG\D-DRIVE\TEMP (valid dir) and tested, worked 15 times without a hitch..
    Left 15 temp files in BUT...
    For the sake of arguement and good programming practice (Point well taken! hehe )


    Code:
    #Compile Exe
    #Include "win32api.inc"
    
    Declare Function GetNewTempFile()As String
    Declare Function GetWindowsTempDir() As String
    Declare Function Exist(ByVal filespec As String)  As Long
    
    
    Function PbMain() As Long
    MsgBox GetNewTempFile
    End Function
    
    
    '========================================================================================
    Function GetNewTempFile() Export As String
    Local lpszPath          As Asciiz * %MAX_PATH
    Local lpTempFileName    As Asciiz * %MAX_PATH
    Local lpszPrefix        As Asciiz * 20
    Local r                 As Long
    lpszPrefix = "CCS"
    r = GetTempFileName(GetWindowsTempDir, lpszPrefix, 0, lpTempFileName)
    If IsTrue Exist(lpTempFileName) Then
        Function = lpTempFileName
    Else
       'WinAPI indicates if "." is present then use currente applications directory
        r = GetTempFileName(".", lpszPrefix, 0, lpTempFileName)
        Function = lpTempFileName
    End If
    End Function
    '========================================================================================
    Function GetWindowsTempDir()Export As String
    'SPT 1/22/01
    Local lResult As Long
    Local buff As Asciiz * %MAX_PATH
    lResult = GetTempPath(ByVal SizeOf(buff), Buff)
    Function = Trim$(buff)
    End Function
    '========================================================================================
    Function Exist(ByVal filespec As String) Export As Long
      Local hDir     As Long
      Local FindData As WIN32_FIND_DATA
    
      FindData.dwFileAttributes = %FILE_ATTRIBUTE_DIRECTORY
      hDir = FindFirstFile(ByVal StrPtr(filespec), FindData)
      If hDir = %INVALID_HANDLE_VALUE Then
        Exit Function     'file not found
      End If
      Function = %TRUE
      FindClose hDir
    End Function
    
    '========================================================================================
    ------------------
    Scott
    mailto:[email protected][email protected]</A>

    [This message has been edited by Scott Turchin (edited January 22, 2001).]

    Leave a comment:


  • Tom Hanlin
    replied
    It would be advisable to use %MAX_PATH for all your filespecs.

    It might be a good idea to check that the returned filespec is actually
    valid. The problem is, under some versions of Windows, you can get back
    invalid temporary filenames if the user's temporary file path is in the
    form \\server\share. In that case, the GetTempPath API can return
    garbaged filespecs without so much as an error code, so you might
    want to do a validity check and fall back on, say, your program's own
    path if necessary.


    ------------------
    Tom Hanlin
    PowerBASIC Staff

    Leave a comment:


  • Scott Turchin
    replied
    Oops!
    I meant to post this in the source code forum!!


    Scott

    ------------------
    Scott
    mailto:[email protected][email protected]</A>

    Leave a comment:


  • Scott Turchin
    started a topic Unique Temp File creation using WIndows API

    Unique Temp File creation using WIndows API

    I need to create a UNIQUE temp file name, one that does not exist in the temp directory.
    I recently ran into a piece of code that was a work of art, but I thought that i could perhaps shorten it down by about 12 to 15 lines of code by using Windows to do the work for me

    Please note!!! Windows will NOT clean this file up, it is your responsibility to do so.
    When the API is complete a zero byte file will exist, closed, ready for your use.

    Code:
    #Compile Exe
    #Include "win32api.inc"
    
    Declare Function GetNewTempFile()As String
    Declare Function GetWindowsTempDir() As String
    
    
    Function PbMain() As Long
    MsgBox GetNewTempFile
    End Function
    
    
    '========================================================================================
    Function GetNewTempFile() Export As String
    Local lpszPath          As Asciiz * %MAX_PATH
    Local lpTempFileName    As Asciiz * 120
    Local lpszPrefix        As Asciiz * 20
    Local r                 As Long
    lpszPrefix = "CCS"
    r = GetTempFileName(GetWindowsTempDir, lpszPrefix, 0, lpTempFileName)
    Function = lpTempFileName
    End Function
    '========================================================================================
    Function GetWindowsTempDir()Export As String
    'SPT 1/22/01
    Local lResult As Long
    Local buff As Asciiz * 255
    lResult = GetTempPath(ByVal SizeOf(buff), Buff)
    Function = Trim$(buff)
    End Function
    '========================================================================================
    ------------------
    Scott
    mailto:[email protected][email protected]</A>
Working...
X