Storing a reference to array in swift,这很方便。我怎样才能做同样的事情而不是文件创建目录?是否有New-TemporaryDirectory
cmdlet?
答案 0 :(得分:37)
我认为通过使用目录名称的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,几乎不可能发生碰撞。
答案 1 :(得分:13)
我也喜欢单行,我在这里乞求一个downvote。我要问的是,你把自己模糊的负面情绪写成了文字。
New-TemporaryFile | %{ rm $_; mkdir $_ }
根据你的纯粹主义者的类型,你可以%{ mkdir $_-d }
,留下占位符以避免碰撞。
同样站在Join-Path $env:TEMP $(New-Guid) | %{ mkdir $_ }
也是合理的。
答案 2 :(得分:3)
这是我的尝试:
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
}
答案 3 :(得分:2)
.NET已经有<?php
$dbhost = 'zzz';
$dbuser = 'zzz';
$dbpwd = 'zzz';
$dbname = 'zzz';
$conn = new mysqli( $dbhost, $dbuser, $dbpwd, $dbname );
$item1 = "apple";
$item2 = "peach";
$items=array( $item1, $item2 );
$sql='select * from products where `Name` in ("'.implode('","',$items).'");';
// here's the first two errors.
// Notice: Undefined variable: sql on line 11
// Warning: mysqli::query(): Empty query on line 11
// You tried to query the db without defining $sql first (lines were mixed up) ->
$res = $conn->query( $sql );
if ($res) {
$prices=new stdClass();
$i=0; /* counter for dynamic variables */
while( $rs=$res->fetch_object() ){
$i++;
/* Populate an object with details for the product */
$prices->{ strtolower( $rs->name ) }=(object)array( 'price'=>$rs->MyPrice, 'description'=>$rs->MyDescription, 'stock'=>$rs->MyStock );
}
// here's 3rd and 4th error:
// Notice: Undefined variable: prices on line 45
// only output if you got values, so put the output into if-condition
echo 'Apple:'.$prices->apple->price . '<br />';
echo 'Peach:'.$prices->peach->stock . '<br />';
} else {
// database didn't return anything, so tell the user or something with that information.
echo 'no data found or an error occured<br />';
}
?>
了很长一段时间;您可以使用它来生成文件(并捕获名称),然后在删除文件后创建一个具有相同名称的文件夹。
[System.IO.Path]::GetTempFileName()
答案 4 :(得分:1)
如果可能,我喜欢一个衬里。 @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
答案 5 :(得分:1)
如果您希望确保循环解决方案既无竞争又无碰撞,那么这里是:
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中的分析,在绝大多数情况下,这将仅循环一次。它很少会通过两次。实际上,它永远不会通过三遍。
答案 6 :(得分:0)
这是user4317867的answer的变体。我在用户的Windows“ Temp ”文件夹中创建一个新目录,并使temp文件夹路径可用作变量({{1)}:
$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
答案 7 :(得分:0)
如果你愿意,你可以非常花哨,像这样调用 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 ...
答案 8 :(得分:-1)
扩展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;
}
这可以消除任何碰撞问题。