CodeItBetter Programming Another VB Programming Blog

How to Create a temporary file

Posted on January 5, 2009
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
'System & API - How to Create a temporary file
Option Explicit
 
Private Declare Function GetTempFileName Lib "kernel32" Alias "GetTempFileNameA" _
    (ByVal lpszPath As String, ByVal lpPrefixString As String, ByVal wUnique As Long, _
    ByVal lpTempFileName As String) As Long
Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" _
    (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
 
' Generate a temporary file (path)\Tow????.TMP, where (path)
' is Windows's temporary file directory and ???? is a randomly assigned unique value.
' Then display the name of the created file on the screen.
Dim temppath As String  ' receives name of temporary file path
Dim tempfile As String  ' receives name of temporary file
Dim slength As Long  ' receives length of string returned for the path
Dim lastfour As Long  ' receives hex value of the randomly assigned ????
Dim tmpFileName As String
 
Private Sub Command1_Click()
    ' Get Windows's temporary file path
    temppath = Space(255)  ' initialize the buffer to receive the path
    slength = GetTempPath(255, temppath)  ' read the path name
    temppath = Left(temppath, slength)  ' extract data from the variable

    ' Get a uniquely assigned random file
    tempfile = Space(255)  ' initialize buffer to receive the filename
    lastfour = GetTempFileName(temppath, "Tow", 0, tempfile)  ' get a unique temporary file name
    ' (Note that the file is also created for you in this case.)
    tempfile = Left(tempfile, InStr(tempfile, vbNullChar) - 1)  ' extract data from the variable
    Label1.Caption = tempfile    '"Temporary filename: " & tempfile & " has been created."
    tmpFileName = tempfile
End Sub
 
Private Sub Command2_Click()
    Label1.Caption = tempfile & " Has been deleted from your system."
    Kill tmpFileName
End Sub

Related posts:

  1. How to Get windows temp path
  2. How to Open Text File with Notepad
  3. How to return the create date/time, last accessed date/time, last modified date/time of a given file
  4. How to Create an Error Log file
  5. How to extract associated Icon from EXE file
  6. How to I create a text file from part of another text file (for given line numbers like Line No. 2 to 7)
  7. How to get the file path for the selected file using Common Dialog control
  8. How to pass the DOS commands directly and output the results to a file.
  9. How to create Formless multimedia control software
  10. How to create an Excel file from your application

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


No trackbacks yet.