Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!

Idiom #139 Create temp directory

Create a new temporary folder on filesystem, for writing.

import tempfile	
td = tempfile.TemporaryDirectory()
using System.IO;
string newDir = Path.GetTempPath() + Guid.NewGuid();
Directory.CreateDirectory(newDir);
string mkTmpDir()
{
    import std.uuid, std.file, std.path;
    string d = buildPath(tempDir, randomUUID.toString);
    if (!d.exists)
    {
        mkdir(d);
        return d;
    }
    else return null;
}

string s;
while (!s.length)
  s = mkTmpDir;
import "os"
dir, err := os.MkdirTemp("", "")
import System.IO.Temp
withTempDirectory $ \d -> do
  -- do something with d
const tempDir = await Deno.makeTempDir();
import java.io.File;
File folder = new File("/path/");
folder.deleteOnExit();
function createTempDir(): ?string
{
    $path = rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . mt_rand() . microtime(true);
    if (mkdir($path)) {
        return $path;
    }
    return null;
}
uses sysutils;
var
  GUID: TGUID;
  Succes: Boolean;

begin
  CreateGUID(GUID);
  Succes := CreateDir(GetTempDir + GUIDToString(GUID));
end.
use Path::Tiny qw(tempdir);
my $path = tempdir;
require 'tmpdir' 
td = Dir.mktmpdir
extern crate tempdir;
use tempdir::TempDir;
let tmp = TempDir::new("prefix")?;

New implementation...
< >
programming-idioms.org