此 powershell 不会为所有子或子子或子子子文件夹提供项目计数

This powershell doesnt give item count to all the sub or sub-sub or sub-sub-sub folders

我正在尝试计算文档库中每个文件夹的数量。 parent下有很多文件夹。每个文件夹可能还有几个文件夹,每个文件夹可能有子文件夹、孙文件夹。

我可以添加更多代码,但我觉得可以用更好的方式完成。请提出建议。

$web = Get-SPWeb "http://inside.cnv.uk/sales/SouthWest"
$list = $web.Lists["SalesManJR"]
$folders = $list.RootFolder.Subfolders
foreach ($folder in $folders)
{ 
    if (!$folder.Name.Equals("Forms"))
    {       
        write-host "Folder Name: " $folder.Name  " Count: "  $folder.Files.Count
        $subfolderURL = $list.RootFolder.SubFolders[$list.Title+"/"+$folder.Name]
        $subfolders = $subfolderURL.SubFolders
        foreach ($subfolder in $subfolders)
        {
            write-host "  Sub-Folder Name: " $subfolder.Name " Count: " $subfolder.Files.Count      
        }       
    }
}
$web.Dispose()

基本上您需要使用递归函数。我不知道你的文件夹结构,但应该这样做:

function Process-Folder($Folder)
{
    Write-Host "Folder Name: " $folder.Name  " Count: "  $folder.Files.Count
    $subFolders = #Do something to get the subfolders#
    if ($subFolders -ne $null)
    {
        foreach ($subFolder in $subFolders)
        {
            Process-Folder $subFolder
        }
    }
}

$web = Get-SPWeb "http://inside.cnv.uk/sales/SouthWest"
$list = $web.Lists["SalesManJR"]
$folders = $list.RootFolder.Subfolders
foreach ($folder in $folders)
{ 
    if (!$folder.Name.Equals("Forms"))
    {
        Process-Folder $folder
    }
}

以下代码有效。

$web = Get-SPWeb "http://inside.cnv.uk/sales/SouthWest"
# $list name is list display name
$list = $web.Lists["SalesManJR"]
$folders = $list.RootFolder.Subfolders

foreach ($folder in $folders)
{ 
    if (!$folder.Name.Equals("Forms"))
    {       
        write-host ""
        write-host "Folder Name: " $folder.Name  " Count: "  $folder.Files.Count
        $subfolderURL = $list.RootFolder.SubFolders[$list.Title+"/"+$folder.Name]
        $subfolders = $subfolderURL.SubFolders
        foreach ($subfolder in $subfolders)
        {
            write-host " Folder Name: " $subfolder.Name " Count: " $subfolder.ItemCount     
            #write-host "  Sub-Folder Name: " $subfolder.Name " Count: " $subfolder.Files.Count     

            # Sub Folder Level 2            
            $subfolders2 = $subfolder.SubFolders
            foreach ($subfolder2 in $subfolders2)
            {
                write-host "  Folder Name: " $subfolder2.Name " Count: " $subfolder2.ItemCount      
                #write-host "  Sub-Folder Name: " $subfolder.Name " Count: " $subfolder.Files.Count

                # Sub Folder Level 3            
                $subfolders3 = $subfolder2.SubFolders
                foreach ($subfolder3 in $subfolders3)
                {
                    write-host "  Folder Name: " $subfolder3.Name " Count: " $subfolder3.ItemCount

                    # Sub Folder Level 4            
                    $subfolders4 = $subfolder3.SubFolders
                    foreach ($subfolder4 in $subfolders4)
                    {
                        write-host "    Folder Name: " $subfolder4.Name " Count: " $subfolder4.ItemCount                    
                    }                   
                }
            }
        }       
    }
}

$web.Dispose()