我想獲得一個目錄串列,-recurse但要在父目錄下顯示子目錄。
這是我嘗試過的:
Get-ChildItem -Directory -Depth 3 | Select-Object Fullname
結果顯示首先是頂級目錄,然后是 2 級,然后是 3 級
例子:
C:\Users\Moshe\onedrive\pictures\35mm_Film (has no sub directories)
C:\Users\Moshe\onedrive\pictures\Camera imports
C:\Users\Moshe\onedrive\pictures\Camera Roll
只有這樣
C:\Users\Moshe\onedrive\pictures\Camera imports\Folder1
C:\Users\Moshe\onedrive\pictures\Camera imports\Folder2
C:\Users\Moshe\onedrive\pictures\Camera imports\Folder3
接著
C:\Users\Moshe\onedrive\pictures\Camera Roll\2014
C:\Users\Moshe\onedrive\pictures\Camera Roll\2016
我想獲得顯示父目錄和子目錄下方的串列
例如:
C:\Users\Moshe\onedrive\pictures\35mm_Film (has no sub directories)
C:\Users\Moshe\onedrive\pictures\Camera imports
C:\Users\Moshe\onedrive\pictures\Camera imports\Folder1
C:\Users\Moshe\onedrive\pictures\Camera imports\Folder2
C:\Users\Moshe\onedrive\pictures\Camera imports\Folder3
C:\Users\Moshe\onedrive\pictures\Camera Roll
C:\Users\Moshe\onedrive\pictures\Camera Roll\2014
C:\Users\Moshe\onedrive\pictures\Camera Roll\2016
擁有每個目錄的檔案數量也很好,但并不重要
例如:
C:\Users\Moshe\onedrive\pictures\35mm_Film 100
C:\Users\Moshe\onedrive\pictures\Camera imports 0 (no files in the parent)
C:\Users\Moshe\onedrive\pictures\Camera imports\Folder1 20
ETC
uj5u.com熱心網友回復:
如果您想這樣做而不需要事后排序,則解決方案可能有點麻煩,您可以使用遞回函式/腳本塊,或者如本例所示,使用 aStack<T>和作為可選,您可以使用類:
class Tree {
[int] $Depth
[string] $Path
[int] $FileCount
hidden [IO.DirectoryInfo] $Instance
Tree ([IO.DirectoryInfo] $Directory, [int] $Depth) {
$this.Instance = $Directory
$this.Path = $Directory.FullName
$this.Depth = $Depth
$this.CountFiles()
}
[IO.DirectoryInfo[]] EnumerateDirectories() {
return $this.Instance.EnumerateDirectories()
}
[void] CountFiles() {
$this.FileCount = $this.Instance.GetFiles().Count
}
}
$stack = [Collections.Generic.Stack[Tree]]::new()
# define max Depth here
$maxDepth = 3
# initial path here
$path = 'C:\Users\Moshe\onedrive\pictures'
# define what you want to skip here
$attrToSkip = [IO.FileAttributes] 'Hidden'
$stack.Push([Tree]::new($path, 0))
while($stack.Count) {
$folder = $stack.Pop()
# this conditions stops the loop
$reachedMaxDepth = $folder.Depth -gt $maxDepth
# this condition skips Hidden folders, remove it if you want to show all
$isHiddenorSystem = $folder.Instance.Attributes -band $attrToSkip
if($reachedMaxDepth -or $isHiddenorSystem) {
continue
}
$folder
foreach($i in $folder.EnumerateDirectories()) {
$stack.Push([Tree]::new($i, $folder.Depth 1))
}
}
uj5u.com熱心網友回復:
要按該順序獲取目錄,您可以使用Sort-Object。
例子:
Get-ChildItem -Directory -recurse -Depth 3 | Select-Object Fullname | Sort-Object Fullname
這將按順序為您提供父檔案夾及其子檔案夾。
uj5u.com熱心網友回復:
我從malexander那里拿了一個例子,并添加了一些丹尼爾的例子,然后想出了這個
Get-ChildItem -Directory -recurse -Depth 3 | Select-Object Fullname | Sort-Object Fullname | % {$_.FullName " " (Get-ChildItem $_.FullName| Measure-Object).Count}
如果理解正確,則 Fullname 僅顯示一次,因為它已通過管道傳輸到同時顯示名稱和計數的 % 。
不管怎樣,它給了我我需要的東西。
謝謝大家
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/487521.html
標籤:电源外壳
