递归计算文件而不计算子文件夹

时间:2017-07-31 12:30:12

标签: powershell

我有一堆文件夹都有两个子文件夹,这两个子文件夹每个都有一个子文件夹。我已经编写了一个脚本,根据文件名将图像分类到这些文件夹中,我找到了一个代码片段,可以生成每个目录的列表以及其中有多少项目的计数。问题是我希望递归搜索不将子文件夹计为文件,同时仍然搜索它们。我目前使用的代码如下:

C:\...\Folder1 2 #These are the two subfolders that are listed
C:\...\Folder1.1 18 #These are the files in the folder, the subfolder included
C:\...\Folder1.2 47 #Subfolder included
C:\...\Folder1.1.1 10 #Herein lies only files
C:\...\Folder1.2.1 5 #Only files

这为我提供了以下目录和计数的列表:

{{1}}

基本上,我希望Folder1.1和Folder1.2的计数不包含Folder 1.1.1和Folder1.2.1,因为它们是文件夹而不是文件。

到目前为止,我的努力导致只搜索文件或仅搜索文件夹。

1 个答案:

答案 0 :(得分:0)

你是如此亲密:

dir -recurse |  ?{ $_.PSIsContainer } | %{ Write-Host $_.FullName
(dir $_.FullName -recurse |  ?{ $_.PSIsContainer -eq $false } | Measure-Object).Count }

dir -recurse |  ?{ $_.PSIsContainer } | %{ Write-Host $_.FullName
(dir $_.FullName -recurse |  ?{ $_.PSIsContainer -eq $false } ).Count }

只计算FileInfo项目。

请帮我一个忙,然后使用" Get-ChildItem"而不是" dir"。它是相同的,但看起来像cmd。 Dir / r - >我明白 Get-ChildItem -Recurse - >超 Dir -Recurse --s就像一个令人毛骨悚然的混合体。

相关问题