【发布时间】:2015-12-04 16:00:04
【问题描述】:
我正在尝试制作一个 powershell 脚本,它将计算目录和子目录中的所有 .xml 文件并列出值 + 路径。
到目前为止,我已经做到了:
Get-ChildItem -Path c:/test -recurse -include *.xml
这与我想要的很接近,但没有实际的文件名,只有文件夹路径和数量。
这就是我得到的:
> Directory: C:\test\A_Sub-folder
> Mode LastWriteTime Length Name
> ---- ------------- ------ ----
> -a--- 27/11/2015 11:29 0 AA.xml
>
> Directory: C:\test
> Mode LastWriteTime Length Name
> ---- ------------- ------ ----
> -a--- 27/11/2015 11:29 0 BB.xml
> -a--- 27/11/2015 11:29 0 CC.xml
> -a--- 27/11/2015 11:29 0 DD.xml
我正在尝试得到这个(或类似的):
> Directory: C:\test\A_Sub-folder
> 1
> Directory: C:\test
> 3
计划是在每个根驱动器上运行这个脚本(有些驱动器有大约 5k .xml 文件,所以我不确定这将如何影响性能。)
编辑:
这对子文件夹非常有效,但由于某种原因它在根驱动器目录下不起作用(例如 e:/)。 我试图排除 \windows 和 \program 文件,但它不起作用。有没有办法在搜索中排除根目录?
到目前为止的脚本:
$excludedPaths = @("E:\Windows", "E\Program Files", "E:\Program Files (x86)", "E:\MSSQL", "E:\MSSQL11.MSSQLSERVER");
$pathtocount = Read-Host -Prompt 'Input path to count xml files'
Get-ChildItem -Path $pathtocount -recurse -include *.xml | Where-Object { $excludedPaths -notcontains $_.Directory } | Group-Object -Property Directory | Sort-Object count
【问题讨论】:
标签: powershell count get-childitem