2014-10-11 258 views
0

我有一个目录,其中包含任何数量为0到300个文件的子目录。Powershell - 列出每个子目录中的子目录和计数文件

我要输出的子目录名称和文件的该子目录

我至今是给我0不管实际的文件有多少是在子目录的数目。

$dir = "C:\Folder\" 
$subFiles = (Get-ChildItem $dir -recurse | where-object {$_.PSIsContainer -eq $true }) 
$subFiles | % { 
Get-ChildItem -Path $_ -Force -Recurse | Measure-Object | Select-Object -ExpandProperty Count 
write-host "$_" 
} 

它有时也被包括在该脚本正在运行IE目录 “C:\用户\布拉赫\的Documents and Settings \ startmen”,造成错误。

不胜感激任何帮助,谢谢

回答

0

这做我想要什么:)它不是很漂亮,但它的工作..

$dir = "C:\folder\" 
$subFiles = (Get-ChildItem $dir -recurse | where-object {$_.PSIsContainer -eq $true }) 
$subFiles | % { 
$path = $dir+$_ 
$files = Get-ChildItem -Path $path -Force -Recurse -File -ErrorAction SilentlyContinue 
write-host "$_ Files:" $files.count 
} 
2

您至少使用PowerShell的3.0,因为你正在使用Get-ChildItem-File参数,所以你不需要使用where-object {$_.PSIsContainer -eq $true }。这已被替换为-Directory参数。循环浏览所有文件夹并收集其文件的文件夹名称和计数。我删除了文件计数的-Recurse,因为这可能会引起误解。如果它更适合你,就把它放回去。最后的Select-Object是为了确保输出的顺序,现在你可以排序或做任何你想要的东西。

$dir = "C:\File" 
Get-ChildItem $dir -Recurse -Directory | ForEach-Object{ 
    [pscustomobject]@{ 
     Folder = $_.FullName 
     Count = @(Get-ChildItem -Path $_.Fullname -File).Count 
    } 
} | Select-Object Folder,Count 

洞察

以前您一直得到这些错误,因为你不打电话的完整路径Get-ChildItem你刚才打电话的文件夹名称。如果没有完整路径,Get-ChildItem假定您正在查找当前目录中的文件夹。那通常是你的用户目录。

+0

嗨马特。任何你可以修改脚本的机会只包含在提供的路径中的子文件夹? – mrjayviper 2017-10-30 13:04:34

+1

@mrjayviper这很简单。只需使用'Get-ChildItem'的'-Depth'参数 – Matt 2017-10-30 13:07:59

+0

我尝试在示例脚本的第一个和第二个gci上使用“-depth 1”(也尝试了2),并且与原始脚本没有区别。 – mrjayviper 2017-10-30 13:13:26

1

对于远程目录而言,这种计数方法似乎更快。

$count = [System.IO.Directory]::GetFiles($_.Fullname).Count 
相关问题