【发布时间】:2016-01-01 19:59:35
【问题描述】:
PowerShell 5 introduces the New-TemporaryFile cmdlet,很方便。我怎样才能做同样的事情,而不是一个文件创建一个目录?有New-TemporaryDirectory cmdlet 吗?
【问题讨论】:
标签: powershell temporary-directory
PowerShell 5 introduces the New-TemporaryFile cmdlet,很方便。我怎样才能做同样的事情,而不是一个文件创建一个目录?有New-TemporaryDirectory cmdlet 吗?
【问题讨论】:
标签: powershell temporary-directory
我认为可以通过对目录名称使用 GUID 来完成,而无需循环:
function New-TemporaryDirectory {
$parent = [System.IO.Path]::GetTempPath()
[string] $name = [System.Guid]::NewGuid()
New-Item -ItemType Directory -Path (Join-Path $parent $name)
}
这是我this C# solution的端口:
function New-TemporaryDirectory {
$parent = [System.IO.Path]::GetTempPath()
$name = [System.IO.Path]::GetRandomFileName()
New-Item -ItemType Directory -Path (Join-Path $parent $name)
}
GetRandomFileName 返回临时文件夹中已存在的名称的可能性有多大?
XXXXXXXX.XXX 的形式返回,其中 X 可以是小写字母或数字。GetRandomFileName 可能会发生碰撞
NewGuid 而can be one of 2^122 possibilities,几乎不可能发生碰撞。
【讨论】:
GetRandomFileName() 和New-Item 放在一个循环中,以便在名称冲突的情况下自动重试。
-Force 来创建不存在的父目录,但我错了,所以我更新了答案。
我也喜欢单线,我请求在这里投反对票。我所要求的只是你把我自己对此模糊的负面感受用语言表达出来。
New-TemporaryFile | %{ rm $_; mkdir $_ }
根据您的纯粹主义者类型,您可以使用%{ mkdir $_-d },留下占位符以避免冲突。
而且支持Join-Path $env:TEMP $(New-Guid) | %{ mkdir $_ } 也是合理的。
【讨论】:
New-TemporaryFile | % { Remove-Item $_; New-Item -ItemType Directory -Path $_ }
-d 并不能避免冲突。 (如果您同时在两个不同的脚本中使用相同的方法怎么办?)但是,您并没有试图避免冲突,是吗? ;-)
-d 是为了避免生成的文件和目录之间的所谓“冲突”。
这是 user4317867 的answer 的变体。我在用户的 Windows "Temp" 文件夹中创建了一个新目录,并将临时文件夹路径作为变量提供 ($tempFolderPath):
$tempFolderPath = Join-Path $Env:Temp $(New-Guid)
New-Item -Type Directory -Path $tempFolderPath | Out-Null
以下是作为单行脚本可用的相同脚本:
$tempFolderPath = Join-Path $Env:Temp $(New-Guid); New-Item -Type Directory -Path $tempFolderPath | Out-Null
以下是完全限定的临时文件夹路径 ($tempFolderPath):
C:\Users\MassDotNet\AppData\Local\Temp\2ae2dbc4-c709-475b-b762-72108b8ecb9f
【讨论】:
如果您希望循环解决方案能够保证无竞争和无碰撞,那么就是:
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
}
根据Michael Kropat's answer 中的分析,绝大多数情况下,这只会通过循环一次。很少会通过两次。几乎永远不会超过三遍。
【讨论】:
这是我的尝试:
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
}
【讨论】:
while 的结尾和New-Item 命令之间的竞争条件。
.NET 拥有[System.IO.Path]::GetTempFileName() 已经有一段时间了;您可以使用它来生成文件(并捕获名称),然后在删除文件后创建一个具有相同名称的文件夹。
$tempfile = [System.IO.Path]::GetTempFileName();
remove-item $tempfile;
new-item -type directory -path $tempfile;
【讨论】:
$tempfilename 在哪里使用?
如果可能的话,我喜欢一个班轮。 @alroc .NET 也有 [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
【讨论】:
如果你愿意,你可以非常花哨,像这样调用Windows API函数GetTempPathA():
# DWORD GetTempPathA(
# DWORD nBufferLength,
# LPSTR lpBuffer
# );
$getTempPath = @"
using System;
using System.Runtime.InteropServices;
using System.Text;
public class getTempPath {
[DllImport("KERNEL32.DLL", EntryPoint = "GetTempPathA")]
public static extern uint GetTempPath(uint nBufferLength, [Out] StringBuilder lpBuffer);
}
"@
Add-Type $getTempPath
$str = [System.Text.StringBuilder]::new()
$MAX_PATH = 260
$catch_res = [getTempPath]::GetTempPath($MAX_PATH, $str)
Write-Host $str.ToString() #echos temp path to STDOUT
# ... continue your code here and create sub folders as you wish ...
【讨论】:
Path.GetTempPath 在内部为您执行的准确代码。
从 Michael Kropat 的回答扩展: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;
}
这应该可以消除任何碰撞问题。
【讨论】:
while 和New-Item 命令的结尾之间面临竞争条件,与上面的other answer 完全相同。