Create a temporary directory in PowerShell?

前端 未结 8 1692
一生所求
一生所求 2021-02-01 14:36

PowerShell 5 introduces the New-TemporaryFile cmdlet, which is handy. How can I do the same thing but instead of a file create a directory? Is there a New-TemporaryDirec

相关标签:
8条回答
  • 2021-02-01 15:06

    I love one liners if possible. @alroc .NET also has [System.Guid]::NewGuid()

    $temp = [System.Guid]::NewGuid();new-item -type directory -Path d:\$temp
    
    Directory: D:\
    
    
    Mode                LastWriteTime     Length Name                                                                                                                        
    ----                -------------     ------ ----                                                                                                                        
    d----          1/2/2016  11:47 AM            9f4ef43a-a72a-4d54-9ba4-87a926906948  
    
    0 讨论(0)
  • 2021-02-01 15:08

    Expanding from Michael Kropat's answer: https://stackoverflow.com/a/34559554/8083582

    Function New-TemporaryDirectory {
      $tempDirectoryBase = [System.IO.Path]::GetTempPath();
      $newTempDirPath = [String]::Empty;
      Do {
        [string] $name = [System.Guid]::NewGuid();
        $newTempDirPath = (Join-Path $tempDirectoryBase $name);
      } While (Test-Path $newTempDirPath);
    
      New-Item -ItemType Directory -Path $newTempDirPath;
      Return $newTempDirPath;
    }
    

    This should eliminate any issues with collisions.

    0 讨论(0)
  • 2021-02-01 15:15

    If you want the looping solution that is guaranteed to be both race- and collision-free, then here it is:

    function New-TemporaryDirectory {
      $parent = [System.IO.Path]::GetTempPath()
      do {
        $name = [System.IO.Path]::GetRandomFileName()
        $item = New-Item -Path $parent -Name $name -ItemType "directory" -ErrorAction SilentlyContinue
      } while (-not $item)
      return $Item.FullName
    }
    

    According to the analysis in Michael Kropat's answer, the vast majority of the time, this will pass only once through the loop. Rarely will it pass twice. Virtually never will it pass three times.

    0 讨论(0)
  • 2021-02-01 15:15

    Here's my attempt:

    function New-TemporaryDirectory {
        $path = Join-Path ([System.IO.Path]::GetTempPath()) ([System.IO.Path]::GetRandomFileName())
    
        #if/while path already exists, generate a new path
        while(Test-Path $path)) {
            $path = Join-Path ([System.IO.Path]::GetTempPath()) ([System.IO.Path]::GetRandomFileName())
        }
    
        #create directory with generated path
        New-Item -ItemType Directory -Path $path
    }
    
    0 讨论(0)
  • 2021-02-01 15:21

    Here's a variant of user4317867's answer. I create a new directory in the user's Windows "Temp" folder and make the temp folder path available as a variable ($tempFolderPath):

    $tempFolderPath = Join-Path $Env:Temp $(New-Guid)
    New-Item -Type Directory -Path $tempFolderPath | Out-Null
    

    Here's the same script available as a one-liner:

    $tempFolderPath = Join-Path $Env:Temp $(New-Guid); New-Item -Type Directory -Path $tempFolderPath | Out-Null
    

    And here's what the fully qualified temp folder path ($tempFolderPath) looks like:

    C:\Users\MassDotNet\AppData\Local\Temp\2ae2dbc4-c709-475b-b762-72108b8ecb9f
    
    0 讨论(0)
  • 2021-02-01 15:22

    I also love one-liners, and I'm begging for a downvote here. All I ask is that you put my own vague negative feelings about this into words.

    New-TemporaryFile | %{ rm $_; mkdir $_ }
    

    Depending on the type of purist you are, you can do %{ mkdir $_-d }, leaving placeholder to avoid collisions.

    And it's reasonable to stand on Join-Path $env:TEMP $(New-Guid) | %{ mkdir $_ } also.

    0 讨论(0)
提交回复
热议问题