none
[URGENT]download document library files to shared path location RRS feed

  • Question

  • i have some list of sub-sites and bulk of  files in the document libraries and the objective is to download document library files as per below example to shared path location

    please help me if any tool could help this or script

    sharepoint site

    document library (Source)

    First name lastname  employeeID

    XX yy file                    123456

    shared location (Target)

    XX YY_123456 (to create folder in this name) ->(To place inside this folder source document library file)


    Blitz



    • Edited by sree_23 Friday, July 12, 2019 12:32 PM
    Friday, July 12, 2019 11:06 AM

Answers

  • Hi,

    You could try below script.

    ######################## Start Variables ########################
    ######################## Varun's Script######################
    $destination = "C:\Lee\FileDownload"
    $webUrl = "http://sp"
    $listUrl = "http://sp/mydoc2"
    ##############################################################
    
    $web = Get-SPWeb -Identity $webUrl
    $list = $web.GetList($listUrl)
    
    function ProcessFolder {
        param($folderUrl)
        $folder = $web.GetFolder($folderUrl)
        foreach ($file in $folder.Files) {
            #Ensure destination directory
    		$item=$file.Item
            $destinationfolder = $destination + "/" + $folder.Url+ "/" +$item["FirstName"]+$item["LastName"]+"_"+$item["EmployeeID"]
            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,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    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.

    • Marked as answer by sree_23 Tuesday, July 23, 2019 10:14 AM
    Tuesday, July 23, 2019 8:06 AM
  • Hi, 

    To get user name from people field, try below.

    $userfield = New-Object Microsoft.SharePoint.SPFieldUserValue($web,$item["FirstName"].ToString());            
    		$userName=$userfield.User.DisplayName; 
            $destinationfolder = $destination + "/" + $folder.Url+ "/" +$userName+$item["LastName"]+"_"+$item["EmployeeID"]

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    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.

    • Marked as answer by sree_23 Wednesday, July 24, 2019 6:52 AM
    Wednesday, July 24, 2019 6:22 AM
  • Hi,

    Sample demo to check null value.

    $destinationfolder="";
    		if(($item["FirstName"] -eq $null) -and ($item["EmployeeID"] -eq $null)){
    			$destinationfolder=$destination + "/" + $folder.Url+ "/"
    		}
    		else{
    		$userfield = New-Object Microsoft.SharePoint.SPFieldUserValue($web,$item["FirstName"].ToString());		
    		$userName=$userfield.User.DisplayName; 
            $destinationfolder = $destination + "/" + $folder.Url+ "/" +$userName+$item["LastName"]+"_"+$item["EmployeeID"]
            }

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    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.

    • Marked as answer by sree_23 Wednesday, July 24, 2019 7:41 AM
    Wednesday, July 24, 2019 7:10 AM
  • Hi,

    Comment the folder url for $destinationfolder so it won’t create folder for each document/sub folder.

    ######################## Start Variables ########################
    ######################## Varun's Script######################
    $destination = "C:\Lee\FileDownload"
    $webUrl = "http://sp"
    ##############################################################
    
    $web = Get-SPWeb -Identity $webUrl
    $listUrls=@("http://sp/mydoc2","http://sp/mydoc")
    
    function ProcessFolder {
        param($folderUrl)
        $folder = $web.GetFolder($folderUrl)
        foreach ($file in $folder.Files) {
            #Ensure destination directory
    		$item=$file.Item
            $destinationfolder="";
    		if(($item["FirstName"] -eq $null) -and ($item["EmployeeID"] -eq $null)){
    			$destinationfolder=$destination + "/" # comment folder + $folder.Url+ "/"
    		}
    		else{
    		$userfield = New-Object Microsoft.SharePoint.SPFieldUserValue($web,$item["FirstName"].ToString());		
    		$userName=$userfield.User.DisplayName; 
            $destinationfolder = $destination + "/" +$userName+$item["LastName"]+"_"+$item["EmployeeID"] #+ $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()
            }
    }
    
    function ProcessLib($listUrl){
    	$list = $web.GetList($listUrl)
    	
    	#Download root files
    	ProcessFolder($list.RootFolder.Url)
    	#Download files in folders
    	foreach ($folder in $list.Folders) {
    	    ProcessFolder($folder.Url)
    	}
    }
    
     foreach($listUrl in $listUrls) {
       ProcessLib($listUrl)
     }

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    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.

    • Marked as answer by sree_23 Tuesday, July 30, 2019 9:45 AM
    Tuesday, July 30, 2019 8:32 AM

All replies

  • Hi,

    You could try the PowerShell below.

    ######################## Start Variables ########################
    ######################## Varun's Script######################
    $destination = "C:\\tools\\Folder"
    $webUrl = "<Url of the specific site>"
    $listUrl = "<Url of the specific list. This url is complete Url and NOT relative url>"
    ##############################################################
    
    $web = Get-SPWeb -Identity $webUrl
    $list = $web.GetList($listUrl)
    
    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)
    }

    Here is the thread for your reference.

    https://blogs.msdn.microsoft.com/varun_malhotra/2012/02/08/sharepoint-2010-powershell-download-all-files-in-document-library-to-network-share-file-share/

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    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, July 15, 2019 3:23 AM
  • Hi, 

    Below line use to create folder, so just update it accordingly based on you item field logic.

    $dest = New-Item $destinationfolder -type directory

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    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.

    Tuesday, July 16, 2019 8:35 AM
  • Thank you.

    Item field based on the empid items ,it should be updated in that respective Firstname,Lastname empid folder so my query is it achievable via powershell script or Jquery and appreciate your kind help for the same.


    Blitz


    • Edited by sree_23 Tuesday, July 16, 2019 9:19 AM
    Tuesday, July 16, 2019 9:06 AM
  • Hi, 

    You could join the values together as folder name before create the folder, PowerShell is fine.

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    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, July 17, 2019 8:18 AM
  • Kindly help

    Blitz

    Monday, July 22, 2019 6:26 AM
  • Hi,

    I would kindly request kind help for copying the Item field based on the empid items ,it should be updated in that respective Firstname,Lastname empid folder in shared drive


    Blitz

    Tuesday, July 23, 2019 6:29 AM
  • Hi,

    You could try below script.

    ######################## Start Variables ########################
    ######################## Varun's Script######################
    $destination = "C:\Lee\FileDownload"
    $webUrl = "http://sp"
    $listUrl = "http://sp/mydoc2"
    ##############################################################
    
    $web = Get-SPWeb -Identity $webUrl
    $list = $web.GetList($listUrl)
    
    function ProcessFolder {
        param($folderUrl)
        $folder = $web.GetFolder($folderUrl)
        foreach ($file in $folder.Files) {
            #Ensure destination directory
    		$item=$file.Item
            $destinationfolder = $destination + "/" + $folder.Url+ "/" +$item["FirstName"]+$item["LastName"]+"_"+$item["EmployeeID"]
            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,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    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.

    • Marked as answer by sree_23 Tuesday, July 23, 2019 10:14 AM
    Tuesday, July 23, 2019 8:06 AM
  • Hi, 

    To get user name from people field, try below.

    $userfield = New-Object Microsoft.SharePoint.SPFieldUserValue($web,$item["FirstName"].ToString());            
    		$userName=$userfield.User.DisplayName; 
            $destinationfolder = $destination + "/" + $folder.Url+ "/" +$userName+$item["LastName"]+"_"+$item["EmployeeID"]

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    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.

    • Marked as answer by sree_23 Wednesday, July 24, 2019 6:52 AM
    Wednesday, July 24, 2019 6:22 AM
  • Hi,

    Sample demo to check null value.

    $destinationfolder="";
    		if(($item["FirstName"] -eq $null) -and ($item["EmployeeID"] -eq $null)){
    			$destinationfolder=$destination + "/" + $folder.Url+ "/"
    		}
    		else{
    		$userfield = New-Object Microsoft.SharePoint.SPFieldUserValue($web,$item["FirstName"].ToString());		
    		$userName=$userfield.User.DisplayName; 
            $destinationfolder = $destination + "/" + $folder.Url+ "/" +$userName+$item["LastName"]+"_"+$item["EmployeeID"]
            }

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    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.

    • Marked as answer by sree_23 Wednesday, July 24, 2019 7:41 AM
    Wednesday, July 24, 2019 7:10 AM
  • Kindly help 

    Blitz

    Monday, July 29, 2019 6:47 AM
  • Hi,

    Download files to library name named folder.

    ######################## Start Variables ########################
    ######################## Varun's Script######################
    $destination = "C:\Lee\FileDownload"
    $webUrl = "http://sp"
    ##############################################################
    
    $web = Get-SPWeb -Identity $webUrl
    $listUrls=@("http://sp/mydoc2","http://sp/mydoc")
    
    function ProcessFolder {
        param($folderUrl)
        $folder = $web.GetFolder($folderUrl)
        foreach ($file in $folder.Files) {
            #Ensure destination directory
    		$item=$file.Item
            $destinationfolder="";
    		if(($item["FirstName"] -eq $null) -and ($item["EmployeeID"] -eq $null)){
    			$destinationfolder=$destination + "/" + $folder.Url+ "/"
    		}
    		else{
    		$userfield = New-Object Microsoft.SharePoint.SPFieldUserValue($web,$item["FirstName"].ToString());		
    		$userName=$userfield.User.DisplayName; 
            $destinationfolder = $destination + "/" + $folder.Url+ "/" +$userName+$item["LastName"]+"_"+$item["EmployeeID"]
            }
            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()
            }
    }
    
    function ProcessLib($listUrl){
    	$list = $web.GetList($listUrl)
    	
    	#Download root files
    	ProcessFolder($list.RootFolder.Url)
    	#Download files in folders
    	foreach ($folder in $list.Folders) {
    	    ProcessFolder($folder.Url)
    	}
    }
    
     foreach($listUrl in $listUrls) {
       ProcessLib($listUrl)
     }


    Best Regards,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    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, July 29, 2019 7:29 AM
  • Kindly help

    Blitz

    Tuesday, July 30, 2019 6:45 AM
  • Hi,

    Comment the folder url for $destinationfolder so it won’t create folder for each document/sub folder.

    ######################## Start Variables ########################
    ######################## Varun's Script######################
    $destination = "C:\Lee\FileDownload"
    $webUrl = "http://sp"
    ##############################################################
    
    $web = Get-SPWeb -Identity $webUrl
    $listUrls=@("http://sp/mydoc2","http://sp/mydoc")
    
    function ProcessFolder {
        param($folderUrl)
        $folder = $web.GetFolder($folderUrl)
        foreach ($file in $folder.Files) {
            #Ensure destination directory
    		$item=$file.Item
            $destinationfolder="";
    		if(($item["FirstName"] -eq $null) -and ($item["EmployeeID"] -eq $null)){
    			$destinationfolder=$destination + "/" # comment folder + $folder.Url+ "/"
    		}
    		else{
    		$userfield = New-Object Microsoft.SharePoint.SPFieldUserValue($web,$item["FirstName"].ToString());		
    		$userName=$userfield.User.DisplayName; 
            $destinationfolder = $destination + "/" +$userName+$item["LastName"]+"_"+$item["EmployeeID"] #+ $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()
            }
    }
    
    function ProcessLib($listUrl){
    	$list = $web.GetList($listUrl)
    	
    	#Download root files
    	ProcessFolder($list.RootFolder.Url)
    	#Download files in folders
    	foreach ($folder in $list.Folders) {
    	    ProcessFolder($folder.Url)
    	}
    }
    
     foreach($listUrl in $listUrls) {
       ProcessLib($listUrl)
     }

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    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.

    • Marked as answer by sree_23 Tuesday, July 30, 2019 9:45 AM
    Tuesday, July 30, 2019 8:32 AM
  • Many Thanks.working as expected.



    • Edited by sree_23 Wednesday, July 31, 2019 12:14 PM
    Tuesday, July 30, 2019 9:45 AM
  • Hi,

    how to  to log the files for example how many files copied from source and how many got saved into destination

    Thanks


    Blitz

    Wednesday, July 31, 2019 8:47 AM