- How to get the Windows temporary directory
- TEMP environment variable
- GetTempPath API function
- Windows Temporary Files – Everything you want to know
- Windows Temporary Files
- What are Temporary Files in Windows 10
- Why are Temporary Files created
- Temporary Files Location
- Change the location of the Temp folder
- Empty Temporary Files folder
- Getting user temporary folder path in Windows
- 9 Answers 9
- Creating a temporary directory in Windows?
- 9 Answers 9
- How to get temporary folder for current user
- 4 Answers 4
How to get the Windows temporary directory
There are at least two ways to get the Windows temporary directory folder (i.e. that folder in which by convention applications may freely create temporary files):
TEMP environment variable
The TEMP environment variable dates back to the pre-windows days of DOS, but is still used and available in windows applications. By convention both the variables TMP and TEMP indicate the temporary directory. The values of these variables can be easily read using the GetEnvironmentVariable function, i.e.:
- You will need to include SysUtils to use the GetEnvironmentVariable function. The version in SysUtils places a wrapper around the API version of the function defined in the unit Windows (which takes different parameters).
GetTempPath API function
The Windows API function GetTempPath returns the path to the Windows temporary file folder. The GetTempPath function is defined in the Windows unit and its definition looks like:
function GetTempPath(nBufferLength: Cardinal; lpBuffer: PChar): Cardinal;
This takes a buffer and the size of the buffer and writes the temporary folder into the buffer, returning the number of characters written to the buffer.
Personally I don’t find this useful, because I prefer to be able to assign the value to a variable directly. For this reason I use a wrapper function:
This lets you do more intuitive assignments such as
- GetTempPath and MAX_PATH are defined in the unit Windows.
- StrPas is defined in the unit SysUtils .
These notes are believed to be correct for Delphi 6 and Delphi 7 and may apply to other versions as well.
About the author: Brian Cryer is a dedicated software developer and webmaster. For his day job he develops websites and desktop applications as well as providing IT services. He moonlights as a technical author and consultant.
Windows Temporary Files – Everything you want to know
We have already seen the nature of some temporary files like Temporary Internet Files, Index.dat file, Cookies, and Prefetch Files. In this article, we shall have a look at a few aspects of the Windows Temporary Files, which your computer creates, during the course of its normal running.
Windows Temporary Files
What are Temporary Files in Windows 10
Temporary Files in Windows are those junk files whose use is only temporary and become redundant once the task in hand is completed. Such temporary files are created to hold data temporarily while a file is being created or processed or used.
Why are Temporary Files created
Windows Temporary files are created by the operating system during the normal course of its running when there may not be enough memory allocated for the task.
Software that uses large amounts of data like Graphics, Video, or Media editing software also creates temporary files. Such created temporary files are more often than not, left behind even when the task is over, leading to their wasting disk space.
Temporary Files are also created for backup purposes, by programs. For instance, Microsoft Office saves a Temporary File of the open document every few minutes. If you save the document and exit, the Temporary File gets deleted. If the program crashes unexpectedly, the Temporary File is not deleted. They can thus be useful to help recover lost data if the program or the system crashes.
Ideally, the Temporary Files should get deleted once the program exits. But this is not always the case, leading to wasted disk space.
Temporary Files Location
The Temporary Files in Windows are typically found located in two locations:
- %systemdrive%\Windows\Temp
- %userprofile%\AppData\Local\Temp
If you click on C:\Windows\Temp you will receive a message You don’t currently have permission to access this folder. Click on Continue to do so. You will see that most of its contents are .tmp, .temp and .txt files.
The other folder typically located at C:\Users\username\AppData\Local\Temp, is created for each User. It’s a hidden folder and you will have to first ‘un-hide’ System folders from the Folder Options before you can see it.
The Temporary files created by the Windows operating system are usually stored in the %system%\Windows\Temp folder, whereas the ones created by the User when running any software is stored in his user profile at %userprofiles%\AppData\Local\.
Temporary Files of a particular software may also be located in a sub-folder, inside the parent folder of the particular software.
On rare occasions, a temporary file or a temporary files folder may get created into the root directory of the C (System) drive. You may want to examine the folder in details, and then delete it if you are sure, if it indeed contains temporary files.
Change the location of the Temp folder
If you wish, you can change the location of the Temporary Files folder. To do so, open System Properties via the Control Panel > Environment Variables > Edit the System and/or the User variables as you wish.
But do remember that it is never a good idea to combine the temporary directories together for all the user profiles, for security reasons, since there have been cases of security vulnerabilities with temporary files, due to a particular software’s incorrect file permissions or race conditions.
Empty Temporary Files folder
There are several different ways to delete Temporary Files. You can use freeware junk file cleaners or the in-built Disk Cleanup utility to easily empty the contents of the Temp folders.
Planning to empty the contents of your Windows Installer Folder or WinSxS directory due to its sheer large size!? Think again!
Date: February 5, 2018 Tags: Files, Folder
Getting user temporary folder path in Windows
How I can get the user’s temp folder path in C++? My program has to run on Windows Vista and XP and they have different temp paths. How I can get it without losing compatibility?
9 Answers 9
Is there a reason you can’t use the Win32 GetTempPath API?
This API is available starting with W2K and hence will be available on all of your listed targets.
Since C++ 17 you can use a cross-platform function: std::filesystem::temp_directory_path()
The GetTempPath function retrieves the path of the directory designated for temporary files. This function supersedes the GetTempDrive function.
Parameters
Specifies the size, in characters, of the string buffer identified by lpBuffer.
Points to a string buffer that receives the null-terminated string specifying the temporary file path.
Return Values
If the function succeeds, the return value is the length, in characters, of the string copied to lpBuffer, not including the terminating null character. If the return value is greater than nBufferLength, the return value is the size of the buffer required to hold the path. If the function fails, the return value is zero. To get extended error information, call GetLastError.
Remarks
The GetTempPath function gets the temporary file path as follows:
- The path specified by the TMP environment variable.
- The path specified by the TEMP environment variable, if TMP is not defined.
- The current directory, if both TMP and TEMP are not defined.
Creating a temporary directory in Windows?
What’s the best way to get a temp directory name in Windows? I see that I can use GetTempPath and GetTempFileName to create a temporary file, but is there any equivalent to the Linux / BSD mkdtemp function for creating a temporary directory?
9 Answers 9
No, there is no equivalent to mkdtemp. The best option is to use a combination of GetTempPath and GetRandomFileName.
You would need code similar to this:
57 bits. You can always make two calls to square it – Marty Neal Jul 25 ’13 at 17:05
I hack Path.GetTempFileName() to give me a valid, pseudo-random filepath on disk, then delete the file, and create a directory with the same file path.
This avoids the need for checking if the filepath is available in a while or loop, per Chris’ comment on Scott Dorman’s answer.
If you truly need a cryptographically secure random name, you may want to adapt Scott’s answer to use a while or do loop to keep trying to create a path on disk.
I like to use GetTempPath(), a GUID-creation function like CoCreateGuid(), and CreateDirectory().
A GUID is designed to have a high probability of uniqueness, and it’s also highly improbable that someone would manually create a directory with the same form as a GUID (and if they do then CreateDirectory() will fail indicating its existence.)
@Chris. I too was obsessed with the remote risk that a temporary directory might already exist. The discussions about random and cryptographically strong don’t completely satisfy me either.
My approach builds on the fundamental fact that the O/S must not allow 2 calls to create a file to both succeed. It is a little surprising that .NET designers chose to hide the Win32 API functionality for directories, which makes this much easier, because it does return an error when you attempt to create a directory for the second time. Here is what I use:
You get to decide whether the «cost/risk» of unmanaged p/invoke code is worth it. Most would say it is not, but at least you now have a choice.
How to get temporary folder for current user
Currently I am using following function to get the temporary folder path for current user:
On some machines it gives me temp folder path of current user like:
C:\Documents and Settings\administrator\Local Settings\Temp\
On some machines it gives me system temp folder path like:
MSDN Documentation also says that above API returns current system’s temporary folder.
Is there any other API available which gives me current user’s temporary folder path like this:
C:\Documents and Settings\administrator\Local Settings\Temp\
4 Answers 4
System.IO.Path.GetTempPath() is just a wrapper for a native call to GetTempPath(..) in Kernel32.
Copied from that page:
The GetTempPath function checks for the existence of environment variables in the following order and uses the first path found:
- The path specified by the TMP environment variable.
- The path specified by the TEMP environment variable.
- The path specified by the USERPROFILE environment variable.
- The Windows directory.
It’s not entirely clear to me whether «The Windows directory» means the temp directory under windows or the windows directory itself. Dumping temp files in the windows directory itself sounds like an undesirable case, but who knows.
So combining that page with your post I would guess that either one of the TMP, TEMP or USERPROFILE variables for your Administrator user points to the windows path, or else they’re not set and it’s taking a fallback to the windows temp path.