Wednesday, December 12, 2018

Archive documents from site using PowerShell

Hi All,

Recently, I worked on a PowerShell script to archive documents from multiple document libraries to local machine. It was a SharePoint 2010 site and links for all the documents are stored in a CSV file. There were about 9K files. The script worked well but it has some limitations. Within the link, any folder name/file name can't have commas (,) otherwise it won't create a structure in local machine. All the documents will be archived in local machine within the folder structure being created from the URL.

Here's the script below.

Add-PSSnapin Microsoft.SharePoint.PowerShell


$Files = Import-Csv "C:\Microsoft PowerShell\GDWFilesTask\GDW Files task\LessFiles.txt"

$TargetPath = "E:\GDW Files"

$wc = New-Object System.Net.WebClient
$wc.UseDefaultCredentials = $true

ForEach ($File in $Files){ 
  
  $FPath = $($File.Path)

  $FolPath = $FPath

  $String = $FolPath.ToString()

  $String -replace ',',' '
  

  [System.Collections.ArrayList]$ArrayList = $String.split("/")
  $ArrayList[0] = $TargetPath;
  $ArrayList[2] = "GDW"
  $ArrayList.RemoveAt(1)
  $ArrayList = $ArrayList -notlike "*.*"

  $b = ($ArrayList -join "\")

  New-Item -ItemType directory -Path $b  

  $FileName = Split-Path $FPath -leaf  

  $output = $b +"\"+ $FileName

  $wc.DownloadFile($FPath, $output)

  #write-host $FPath -ForegroundColor Green
  #write-host $output -ForegroundColor Yellow

  write-host $FileName " is downloaded.`n" -ForegroundColor Yellow
  
}


LessFiles.txt
Path
http://gdw.myaccobrands.com/Teams/BusinessTransformation/Restructuring/2012 Restructuring/PFS Model Change Phase III Tournis John Severance Calc Estimator 10.12.12.xls
http://gdw.myaccobrands.com/Processes/TPB/Documents/Audit/Projects/PROJ-61-Canada Fin/Oracle Programs Freight Distribution allocation logic into GDW for Canada.xlsx
http://gdw.myaccobrands.com/Teams/BusinessTransformation/Restructuring/2013 Restructuring/US Ops Reorg RPA-2013-15 Restructuring Project Approval Form_OPS May 10 2013.xlsx
http://gdw.myaccobrands.com/Teams/BusinessTransformation/Restructuring/2013 Restructuring/IT Restructuring RPA-2013-14 Restructuring Project Approval Form_IT April 29 2013.xlsx
http://gdw.myaccobrands.com/Teams/BusinessTransformation/Restructuring/2013 Restructuring/Canada Call Center Restructuring Project Approval Form - March 18 2013.xlsx
http://gdw.myaccobrands.com/Teams/BusinessTransformation/Restructuring/2013 Restructuring/CAN Footprint Phase II Boulder Final DCF Model - March 18 2013.xls


I followed the below references to develop the code

http://keith-wood.name/countdown.html
https://www.w3schools.com/howto/howto_google_translate.asp
http://www.openxrest.com/translatejs/
https://www.script-tutorials.com/demos/42/index.html
https://social.technet.microsoft.com/Forums/office/en-US/1351c3ed-e673-4d6c-ba3b-27a50ced7e1a/translate-sharepoint-custom-list-forms?forum=sharepointgenerallegacy

https://www.c-sharpcorner.com/UploadFile/mahesh/how-to-add-items-to-a-dictionary-with-C-Sharp/


Thanks,
Kunal