Security measures in Windows prevents programs from writing files to all areas. Use the Windows API to retrieve the user's temporary directory and to create a temporary filename that is unlikely to be used by another application.
GetTempPathA
To insure that your program can successfully write a temporary file on the user's computer, use the function GetTempPathA to retrieve the path to a directory where temporary files should be created.
GetTempFileNameA
The GetTempFileNameA function allows you to create a unique temporary filename. The name is in this format:
An example that uses GetTempPathA for the temporary path and "xyz" for the prefix might look like this:
C:\DOCUME~1\MYNAME~1\LOCALS~1\Temp\xyzDA7.tmp
The function looks like this:
calldll#kernel32,"GetTempFileNameA",_
TempPath$ asptr,_ 'directory for temp file
prefix$ asptr,_ 'desired prefix for temp filename
unique asulong,_ '0=file created,nonzero=you must create file
TempFile$ asptr,_ 'string buffer to hold qualified path and filename
result asulong'nonzero=success
The TempPath$ string can contain any directory. It can be the DefaultDir$, for instance. Best practice dictates that you use the GetTempPathA function to retrieve the user's temporary directory.
The prefix$ argument should be one to three characters long. Some examples:
abc
rgf
aa
b
xyz
zy
The "unique" argument tells the function whether to create the file, or to simply return the filename. If it is 0, the function creates the file, thus assuring its existence. If the value for unique is nonzero, the programmer must create the file himself.
TempFile$ should be a string buffer of blank spaces that will receive the temporary filename returned by the function. This is a fully qualified path and filename, as in the example above.
Best practice dictates that you should delete your temporary file when your program closes, or when it is no longer needed. Use the KILL statement to do this.
Demo
This demo program retrieves the path to the temporary directory. It created a prefix of "xyz". It creates a string variable full of blank spaces that will hold the fully qualified path and filename to the temporary file when the function returns.
The demo does not write anything to the file, nor does it delete the file on exit. You will do both of these things when you include the code in your program.
TempPath$=GetTempPath$()
prefix$="xyz"'up to 3 characters for desired prefix
TempFile$ =space$(256)+chr$(0)calldll#kernel32,"GetTempFileNameA",_
TempPath$ asptr,_ 'directory for temp file
prefix$ asptr,_ 'desired prefix for temp filename0asulong,_ '0=file created,nonzero=you must create file
TempFile$ asptr,_ 'string buffer to hold qualified path and filename
result asulong'nonzero=success'TempFile$ holds complete path and filename infoprint TempFile$
endFunction GetTempPath$()CallDLL#kernel32,"GetTempPathA",_
0aslong,_
_NULL aslong,_
length aslong
buf$ =space$(length)CallDLL#kernel32,"GetTempPathA",_
length aslong,_
buf$ asptr,_
ret aslong
GetTempPath$ = buf$
EndFunction
Creating a Name for Temporary Files
-Creating a Name for Temporary Files | Writing Temporary Files | GetTempPathA | GetTempFileNameA | Demo
Writing Temporary Files
Security measures in Windows prevents programs from writing files to all areas. Use the Windows API to retrieve the user's temporary directory and to create a temporary filename that is unlikely to be used by another application.GetTempPathA
To insure that your program can successfully write a temporary file on the user's computer, use the function GetTempPathA to retrieve the path to a directory where temporary files should be created.GetTempFileNameA
The GetTempFileNameA function allows you to create a unique temporary filename. The name is in this format:TemporaryPath$ + ThreeCharPrefix + HexadecimalString$ + ".TMP"
An example that uses GetTempPathA for the temporary path and "xyz" for the prefix might look like this:
C:\DOCUME~1\MYNAME~1\LOCALS~1\Temp\xyzDA7.tmp
The function looks like this:
The TempPath$ string can contain any directory. It can be the DefaultDir$, for instance. Best practice dictates that you use the GetTempPathA function to retrieve the user's temporary directory.
The prefix$ argument should be one to three characters long. Some examples:
abc
rgf
aa
b
xyz
zy
The "unique" argument tells the function whether to create the file, or to simply return the filename. If it is 0, the function creates the file, thus assuring its existence. If the value for unique is nonzero, the programmer must create the file himself.
TempFile$ should be a string buffer of blank spaces that will receive the temporary filename returned by the function. This is a fully qualified path and filename, as in the example above.
Best practice dictates that you should delete your temporary file when your program closes, or when it is no longer needed. Use the KILL statement to do this.
Demo
This demo program retrieves the path to the temporary directory. It created a prefix of "xyz". It creates a string variable full of blank spaces that will hold the fully qualified path and filename to the temporary file when the function returns.The demo does not write anything to the file, nor does it delete the file on exit. You will do both of these things when you include the code in your program.
See also: GetTempPathA
Creating a Name for Temporary Files | Writing Temporary Files | GetTempPathA | GetTempFileNameA | Demo