From 0cc5f9701ef4c73b648d73d52fb320112e49b806 Mon Sep 17 00:00:00 2001 From: Valeras Narbutas Date: Thu, 1 Feb 2024 11:17:07 +0200 Subject: [PATCH] Download an array of file urls and zip them #527 --- .../spo-download-files-and-archive/README.md | 156 ++++++++++++++++++ .../assets/preview.png | Bin 0 -> 11109 bytes .../assets/sample.json | 71 ++++++++ 3 files changed, 227 insertions(+) create mode 100644 scripts/spo-download-files-and-archive/README.md create mode 100644 scripts/spo-download-files-and-archive/assets/preview.png create mode 100644 scripts/spo-download-files-and-archive/assets/sample.json diff --git a/scripts/spo-download-files-and-archive/README.md b/scripts/spo-download-files-and-archive/README.md new file mode 100644 index 000000000..bfb9963be --- /dev/null +++ b/scripts/spo-download-files-and-archive/README.md @@ -0,0 +1,156 @@ +--- +plugin: add-to-gallery +--- + +# Download all files from array of URLs and archive them + +## Summary + +The script will download all files from an array of URLs and archive them into a zip file. Two options are available to specify the file URLs: +1. add the URLs in the script, or +2. read the URLs from a CSV file. + +## Implementation + +- Open Windows PowerShell ISE or VS Code +- Copy script below to your clipboard +- Paste script into your preferred editor +- Change config variables to reflect the site, library name & download location required + + +# [PnP PowerShell](#tab/pnpps) + +```powershell + +# Connect to the SharePoint site +$siteUrl = "https://[tenant].sharepoint.com/sites/[sitename]" +Connect-PnPOnline -Url $siteUrl -UseWebLogin + +# Define the array of file URLs (relative to the site URL) +# Option 1: with urls added in script +$fileUrls = @( + "/sites/[sitename]/Shared Documents/Document.docx", + "/sites/[sitename]/Shared Documents/Book.xlsx" +) + +# Option 2: with urls from a CSV file +# CSV file should have a column named FileUrl +# Example CSV file content: +# FileUrl +# /sites/[sitename]/Shared Documents/file1.docx +# /sites/[sitename]/Shared Documents/file2.pdf + +# Path to the CSV file +# uncomment the line below and specify the path to your CSV file +# $csvFilePath = "C:\path\to\your\file.csv" + +# Reading the file URLs from the CSV file +# uncomment the line below if you want to read the file URLs from a CSV file +# $fileUrls = Import-Csv -Path $csvFilePath | Select-Object -ExpandProperty FileUrl + + +# Specify the local directory to save the downloaded files +$localDirectory = "C:\DownloadedFiles" +if (-not (Test-Path -Path $localDirectory)) { + New-Item -ItemType Directory -Path $localDirectory +} + +# Loop through each file URL, download, and save the file +foreach ($fileUrl in $fileUrls) { + $fileName = [System.IO.Path]::GetFileName($fileUrl) + $localFilePath = Join-Path -Path $localDirectory -ChildPath $fileName + + # Download the file + Get-PnPFile -Url $fileUrl -Path $localDirectory -Filename $fileName -AsFile +} + +# Zip the downloaded files +$zipFilePath = "C:\DownloadedFiles\files.zip" +Compress-Archive -Path "$localDirectory\*" -DestinationPath $zipFilePath + +# Output the location of the zip file +Write-Host "Files have been zipped to: $zipFilePath" + +# Disconnect SharePoint online connection +Disconnect-PnPOnline + +``` + +[!INCLUDE [More about PnP PowerShell](../../docfx/includes/MORE-PNPPS.md)] + +# [CLI for Microsoft 365](#tab/cli-m365-ps) + +```powershell + +# Connect to the SharePoint site +$siteUrl = "https://[tenant].sharepoint.com/sites/[sitename]" + +# Get Credentials to connect +$m365Status = m365 status +if ($m365Status -match "Logged Out") { + m365 login +} + +# Define the array of file URLs (relative to the site URL) +# Option 1: with urls added in script +$fileUrls = @( + "/sites/[sitename]/Shared Documents/Document.docx", + "/sites/[sitename]/Shared Documents/Book.xlsx" +) + +# Option 2: with urls from a CSV file +# CSV file should have a column named FileUrl +# Example CSV file content: +# +# FileUrl +# /sites/[sitename]/Shared Documents/file1.docx +# /sites/[sitename]/Shared Documents/file2.pdf + +# Path to the CSV file +# uncomment the line below and specify the path to your CSV file +# $csvFilePath = "C:\path\to\your\file.csv" + +# Reading the file URLs from the CSV file +# uncomment the line below if you want to read the file URLs from a CSV file +# $fileUrls = Import-Csv -Path $csvFilePath | Select-Object -ExpandProperty FileUrl + + +# Specify the local directory to save the downloaded files +$localDirectory = "C:\DownloadedFiles" +if (-not (Test-Path -Path $localDirectory)) { + New-Item -ItemType Directory -Path $localDirectory +} + +# Loop through each file URL, download, and save the file +foreach ($fileUrl in $fileUrls) { + $fileName = [System.IO.Path]::GetFileName($fileUrl) + $localFilePath = Join-Path -Path $localDirectory -ChildPath $fileName + + # Download the file + m365 spo file get --webUrl $siteUrl --url $fileUrl --asFile --path $localFilePath +} + +# Zip the downloaded files +$zipFilePath = "C:\DownloadedFiles\files.zip" +Compress-Archive -Path "$localDirectory\*" -DestinationPath $zipFilePath + +# Output the location of the zip file +Write-Host "Files have been zipped to: $zipFilePath" + +# Disconnect SharePoint online connection +m365 logout + +``` + +[!INCLUDE [More about CLI for Microsoft 365](../../docfx/includes/MORE-CLIM365.md)] + +*** + +## Contributors + +| Author(s) | +|-----------| +| [Valeras Narbutas](https://github.com/ValerasNarbutas) | + +[!INCLUDE [DISCLAIMER](../../docfx/includes/DISCLAIMER.md)] + diff --git a/scripts/spo-download-files-and-archive/assets/preview.png b/scripts/spo-download-files-and-archive/assets/preview.png new file mode 100644 index 0000000000000000000000000000000000000000..c07434bb0ea652f49f3f193b51b176feb039ec9e GIT binary patch literal 11109 zcmdUVc~sJC_b-i_V`XLKbQ;Z4$p$ee&Pf|Aha5`Ffzr%O5z7z>5l|^j5J8ba5WHCDbk6&`@4aii_mBHqzqN4rou9q;v!A^``?H^$ z$J`HTYpmA*007!9hn>9ufQ2LgV8KE4rK&4;DsppG{}#k}9oi45=rkNtoh%Of#qAdW zpej>y_RJF1`LgK4Cu0BrowxIU3tC~%&jJA3WiHOY_{0VA7%ZGc7|LW&sPFtBffC?M zjC>~E`t*HsIPCBNir2=EL8~jwYxk9}H#$#!_UzfVCVLICyZw0v&7X(>aefZ=;Cv_Ez`W@e{&&%N)3oeBwy_Gv5ewqp-8x zuKJu~qIf*U(gy(8xhNS3095}ZT?p8z`QLCDJ#-sH@-e&hbern;#}j=IUZDC3}rvL1Tg5jR--&*nQCToAIPAr_>cdrT5D;xol<5x$9%*si8 z`3shk$Es*`a+SX`b^LJ0;()?$Vo%*}{ypp2qBDvUeUeEVm&CDMAz3Vm^&)_ZhquzPP{C%iIf*QJ(C*M&uYz~c zuD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