简体   繁体   English

Windows10 / PowerShell:如何计算文件数,然后按子文件夹分类?

[英]Windows10/powershell: how to count files and then sort them by subfolders?

I have a script in Linux that searches/counts all the files in subfolders and gives me a count for each. 我在Linux中有一个脚本,可以搜索/计算子文件夹中的所有文件,并为每个文件计数。

Sample output when run: my-count-script.sh /usr 运行时的示例输出: my-count-script.sh /usr

 5322 X11R6
  316 bin
   89 lib
 2165 libdata
   50 libexec
19220 local
   10 mdec
  206 sbin
 8970 share

This gives me count for all folders immediately inside /usr . 这给了我/usr内所有文件夹的计数。 So the actual path to share is /usr/share . 因此,实际的共享路径为/usr/share path to bin is /usr/bin . bin的路径是/usr/bin

I guess /usr is the parent and the children includes share , bin and etc. I want the display to stop at the "children" level. 我想/usr是父级,子级包括sharebin等。我希望显示在“ children”级别停止。

Any ideas how it can be done in power shell? 有什么想法可以在电源外壳中完成吗?

像这样的快速班轮:

(ls c:\YourPath\*\* -File).Directory | Group-Object name

You want something like: 您想要类似的东西:

Get-Childitem $home -recurse | Group-Object Directory -noelement

I have used $home as a directory spec here, but you will probably want to use one that's more relevant to your situation. 我在这里使用$ home作为目录规范,但是您可能希望使用与您的情况更相关的目录。

This will get child item count for each sub directory on its first level. 这将在其第一级上获取每个子目录的子项计数。

Gets all children that are directories then for each directory it gets all files recursively for each sub folder. 获取所有子目录,然后为每个目录递归获取每个子文件夹的所有文件。

Get-childitem C:\Test -Recurse -Depth 0 -Directory | %{@{$_.Name = (Get-ChildItem $_.FullName -File -Recurse).count}} | format-table

If you do not want the recursive count then run this which will just get you the file count in each sub directory 如果您不希望递归计数,请运行此命令,这只会使您获得每个子目录中的文件计数

Get-childitem C:\Test -Recurse -Depth 0 -Directory | %{@{$_.Name = (Get-ChildItem $_.FullName -File).count}} | format-table

Here is a function to also achieve what you would like 这是一个还可以实现您想要的功能

function Get-SubDirectoryCount(){
    Param(
      [Parameter(Mandatory=$True)]
      [string]$Directory,
      [switch]$Recurse,
      [Parameter(ParameterSetName='FilesOnly')]
      [switch]$FilesOnly,
      [Parameter(ParameterSetName='DirectoriesOnly')]
      [switch]$DirectoriesOnly,
      [Parameter(ParameterSetName='FilesDirectoriesOnly')]
      [switch]$FilesAndDirectories
    )
    Invoke-expression  "Get-childitem $Directory -Recurse -Depth 0 -Directory | %{@{`$_.Name = (Get-ChildItem `$_.FullName $(if($DirectoriesOnly){write-output "-Directory"})$(if($FilesOnly){write-output "-File"}) $(if($Recurse){write-output "-Recurse"})).count}} | format-table"
}

#Get Files and Directories Recursively 
Get-SubDirectoryCount -Directory C:\Test -FilesAndDirectories -Recurse
#Get Files Recursively 
Get-SubDirectoryCount -Directory C:\Test -FilesOnly -Recurse
#Get Directories Recursively
Get-SubDirectoryCount -Directory C:\Test -DirectoriesOnly -Recurse

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM