Share via


PowerShell script to copy library files

Question

Thursday, December 12, 2019 6:58 AM

Hi All,
I have SP 2010 site(s) containing libraries and files.

Library (source) is a view filtered by the "document type" column showing documents based on each value of that column.

I need a script that creates "document type" as sub folder within that library and copy respective files to sub folder. ie. Library is main folder and document type should be sub folder and destination location is local drive. I'm using Sharegate tool, but all files are coming into one folder under library and again I need to create sub folders and move. Because library created as view and tool can't support my requirement. Please share your experience..

Advanced thanks..

All replies (14)

Friday, December 27, 2019 6:29 AM ✅Answered

Hi Jayanth b,

Try below script to create sub folders and copy files from SharePoint library to sub folders in your local drive.

$destination = "your local drive address"
$web = Get-SPWeb -Identity "your site url"
$list = $web.GetList("your document library url")
function ProcessFolder {
    param($folderUrl)
    $folder = $web.GetFolder($folderUrl)
    foreach ($file in $folder.Files) {
        #Ensure destination directory
    if($file.item['Document Type'] -eq $null){
    $destinationfolder = $destination + "/document"
    }else{
        $destinationfolder = $destination + "/" + $file.item['Document Type']
    }
        if (!(Test-Path -path $destinationfolder))
        {
            $dest = New-Item $destinationfolder -type directory
        }
        #Download file
        $binary = $file.OpenBinary()
        $stream = New-Object System.IO.FileStream($destinationfolder + "/" + $file.Name), Create
        $writer = New-Object System.IO.BinaryWriter($stream)
        $writer.write($binary)
        $writer.Close()
    }
}
#Download root files
ProcessFolder($list.RootFolder.Url)
#Download files in folders
foreach ($folder in $list.Folders) {
    ProcessFolder($folder.Url)
}

Best regards,

Emily Du

Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact [email protected].

SharePoint Server 2019 has been released, you can click

here to download it.
Click

here to learn new features. Visit the dedicated

forum to share, explore and talk to experts about SharePoint Server 2019.


Friday, December 13, 2019 8:56 AM

Hi Jayanth b,

Here’s a PowerShell Script to meet your needs:

