【问题标题】:SharePoint CSOM Powershell - Get Count of files in each folder in LibrarySharePoint CSOM Powershell - 获取库中每个文件夹中的文件计数
【发布时间】:2017-08-07 04:58:52
【问题描述】:

我有一个包含许多文件夹的文档库。 我想获取此库中每个文件夹中的项目数。

我想了解这是否可以使用 CSOM Powershell 来实现。

有没有 OOTB 方法?

【问题讨论】:

    标签: file powershell count sharepoint-2013 sharepointdocumentlibrary


    【解决方案1】:

    作为一个预警,我不确定 CSOM Powershell 是否具有与传统 Powershell 相同的 cmdlet。如果他们这样做,请告诉我,我会删除这篇文章。如果没有,这应该有效:

    假设您有一个结构,其中 [library] 包含 [folders] 包含您要计算的所有文件。

    foreach($folder in (Get-ChildItem $[library])){  
        $filecount = 0
        Get-ChildItem $folder -recurse |
        %{$filecount += 1}
        #do whatever you want with $filecount here.
    }
    

    您可以在foreach 循环之外使用字典或其他数据结构来计算哪些文件夹有多少项目。

    【讨论】:

      【解决方案2】:

      文件夹中的文件数量可以通过ItemChildCount field获取,这里是一个示例,演示如何获取每个文件夹的文件数量:

      $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)  
      $ctx.Credentials = new-object System.Net.NetworkCredential($username, $password, $domain)
      $list = $ctx.Web.Lists.GetByTitle("Documents")
      
      $query = New-Object Microsoft.SharePoint.Client.CamlQuery
      $query.ViewXml = "<View Scope='RecursiveAll'>
      <ViewFields>
          <FieldRef Name='FileRef' />
          <FieldRef Name='ItemChildCount' />
      </ViewFields>
      <Query>
          <Where>
             <Eq>
               <FieldRef Name='FSObjType' />
               <Value Type='Integer'>1</Value>
             </Eq>
         </Where>
      </Query>
      </View>"
      
      $items = $list.GetItems($query)
      $ctx.Load($items) 
      $ctx.ExecuteQuery()
      
      
      $items.GetEnumerator() | % {
      
         Write-Host $_["FileRef"] , ":" , $_["ItemChildCount"]
      }
      
      $ctx.Dispose() 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多