Windows temp directory details (Java)
Asked Answered
B

7

9

I'm writing a program that needs a generic temp folder. I'm trying to find details about the Windows Temp folders. There are two paths that I know about -

  1. In each user directory under AppData\Local\Temp\ This may change depending Windows version?

  2. In the system folder under Temp\ (C:\Windows\Temp)

I'm wondering exactly what Windows does to each of these. If Windows deletes files from either location, when does it do so? How can/should I use these directories for my programming?

EDIT: I have a bigger problem actually - Because of a certain engine I'm running indirectly with my program, which uses files I'm creating in a temp directory, I need a temp directory that doesn't use whitespace characters in the path. Java's System.getProperty("java.io.tmpdir") on Windows gives me the temp that's in the user directory, which on XP is under "Documents and Settings..." Not good. Any suggestions? This is why I'm wondering about the C:\Windows\Temp\ directory...

Barter answered 9/8, 2010 at 3:6 Comment(0)
B
3

Not quite. There is a user and system folder, the default location of which varies according the windows version, system folder name, and indeed in older versions of windows was the same for both the user and system case. However, these defaults can be over-ridden (they are on the system I'm using now, where they aren't on the same drive as the system folder).

The locations are stored in system variables. Some frameworks (.NET, VB6 and no doubt others) give you convient ways to find the paths rather than having to look up the system variable (e.g. System.IO.Path.GetTempPath in .NET).

Windows does not clean up the temporary folder for you (which is why it's worth blasting out old files it every few months on your own machine), it's up to you to play nice. Create a file or files unlikely to step on the names any other software is using (they should take care to do the same, and so any name should do, but it's always good to assume the worse of other code on the system), and delete files when you're done (or on application exit at least).

In .NET System.IO.Path.GetTempFileName() will create a new file in the temp area and return the name of it to you, that is reasonably guaranteed not to conflict with others' so use that or similar methods if you can.

Buitenzorg answered 9/8, 2010 at 3:23 Comment(4)
I'm pretty sure Windows empties the C:\Windows\Temp\ folder on reboot... or at least it seems that way. Can anyone confirm this?Barter
@Tony R - it's configuration dependent. Most sites I have worked at don't clear tmp on reboot (but some do).Barring
System.IO.Path.GetTempFileName() is File.createTempFile(...) in Java.Milkweed
@Tony R. I'm very sure it doesn't always succeed, given the times I've gone to clean up drives and found them with temp folders full of very very old files! :)Buitenzorg
N
10

This will give you the path to the windows temp directory in Java.

File.createTempFile("temp-file", "tmp").getParent()
Newsman answered 28/5, 2012 at 3:8 Comment(0)
B
3

Not quite. There is a user and system folder, the default location of which varies according the windows version, system folder name, and indeed in older versions of windows was the same for both the user and system case. However, these defaults can be over-ridden (they are on the system I'm using now, where they aren't on the same drive as the system folder).

The locations are stored in system variables. Some frameworks (.NET, VB6 and no doubt others) give you convient ways to find the paths rather than having to look up the system variable (e.g. System.IO.Path.GetTempPath in .NET).

Windows does not clean up the temporary folder for you (which is why it's worth blasting out old files it every few months on your own machine), it's up to you to play nice. Create a file or files unlikely to step on the names any other software is using (they should take care to do the same, and so any name should do, but it's always good to assume the worse of other code on the system), and delete files when you're done (or on application exit at least).

In .NET System.IO.Path.GetTempFileName() will create a new file in the temp area and return the name of it to you, that is reasonably guaranteed not to conflict with others' so use that or similar methods if you can.

Buitenzorg answered 9/8, 2010 at 3:23 Comment(4)
I'm pretty sure Windows empties the C:\Windows\Temp\ folder on reboot... or at least it seems that way. Can anyone confirm this?Barter
@Tony R - it's configuration dependent. Most sites I have worked at don't clear tmp on reboot (but some do).Barring
System.IO.Path.GetTempFileName() is File.createTempFile(...) in Java.Milkweed
@Tony R. I'm very sure it doesn't always succeed, given the times I've gone to clean up drives and found them with temp folders full of very very old files! :)Buitenzorg
B
1

It sounds like you have two programs that need to share temp files and one definitely doesn't want spaces in the path name. Probably the easiest thing to do is:

  1. set the TMP and TEMP variable to a common directory
  2. launch each application (from this modified environment) - which should pick up the temp variable

So at the command prompt you could do this:

  1. set TMP=c:\mytemp
  2. set TEMP=c:\mytemp
  3. java -cp x;y;z my.application.Entry
  4. run other application (hopefully it also reads the environment for temp/tmp)

Hope that helps.

Barring answered 9/8, 2010 at 7:50 Comment(2)
You're going to change the way a system works just to make one application work? What happens if someone "fixes" the temp folder location. If these applications have specific needs for temporary folders above and beyond that normal for temp folders, I'd create their own temp folder and use that instead of the normal ones, or use the "MS-DOS Compatible" version of the path, so there will be no spaces.Buitenzorg
No need to change anything at system level. The suggestion would work fine from a batch script or cmd shell and have only a localised impact.Barring
L
0

To answer part of your question - if you're using .NET, you can use the Path.GetTempPath() method of the System.IO namespace to get the location of the temporary directory.

// Get the path of the temporary directory
string tempDir = Path.GetTempPath();

// "Creates a uniquely named, zero-byte temporary file on disk and returns the full path of that file."
string tempFile = Path.GetTempFileName();
Lianaliane answered 9/8, 2010 at 3:15 Comment(1)
Thanks =D. I'm using Java, so I can get the temp path that's in the user directory with System.getPropterty(...)Barter
C
0

The %TEMP% environment variable that's defined on my PC (XP SP3) uses the DOS-style abcdef~1 directory names - hence, if you can pull that variable, you should end up with a path without spaces.

e.g. Start>Run>%TEMP% takes me to C:\DOCUME~1\<user>\LOCALS~1\Temp

However, if a 'super-user' fiddles around with that variable and points it somewhere else, it's possible that things will fall over. You could look at something like this to retrieve the 8-char-and-no-spaces path.

Culvert answered 9/8, 2010 at 4:21 Comment(0)
M
0

use this code

   try {    String s=File.createTempFile("temp-file", "tmp").getParent();
            System.out.println(s);

        } catch (IOException ex) {
            Logger. getLogger(Result.class.getName()).log(Level.SEVERE, null, ex);
            }
Marteena answered 10/9, 2014 at 5:0 Comment(0)
K
0

you can try this way

System.out.println(File.createTempFile("temp-file", "tmp").getParent());
String property = "java.io.tmpdir";
String tempDir = System.getProperty(property);
System.out.println("OS current temporary directory is " + tempDir);
Kosse answered 6/8, 2018 at 4:40 Comment(1)
Try adding what this code is doing also, so that future reader understands.Ish

© 2022 - 2024 — McMap. All rights reserved.