当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 将文档库文件-文件夹-子文件夹结构导出为 CSV

作者:精品下载站 日期:2024-12-14 14:06:54 浏览:15 分类:玩电脑

将文档库文件-文件夹-子文件夹结构导出为 CSV


要求:从 SharePoint 文档库获取所有文件夹-子文件夹-文件的完整结构,并将它们导出到 CSV 文件。

用于迭代每个文件夹和子文件夹并获取所有文件的 PowerShell 脚本:


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue 

#Function to get all files of a folder
Function GetFiles-ByFolder([Microsoft.SharePoint.SPFolder]$Folder)
{
    write-host -f Yellow "Processing Folder:"$Folder.URL
    Foreach($File in $Folder.Files)
    {
        $Content = $Folder.Name + "," + $Folder.URL +"," + $File.Name
        #Append content to CSV file
        Add-content $OutPutFile $Content
        Write-host $Content
    }
    
    #Call the function for each subfolder - Excluding "Forms"
    $Folder.SubFolders | Where {$_.Name -ne "Forms" } | Foreach-Object {
    GetFiles-ByFolder $_
    }
}
 
#Variables
$SiteURL = "https://opera.crescent.com/sites/sales"
$ListName ="Shared Documents"
$OutPutFile = "C:\LibraryFiles.csv"

#Delete the CSV file if exists
If (Test-Path $OutPutFile) { Remove-Item $OutPutFile }

#Write CSV headers
Add-Content $OutPutFile "Folder Name, Relative URL, File Name"

#Get site and list objects
$Web = Get-SPWeb $SiteURL
$List = $Web.Lists[$ListName]
$Folder = $List.RootFolder

#Call the function for Root folder
GetFiles-ByFolder $Folder

这会将所有文件和文件夹结构的清单导出到 CSV 文件!。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