#get the web
$web=get-spweb "your web url"
#get the library
$list=$web.lists['library name']
#get all files
$files=$List.RootFolder.files 
for($i = 0; $i -lt $files.Count){
$file=$files[$i]
#Check if the folde exists, if not create one
$foldername=$file.item['document type']
$folder=$list.ParentWeb.GetFolder($list.RootFolder.Url + "/" +$foldername)
if ($folder.Exists -eq $false)
   {
    #Create a Folder
    $folder = $list.AddItem("", [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, $foldername)
    $folder.Update();
   }
#move the file to the folder
$file.MoveTo($folder.Url + "/" + $file.name)
}

Best regards,

Emily Du


Friday, December 13, 2019 10:47 AM

Hi Emily,

Thank you.

I need copy instead of moving. So i used "CopyTo" in place of "MoveTo" as below..
$file.CopyTo("C:\ID\ + $folder.Url + "/" + $file.name)

and met with below error(s)

Exception calling "CopyTo" with "1" argument(s): "Value does not fall within the expected range."
At C:\xxxx\xxx\task.ps1:20 char:13

  • $file.CopyTo <<<< ("C:\Id\ + $folder.Url + "/" + $file.name)
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : DotNetMethodException

Tuesday, December 17, 2019 9:41 AM

Hi Jayanth b,

The function of the power shell provided by the previous reply is to create folders based on the document type in the SharePoint document library and move multiple documents to corresponding folders.

After executing the power shell, you just need to move folders to local drive by using Sharegate tool.

Best regards,

Emily Du


Friday, December 20, 2019 9:41 AM

Hi Jayanth b,

I'm checking how the things are going on about this issue. Whether the post helps you?

You can mark the post as answer if it helps.

Best regards,

Emily Du

Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact [email protected].

SharePoint Server 2019 has been released, you can click here to download it.
Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.


Monday, December 23, 2019 1:19 AM

Hi Jayanth b,

Is there any progress on this issue?

If you find any replies helpful to you, please remember to mark them as answers.

It will help others who meet the similar question in this forum.

Thank you for your understanding.

Best regards,

Emily Du

Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact [email protected].

SharePoint Server 2019 has been released, you can click

here to download it.
Click

here to learn new features. Visit the dedicated

forum to share, explore and talk to experts about SharePoint Server 2019.


Monday, December 23, 2019 4:59 AM

Hi Emily,

Thank you.

I need copy instead of moving. So i used "CopyTo" in place of "MoveTo" as below..
$file.CopyTo("C:\ID\ + $folder.Url + "/" + $file.name)

and met with below error(s)

Exception calling "CopyTo" with "1" argument(s): "Value does not fall within the expected range."
At C:\xxxx\xxx\task.ps1:20 char:13

  • $file.CopyTo <<<< ("C:\Id\ + $folder.Url + "/" + $file.name)
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : DotNetMethodException

Tuesday, December 24, 2019 9:48 AM | 1 vote

Hi Jayanth b,

I want to confirm whether you want to copy files from document library to local drive after creating folders based on the document type and moving the corresponding files into folders in the document library. If there is something wrong, you can point it out.

Please run the power shell provided by the first reply, then run the following power shell.

$destination = "your local drive address"
$web = Get-SPWeb -Identity "your site url"
$list = $web.GetList("your document library url")

function ProcessFolder {
    param($folderUrl)
    $folder = $web.GetFolder($folderUrl)
    foreach ($file in $folder.Files) {
        #Ensure destination directory
        $destinationfolder = $destination + "/" + $folder.Url 
        if (!(Test-Path -path $destinationfolder))
        {
            $dest = New-Item $destinationfolder -type directory 
        }
        #Download file
        $binary = $file.OpenBinary()
        $stream = New-Object System.IO.FileStream($destinationfolder + "/" + $file.Name), Create
        $writer = New-Object System.IO.BinaryWriter($stream)
        $writer.write($binary)
        $writer.Close()
    }
}

#Download root files
ProcessFolder($list.RootFolder.Url)
#Download files in folders
foreach ($folder in $list.Folders) {
    ProcessFolder($folder.Url)
}

Best regards,

Emily Du

Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact [email protected].

SharePoint Server 2019 has been released, you can click here to download it.
Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.


Thursday, December 26, 2019 5:23 AM

Hi Emily,

1st script ended with errors as said above. Although i ran 2nd script, it created respective library and  copied files.
But not created document type as sub folder. Again I have created sub folders manually and ran 2nd script but files not copied into sub folders.

many thanks...


Thursday, December 26, 2019 8:31 AM

Hi Jayanth b,

Here’ a new script for you. Its function is to create sub folders based on the document type in your local drive. It also can copy files from SharePoint library to sub folders in your local drive.

$destination = "your local drive address"
$web = Get-SPWeb -Identity "your site url"
$list = $web.GetList("your document library url")
function ProcessFolder {
    param($folderUrl)
    $folder = $web.GetFolder($folderUrl)
    foreach ($file in $folder.Files) {
        #Ensure destination directory
        $destinationfolder = $destination + "/" + $file.item['document type'] 
        if (!(Test-Path -path $destinationfolder))
        {
            $dest = New-Item $destinationfolder -type directory 
        }
        #Download file
        $binary = $file.OpenBinary()
        $stream = New-Object System.IO.FileStream($destinationfolder + "/" + $file.Name), Create
        $writer = New-Object System.IO.BinaryWriter($stream)
        $writer.write($binary)
        $writer.Close()
    }
}
#Download root files
ProcessFolder($list.RootFolder.Url)
#Download files in folders
foreach ($folder in $list.Folders) {
    ProcessFolder($folder.Url)
}

Best regards,

Emily Du

Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact [email protected].

SharePoint Server 2019 has been released, you can click

here to download it.
Click

here to learn new features. Visit the dedicated

forum to share, explore and talk to experts about SharePoint Server 2019.


Thursday, December 26, 2019 12:11 PM

Hi Emily,

Sorry to bother you. Above script copied files into target location without creating sub folders. 

If name not mentioned for first "Document type" as below, that files should go to "Documents" folder. Please suggest on this too.



FYI...  as you may know all attached screen shots are views, taken "document type" as column and grouped by document type.

Advance thanks


Friday, December 27, 2019 9:19 AM

Many many thanks Emily, this worked perfect.  :)


Monday, December 30, 2019 9:09 AM

Hi Jayanth b,

Thanks for marking my reply as answer. I’m pleased to know that the information is helpful to you. I

hope you are delighted with the service we provided you.

Here I will provide a brief summary of this post for your information.

Request/Expectation:

There’re many files and a column named Document Type in the document library.

Need a power shell script to create sub folders based on the Document Type column in the local drive,

copy and move files into corresponding sub folders from SharePoint document library to local drive.

====================

Solution Summary:

$destination = "your local drive address"
$web = Get-SPWeb -Identity "your site url"
$list = $web.GetList("your document library url")
function ProcessFolder {
    param($folderUrl)
    $folder = $web.GetFolder($folderUrl)
    foreach ($file in $folder.Files) {
        #Ensure destination directory
    if($file.item['Document Type'] -eq $null){
    $destinationfolder = $destination + "/document"
    }else{
        $destinationfolder = $destination + "/" + $file.item['Document Type']
    }
        if (!(Test-Path -path $destinationfolder))
        {
            $dest = New-Item $destinationfolder -type directory
        }
        #Download file
        $binary = $file.OpenBinary()
        $stream = New-Object System.IO.FileStream($destinationfolder + "/" + $file.Name), Create
        $writer = New-Object System.IO.BinaryWriter($stream)
        $writer.write($binary)
        $writer.Close()
    }
}
#Download root files
ProcessFolder($list.RootFolder.Url)
#Download files in folders
foreach ($folder in $list.Folders) {
    ProcessFolder($folder.Url)
}

Best regards,

Emily Du

Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact [email protected].

SharePoint Server 2019 has been released, you can click here to download it.
Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.


Wednesday, March 25, 2020 6:33 PM

I've tried about 10 different Powershells to copy folder structure and Docs. to harddrive (must be automatic). They all work fine as long as there are documents in the folder. If you have a folder that you need to copy to harrdrive with powershell and it's empty, then it gets ignored. I need ALL the folders, empty or not copied to file system (and it must be Powershell). So far I get all folders and docs. unless folder is empty. Used the script above also.