{"id":1213,"date":"2012-10-15T10:23:22","date_gmt":"2012-10-15T10:23:22","guid":{"rendered":"https:\/\/poiseddevelopers.com\/reality-tech\/?p=1213"},"modified":"2024-04-26T12:41:40","modified_gmt":"2024-04-26T12:41:40","slug":"copy-files-in-folders-from-a-document-library-to-disk","status":"publish","type":"post","link":"https:\/\/poiseddevelopers.com\/reality-tech\/copy-files-in-folders-from-a-document-library-to-disk\/","title":{"rendered":"Copy files in folders from a Document Library to disk"},"content":{"rendered":"<h4>Copy SharePoint files in folders to disk<\/h4>\n<p>A common request is extracting all the files within folders in a document library. The simple script below allows one to specify a source folder, library, web, and output location. The $TargetRootFolder can be found as a URL parameter in the URL when viewing the target folder for migrating. The script preserves the full URL structure including all folders and subfolders. Recursion makes this clean and simple.<\/p>\n<pre lang=\"php\"> $webUrl = \"http :\/\/SharePoint\/sites\/MyWeb\"\r\n$TargetLib=\"MyLibrary\"\r\n$destination = \"L:OutputLocation\"\r\n#derived right from the URL when viewing the folder, this works:\r\n$TargetRootFolder=\"%2MyWeb%2SubFolder\"\r\n \r\n$web = Get-SPWeb -Identity $webUrl\r\n \r\nfunction ProcessFolder \r\n{\r\n    param($folderUrl)    \r\n    $folder = $web.GetFolder($folderUrl)    \r\n    if (!$Folder.exists)\r\n    {\r\n        Write-Host -ForegroundColor DarkRed \"Whoops, folder at source does not exist, please recheck\"\r\n    }\r\n    else\r\n    {\r\n     \r\n    $destinationfolder = $destination + \"\/\" + $folder.Url\r\n \r\n    if (!(Test-Path -path $destinationfolder))         \r\n    {             \r\n        $dest = New-Item $destinationfolder -type directory          \r\n    }   \r\n \r\n    foreach ($file in $folder.Files) \r\n    {\r\n    $binary = $file.OpenBinary()         \r\n    $stream = New-Object System.IO.FileStream($destinationfolder + \"\/\" + $file.Name), Create         \r\n    $writer = New-Object System.IO.BinaryWriter($stream)         \r\n    $writer.write($binary)         \r\n    $writer.Close()  \r\n    Write-Host \"+\" -NoNewline\r\n    }\r\n        foreach ($sf in $folder.SubFolders)\r\n        {\r\n            ProcessFolder($sf.url)  #Not quite Ackerman's function, this is first order recursion\r\n        }   \r\n    }\r\n}\r\nProcessFolder($TargetRootFolder)<\/pre>\n<p>Now, if you need to ensure no documents ever overwrite, such as if you rename on the fly, here\u2019s a bit of code that will cycle through optional filenames until one is found that does not exist.<\/p>\n<pre lang=\"php\"> $inc=$null;\r\nwhile (test-path $OutFileName)\r\n{\r\n$JPname=\"$($JPTitle)DUP$($inc)$($JPName.Substring($JPName.lastindexof(\".\")))\"\r\n$OutFileName=$destinationfolder + \"\/\" + $inc+$JPName\r\n$Inc++;\r\nWrite-Host \"!\" -NoNewline -ForegroundColor DarkRed\r\n}   <\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Copy SharePoint files in folders to disk A common request is extracting all the files within folders in a document library. The simple script below allows one to specify a source folder, library, web, and output location. The $TargetRootFolder can be found as a URL parameter in the URL when viewing the target folder for [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":1217,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[26],"tags":[],"class_list":["post-1213","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell"],"acf":[],"_links":{"self":[{"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/posts\/1213","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/comments?post=1213"}],"version-history":[{"count":1,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/posts\/1213\/revisions"}],"predecessor-version":[{"id":1219,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/posts\/1213\/revisions\/1219"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/media\/1217"}],"wp:attachment":[{"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/media?parent=1213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/categories?post=1213"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/tags?post=1213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}