June 19, 2009

Temp Directory in Powershell

I deal sometimes with very large files that I need to unzip and then parse. I have had instances where I just forget to delete that huge file and it comes back to bite me. I started to unpack these files to the temp directory in order for it to get cleaned up when I clean my temp folder. Here is the simple function that I use:
function CreateTempDir
{
$tmpDir = [System.IO.Path]::GetTempPath()
$tmpDir = [System.IO.Path]::Combine($tmpDir, [System.IO.Path]::GetRandomFileName())

[System.IO.Directory]::CreateDirectory($tmpDir) | Out-Null

$tmpDir
}
and then I use it like this:
$tmpDir = CreateTempDir

No comments:

Post a Comment