如何在 powershell 中创建一个简短但仍然唯一的目录名称
how to create a short but still unique directory name in powershell
是否可以创建比默认 guid 格式更短的真正唯一的目录名称(即基于 uuid)?
到目前为止我已经能够想出这个:
function Get-UglyButShortUniqueDirname {
[CmdletBinding()]
param ()
$t = "$([System.Guid]::NewGuid())".Replace("-", "")
Write-Verbose "base guid: $t"
$t = "$(0..$t.Length | % { if (($_ -lt $t.Length) -and !($_%2)) { [char][byte]"0x$($t[$_])$($t[$_+1])" } })".replace(" ", "").Trim()
Write-Verbose "guid as ascii: $t"
([System.IO.Path]::GetInvalidFileNameChars() | % { $t = $t.replace($_, '.') })
Write-Verbose "dirname: $t"
$t
}
有了这个,我可以生成看起来很奇怪但只需要大约 16 个字符的目录名,这比普通 guid(没有破折号)的默认 32 个字符要好得多。
我有点担心的事情:由于 'invalid file name characters' 被剥离并用点代替,这些标识符不能像 guid 那样具有相同的“唯一性承诺”。
(在基于 Win 的自动化环境中与遗留的 260 字符路径名限制作斗争 :-/)
将您的 quid 转换为 Base64 which gives you a 24 characters string and (as mentioned by zett42) 需要替换可能的斜杠 (/
)。此外,您可以通过删除不必要的填充来节省另外两个字符:
[System.Convert]::ToBase64String((NewGuid).ToByteArray()).SubString(0,22).Replace('/', '-')
zp92wiHcdU+0Eb9Cw2z0VA
BUT,这个思路其实有一个缺陷:文件夹名称不区分大小写,也就是说文件夹命名可能不像原来那么独特guid
.
因此,您可能想求助于 Base32
(需要 26 个字符),这有点复杂,因为没有标准的 .Net 方法:
$Chars = ('A'..'Z') + ('2'..'7')
$Bytes = (New-Guid).ToByteArray()
$Bits = -join $Bytes.ForEach{ [Convert]::ToString($_, 2).PadLeft(8, '0') }
-Join ($Bits -Split '(?<=\G.{5})').foreach{ $Chars[[Convert]::ToInt32($_, 2)] }
DZ77OUQNDRQUTGP5ATAM7KCWCB
您可能会执行类似于 include special characters 的操作,但我会对此非常小心,因为并非每个文件系统都支持它。
我会对 GUID Base32 进行编码。对于 26 个字符,结果将比 Base64 略长,但您不会丢失 case-insensitive 文件系统上的随机性。它也只使用基本的字母数字字符,IMO 看起来更好 ;-).
遗憾的是,.NET 中没有 built-inBase32 编码器。首先我采用了 this C# answer but then I got fancy and modified it to use the z-base32 变体的编码部分,这在人眼上更容易,并且通过不使用填充来节省一些字符。
Add-Type -TypeDefinition @'
public class ZBase32Encoder {
private const string Charset = "ybndrfg8ejkmcpqxot1uwisza345h769";
public static string ToString(byte[] input) {
if (input == null || input.Length == 0) {
throw new System.ArgumentNullException("input");
}
long returnLength = (input.Length * 8L - 1) / 5 + 1;
if( returnLength > (long) System.Int32.MaxValue ) {
throw new System.ArgumentException("Argument length is too large. (Parameter 'input')");
}
char[] returnArray = new char[returnLength];
byte nextChar = 0, bitsRemaining = 5;
int arrayIndex = 0;
foreach (byte b in input) {
nextChar = (byte)(nextChar | (b >> (8 - bitsRemaining)));
returnArray[arrayIndex++] = Charset[nextChar];
if (bitsRemaining < 4) {
nextChar = (byte)((b >> (3 - bitsRemaining)) & 31);
returnArray[arrayIndex++] = Charset[nextChar];
bitsRemaining += 5;
}
bitsRemaining -= 3;
nextChar = (byte)((b << bitsRemaining) & 31);
}
//if we didn't end with a full char
if (arrayIndex != returnLength) {
returnArray[arrayIndex++] = Charset[nextChar];
}
return new string(returnArray);
}
}
'@
foreach( $i in 1..5 ) {
$s = [ZBase32Encoder]::ToString($(New-Guid).ToByteArray())
"$s (Length: $($s.Length))"
}
输出:
81u68ug6txxwpbqz4znzgq3hfa (Length: 26)
sseik38xykrr5n99zedj96nsoy (Length: 26)
a353cgcyhefwdc8euk34zbytxa (Length: 26)
e3x8zd576zcrzn3nyxwxncenho (Length: 26)
7idr4xencm9rmp8wkzidk1fyhe (Length: 26)
是否可以创建比默认 guid 格式更短的真正唯一的目录名称(即基于 uuid)?
到目前为止我已经能够想出这个:
function Get-UglyButShortUniqueDirname {
[CmdletBinding()]
param ()
$t = "$([System.Guid]::NewGuid())".Replace("-", "")
Write-Verbose "base guid: $t"
$t = "$(0..$t.Length | % { if (($_ -lt $t.Length) -and !($_%2)) { [char][byte]"0x$($t[$_])$($t[$_+1])" } })".replace(" ", "").Trim()
Write-Verbose "guid as ascii: $t"
([System.IO.Path]::GetInvalidFileNameChars() | % { $t = $t.replace($_, '.') })
Write-Verbose "dirname: $t"
$t
}
有了这个,我可以生成看起来很奇怪但只需要大约 16 个字符的目录名,这比普通 guid(没有破折号)的默认 32 个字符要好得多。
我有点担心的事情:由于 'invalid file name characters' 被剥离并用点代替,这些标识符不能像 guid 那样具有相同的“唯一性承诺”。
(在基于 Win 的自动化环境中与遗留的 260 字符路径名限制作斗争 :-/)
将您的 quid 转换为 Base64 which gives you a 24 characters string and (as mentioned by zett42) 需要替换可能的斜杠 (/
)。此外,您可以通过删除不必要的填充来节省另外两个字符:
[System.Convert]::ToBase64String((NewGuid).ToByteArray()).SubString(0,22).Replace('/', '-')
zp92wiHcdU+0Eb9Cw2z0VA
BUT,这个思路其实有一个缺陷:文件夹名称不区分大小写,也就是说文件夹命名可能不像原来那么独特guid
.
因此,您可能想求助于 Base32
(需要 26 个字符),这有点复杂,因为没有标准的 .Net 方法:
$Chars = ('A'..'Z') + ('2'..'7')
$Bytes = (New-Guid).ToByteArray()
$Bits = -join $Bytes.ForEach{ [Convert]::ToString($_, 2).PadLeft(8, '0') }
-Join ($Bits -Split '(?<=\G.{5})').foreach{ $Chars[[Convert]::ToInt32($_, 2)] }
DZ77OUQNDRQUTGP5ATAM7KCWCB
您可能会执行类似于 include special characters 的操作,但我会对此非常小心,因为并非每个文件系统都支持它。
我会对 GUID Base32 进行编码。对于 26 个字符,结果将比 Base64 略长,但您不会丢失 case-insensitive 文件系统上的随机性。它也只使用基本的字母数字字符,IMO 看起来更好 ;-).
遗憾的是,.NET 中没有 built-inBase32 编码器。首先我采用了 this C# answer but then I got fancy and modified it to use the z-base32 变体的编码部分,这在人眼上更容易,并且通过不使用填充来节省一些字符。
Add-Type -TypeDefinition @'
public class ZBase32Encoder {
private const string Charset = "ybndrfg8ejkmcpqxot1uwisza345h769";
public static string ToString(byte[] input) {
if (input == null || input.Length == 0) {
throw new System.ArgumentNullException("input");
}
long returnLength = (input.Length * 8L - 1) / 5 + 1;
if( returnLength > (long) System.Int32.MaxValue ) {
throw new System.ArgumentException("Argument length is too large. (Parameter 'input')");
}
char[] returnArray = new char[returnLength];
byte nextChar = 0, bitsRemaining = 5;
int arrayIndex = 0;
foreach (byte b in input) {
nextChar = (byte)(nextChar | (b >> (8 - bitsRemaining)));
returnArray[arrayIndex++] = Charset[nextChar];
if (bitsRemaining < 4) {
nextChar = (byte)((b >> (3 - bitsRemaining)) & 31);
returnArray[arrayIndex++] = Charset[nextChar];
bitsRemaining += 5;
}
bitsRemaining -= 3;
nextChar = (byte)((b << bitsRemaining) & 31);
}
//if we didn't end with a full char
if (arrayIndex != returnLength) {
returnArray[arrayIndex++] = Charset[nextChar];
}
return new string(returnArray);
}
}
'@
foreach( $i in 1..5 ) {
$s = [ZBase32Encoder]::ToString($(New-Guid).ToByteArray())
"$s (Length: $($s.Length))"
}
输出:
81u68ug6txxwpbqz4znzgq3hfa (Length: 26)
sseik38xykrr5n99zedj96nsoy (Length: 26)
a353cgcyhefwdc8euk34zbytxa (Length: 26)
e3x8zd576zcrzn3nyxwxncenho (Length: 26)
7idr4xencm9rmp8wkzidk1fyhe (Length: 26)