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

时间:2015-03-03 13:36:32

标签: powershell sharepoint sharepoint-2010 count

我正在尝试计算文档库中的每个文件夹。父级下有许多文件夹。每个文件夹可能只有几个文件夹,每个文件夹中都有一个子文件夹。

我可以添加更多编码,但我觉得可以做得更好。请建议。

$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()

2 个答案:

答案 0 :(得分:0)

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

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
    }
}

答案 1 :(得分:-1)

以下代码有效。

$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()