使用Powershell计算文件夹

M. *_*. X 6 windows shell powershell scripting

有没有人知道powershell 2.0命令/脚本来计算特定文件夹中的所有文件夹和子文件夹(递归;没有文件)(例如C:\ folder1\folder2中所有子文件夹的数量)?

此外,我还需要所有"叶子"文件夹的数量.换句话说,我只想计算没有子模板的文件夹.

RB.*_*RB. 8

您可以使用get-childitem -recurse获取当前文件夹中的所有文件和文件夹.

管道将Where-Object其过滤为仅作为容器的文件.

$files = get-childitem -Path c:\temp -recurse 
$folders = $files | where-object { $_.PSIsContainer }
Write-Host $folders.Count
Run Code Online (Sandbox Code Playgroud)

作为单线:

(get-childitem -Path c:\temp -recurse | where-object { $_.PSIsContainer }).Count
Run Code Online (Sandbox Code Playgroud)


Sha*_*evy 8

在PowerShell 3.0中,您可以使用目录开关:

(Get-ChildItem -Path <path> -Directory -Recurse -Force).Count
Run Code Online (Sandbox Code Playgroud)