對于各種應用程式,我需要獲取一些檔案的父檔案夾以用于創建其他輸出。
$x = "C:\MyFolder\test.txt"
$parent = Split-Path (Split-Path $x) -Leaf
這很好用,除非我靠近頂層。
$x = "C:\test.txt"
$parent = Split-Path (Split-Path $x) -Leaf
這有效,$parent = C:\但沒有-Leaf這樣的。我剩下的可能是“D:、E:、F:”,甚至可能是一個網路位置。一般來說,處理這些情況的好方法是什么?
我可以:
if ($parent.length -eq 3) {
handle-top-level-situation
}
但這感覺有點不雅。當我以這種方式檢查父檔案夾名稱時,是否有更好的方法來檢查頂層?
uj5u.com熱心網友回復:
基于 .NET API 的解決方案:
將路徑轉換為
[System.IO.FileInfo]允許您通過以下方式獲取父目錄名稱.Directory.Name[System.IO.Path]::IsRooted()告訴您該名稱是代表根目錄還是代表 UNC 共享。
"C:\MyFolder\test.txt", "C:\test.txt" | ForEach-Object {
$parent = ([IO.FileInfo] $_).Directory.Name
if ([IO.Path]::IsPathRooted($parent)) {
"parent dir. is root path: $parent"
} else {
"parent dir. name: $parent"
}
}
輸出:
parent dir. name: MyFolder
parent dir. is root path: C:\
或者,如果您想預先排除根目錄中的檔案,甚至在嘗試提取父目錄名稱之前,您可以使用帶有以下內容的正則運算式-notmatch:
"C:\MyFolder\test.txt", "C:\test.txt" | ForEach-Object {
if ($_ -notmatch '^[a-z]:\\. ?\\.|^\\\\. ?\\. ?\\.') {
"file is located in a root dir: $_"
} else {
"parent dir. name: " ([IO.FileInfo] $_).Directory.Name
}
}
注:以上假設:
常規完整路徑(不會意外加倍,
\例如 inc:\\temp),盡管可以調整正則運算式來處理它們。Windows 路徑分隔符,即
\; 要使正則運算式跨平臺,請將所有\\實體替換為[\\/].
uj5u.com熱心網友回復:
此功能可能能夠幫助您獲得所需的內容,基本上,將路徑作為輸入,并確定它是File 還是 Folder Path,Parent Folder是什么以及Path 是否是 Top-Level。我沒有網路共享來測驗 UNC,只能針對 localhost \ 127.0.0.1 進行測驗...
function Test-Something {
param(
[Parameter(Mandatory, ValueFromPipeline)]
[string] $Path
)
process {
$out = [ordered]@{ InputPath = $path }
try {
$checkFolder = [IO.File]::GetAttributes($Path) -band [IO.FileAttributes]::Directory
}
catch {
$checkFolder = $true
if([IO.Path]::GetExtension($Path)) { $checkFolder = $false }
}
if($checkFolder) {
$path = Join-Path $Path ([IO.Path]::DirectorySeparatorChar)
$io = [IO.DirectoryInfo] $Path
$out['Parent'] = $io.Parent.Name
$out['Type'] = 'Directory'
$out['TopLevel'] = ($true, $false)[[bool] $io.Parent]
}
else {
$io = [IO.FileInfo] $Path
$out['Parent'] = $io.Directory.Name
$out['Type'] = 'Archive'
$out['TopLevel'] = ($true, $false)[[bool] $io.Directory]
}
[pscustomobject] $out
}
}
幾個例子:
@(
'C:'
'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe'
'C:\doesnotexist'
'C:\doesnotexist\somefile.ext'
'\\localhost\c$\Windows'
'\\127.0.0.1\c$'
) | Test-Something
輸出:
InputPath Parent Type TopLevel
--------- ------ ---- --------
C: Directory True
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe v1.0 Archive False
C:\doesnotexist C:\ Directory False
C:\doesnotexist\somefile.ext doesnotexist Archive False
\\localhost\c$\Windows \\localhost\c$ Directory False
\\127.0.0.1\c$ Directory True
uj5u.com熱心網友回復:
一種方法可能是拆分字串
$x -split '\\' | Select-Object -First 1
這兩個斜線只是一個斜線,但第二個是為了逃避分割的斜線所必需的。結果是“ C: ”,但對于網路共享,它不會回傳服務器名。
對于網路共享和本地驅動器,您需要
$x -split '\\' | where {$_} | Select-Object -First 1
回傳驅動器名或服務器名
再次閱讀您的問題和第二個非常好的答案,我不確定您是否期望其他內容。所以在這里我第三次嘗試你可能期望的
function Split-PathToIndex($path, $index) {
$pathlist = @()
while ($path) {
$pathlist = $path
$path = Split-Path -Path $path -Parent
}
$pathlist[$index]
}
“#”后面帶有預期輸出的示例
$YourPath = "c:\Test\Path\with\multiple\folder\and\files.txt"
Split-PathToIndex $YourPath 0 # c:\Test\Path\with\multiple\folder\and\files.txt
Split-PathToIndex $YourPath 1 # c:\Test\Path\with\multiple\folder\and
Split-PathToIndex $YourPath 2 # c:\Test\Path\with\multiple\folder
Split-PathToIndex $YourPath 3 # c:\Test\Path\with\multiple
Split-PathToIndex $YourPath 4 # c:\Test\Path\with
Split-PathToIndex $YourPath 5 # c:\Test\Path
Split-PathToIndex $YourPath 6 # c:\Test
Split-PathToIndex $YourPath 7 # c:\
Split-PathToIndex $YourPath -1 # c:\
Split-PathToIndex $YourPath -2 # c:\Test
Split-PathToIndex $YourPath -3 # c:\Test\Path
Split-PathToIndex $YourPath -4 # c:\Test\Path\with
Split-PathToIndex $YourPath -5 # c:\Test\Path\with\multiple
Split-PathToIndex $YourPath -6 # c:\Test\Path\with\multiple\folder
Split-PathToIndex $YourPath -7 # c:\Test\Path\with\multiple\folder\and
Split-PathToIndex $YourPath -8 # c:\Test\Path\with\multiple\folder\and\files.txt
現在我想你想要Split-PathToIndex $YourPath -2這樣你總是得到第一個檔案夾,但我仍然不確定:D
所有三個解決方案都沒有驗證它是否是有效/可到達的路徑!
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/513266.html
標籤:电源外壳小路父母
上一篇:在引數中強制使用完整的域名
