locked
Move Folders of SharePoint Document Library using powershell RRS feed

  • Question

  • Hi;

    I search a powershell to move Folders with their contained (files + subfolders) of SharePoint Document Library.

    Regards

    Monday, December 21, 2015 9:08 AM

Answers

  • Hi

    make sure this path exists

    $trelativeUrl + "/" +$sourcename 

    if not create folder with $sourcename and try

    here is nice article about the same

    http://codebroth.com/copying-sharepoint-2010-document-libraries-wit-powershell/

    http://sharepoint.stackexchange.com/questions/41884/copy-a-folder-from-one-library-to-another-and-maintain-permissions-on-the-folder


    Rajendra Singh
    sharepoint Adda
    Walking on water and developing software from a specification are easy if both are frozen
    If a post answers your question, please click Mark As Answer on that post and Vote as Helpful If a post answers your question, please click Mark As Answer on that post and Vote as Helpful

    • Marked as answer by soussou97 Thursday, December 24, 2015 6:15 PM
    Monday, December 21, 2015 11:10 AM
  • Hi Rajendra;

    Why the target folder must existed ?

    $trelativeUrl =  /sites/XXX/GestionDoc/Archives this forlder exists

    $sourcename = "Etablissement"

    $trelativeUrl + "/" +$sourcename = /sites/XXX/GestionDoc/Archives/Etablissement this folder dosn't exists my aim is to copy :  From SOURCE /sites/XXX/GestionDoc/Etablissement To TARGET  /sites/XXX/GestionDoc/Archives/Etablissement

    Regards;

    • Marked as answer by soussou97 Thursday, December 24, 2015 6:15 PM
    Monday, December 21, 2015 11:28 AM
  • Hi

    It seems that folder has to be created to copy  into destination  library.

    here is msdn blog

    https://msdn.microsoft.com/EN-US/library/office/microsoft.sharepoint.spfolder.copyto.aspx?f=255&MSPPError=-2147217396


    Rajendra Singh
    sharepoint Adda
    Walking on water and developing software from a specification are easy if both are frozen
    If a post answers your question, please click Mark As Answer on that post and Vote as Helpful If a post answers your question, please click Mark As Answer on that post and Vote as Helpful

    • Marked as answer by soussou97 Thursday, December 24, 2015 6:15 PM
    Monday, December 21, 2015 11:41 AM
  • Hi Rajendra;

    Ok its works fine. I would like also for each document to empty all meta data fields and save only the name document.

    • Marked as answer by soussou97 Thursday, December 24, 2015 6:15 PM
    Monday, December 21, 2015 3:59 PM
  • Run below PowerShell after copying all contains in new folder, It will make metadata blank
    foreach($file in $folder.Files)
    {
        $file.CheckOut();
        $file.Properties['My Cust Prop 1'] = ''
        $file.Properties['My Cust Prop 2'] = ''
        $file.SystemUpdate(false);
        $file.CheckIn();
    }

    Thanks

    Jaydev

    • Marked as answer by soussou97 Thursday, December 24, 2015 6:15 PM
    Monday, December 21, 2015 5:01 PM
  • Hi jaydev;

    Your code not working : any change on the metadata ?

    I try also this but the metadata are already not empty :

    foreach ($file in $files)
      {
       try
       {
        $targetFile = $subdossier.URL + "/" + $file.Name
        $file.CopyTo($targetFile, $true)
        [Microsoft.SharePoint.SPFile]$spFile = $web.GetFile($targetFile)

    if($spFile.CheckOutStatus -eq "None")
        {
         $spFile.CheckOut()
         $fileItem = $spFile.Item
         $taxField1 = $fileItem.Fields["Revue"] 
         $taxField2 = $fileItem.Fields["Date"]
         $taxField3 = $fileItem.Fields["Typologie"]
         $taxField1.SetFieldValue($fileItem,"")
         $taxField2.SetFieldValue($fileItem,"")
         $taxField3.SetFieldValue($fileItem,"")
         $fileItem.Update()
         $spFile.CheckIn("mise-à-jour du champ")
        }
        else
        {
         $out = "## Le fichier $fname est vérouillé "
         $out | out-file $traceFile -append
          }

    • Marked as answer by soussou97 Thursday, December 24, 2015 6:15 PM
    Tuesday, December 22, 2015 10:32 AM
  • Please check below link and code

    https://social.technet.microsoft.com/Forums/sharepoint/en-US/d05f8d8e-7a28-4909-b8b4-9c8fc8e0d501/powershell-to-clear-delete-values-in-site-columns-fields?forum=sharepointgeneralprevious

    $web = get-spweb http://my
    $list = $web.Lists["documents"] -as [Microsoft.SharePoint.SPDocumentLibrary]
    $url = [String]::Format("{0}{1}",$web.Url, $item.File.Url)
    $file = $web.GetFile($url)
    $file.CheckOut()
    $fi = $file.Item
    $tf = $fi.Fields["MetadataField"] -as [Microsoft.SharePoint.Taxonomy.TaxonomyField]
    $fv = $tf.GetFieldValue("");
    $tf.SetFieldValue($fi,$fv)
    $fi.Update()
    $file.CheckIn("")
    • Marked as answer by soussou97 Thursday, December 24, 2015 6:15 PM
    Tuesday, December 22, 2015 12:07 PM
  • Hi;

    When I try this code I have the following error on the method « SetFieldValue » :

    Echec to call the method because [Microsoft.SharePoint.SPFieldChoice] not contains method named « SetFieldValue ».

    My field its a field of type ‘Choice’ : how it updated to empty ?

    regards 


    • Edited by soussou97 Tuesday, December 22, 2015 12:43 PM
    • Marked as answer by soussou97 Thursday, December 24, 2015 6:15 PM
    Tuesday, December 22, 2015 12:42 PM

All replies

  • Hi;

    My script is :

    Function MoveFile
    {
      param ( [Microsoft.SharePoint.SPWeb]$web,
      [Microsoft.SharePoint.SPList]$list,
      [Microsoft.SharePoint.SPFolder]$sourcefolder,
      [Microsoft.SharePoint.SPFolder]$targetfolder  )
     
     $sourcename =  $sourcefolder.Name
     $trelativeUrl = $targetfolder.URL
     $srelativeUrl = $sourcefolder.URL
     $sfolder = $web.GetFolder($srelativeUrl)
     $tfolder = $web.GetFolder($trelativeUrl + "/" +$sourcename)
     if($tfolder.Exists -eq $true)
      {
        # si le dossier existe déjà
        $out = "Le dossier $sourcename existe déjà!"
        $out | out-file $traceFile -append
      }
     
     if($sfolder.Exists -eq $true)
      {
        # si le dossier existe déjà
        $sfolder.CopyTo($trelativeUrl + "/" +$sourcename)
        $out = "Le dossier $sourcename a été déplacé avec succès"
        $out | out-file $traceFile -append
      }
    }

    But I have a error on the method sfolder.CopyTo :

    Exception lors de l'appel de « CopyTo » avec « 1 » argument(s) : « L'URL « /sites/XXX/Lists/GestionDocumentaire/Paris/Etablissement » n'est pas valide. Elle fait pe
    ut-être référence à un fichier ou dossier inexistant. Elle se réfère peut-être également à un fichier ou dossier valide qui ne figure pas dans le site Web actuel. »

    Monday, December 21, 2015 10:51 AM
  • Hi

    make sure this path exists

    $trelativeUrl + "/" +$sourcename 

    if not create folder with $sourcename and try

    here is nice article about the same

    http://codebroth.com/copying-sharepoint-2010-document-libraries-wit-powershell/

    http://sharepoint.stackexchange.com/questions/41884/copy-a-folder-from-one-library-to-another-and-maintain-permissions-on-the-folder


    Rajendra Singh
    sharepoint Adda
    Walking on water and developing software from a specification are easy if both are frozen
    If a post answers your question, please click Mark As Answer on that post and Vote as Helpful If a post answers your question, please click Mark As Answer on that post and Vote as Helpful

    • Marked as answer by soussou97 Thursday, December 24, 2015 6:15 PM
    Monday, December 21, 2015 11:10 AM
  • Hi Rajendra;

    Why the target folder must existed ?

    $trelativeUrl =  /sites/XXX/GestionDoc/Archives this forlder exists

    $sourcename = "Etablissement"

    $trelativeUrl + "/" +$sourcename = /sites/XXX/GestionDoc/Archives/Etablissement this folder dosn't exists my aim is to copy :  From SOURCE /sites/XXX/GestionDoc/Etablissement To TARGET  /sites/XXX/GestionDoc/Archives/Etablissement

    Regards;

    • Marked as answer by soussou97 Thursday, December 24, 2015 6:15 PM
    Monday, December 21, 2015 11:28 AM
  • Hi

    It seems that folder has to be created to copy  into destination  library.

    here is msdn blog

    https://msdn.microsoft.com/EN-US/library/office/microsoft.sharepoint.spfolder.copyto.aspx?f=255&MSPPError=-2147217396


    Rajendra Singh
    sharepoint Adda
    Walking on water and developing software from a specification are easy if both are frozen
    If a post answers your question, please click Mark As Answer on that post and Vote as Helpful If a post answers your question, please click Mark As Answer on that post and Vote as Helpful

    • Marked as answer by soussou97 Thursday, December 24, 2015 6:15 PM
    Monday, December 21, 2015 11:41 AM
  • Hi Rajendra;

    Ok its works fine. I would like also for each document to empty all meta data fields and save only the name document.

    • Marked as answer by soussou97 Thursday, December 24, 2015 6:15 PM
    Monday, December 21, 2015 3:59 PM
  • Run below PowerShell after copying all contains in new folder, It will make metadata blank
    foreach($file in $folder.Files)
    {
        $file.CheckOut();
        $file.Properties['My Cust Prop 1'] = ''
        $file.Properties['My Cust Prop 2'] = ''
        $file.SystemUpdate(false);
        $file.CheckIn();
    }

    Thanks

    Jaydev

    • Marked as answer by soussou97 Thursday, December 24, 2015 6:15 PM
    Monday, December 21, 2015 5:01 PM
  • Moving folders along with subfolders hope this code helps when required:
    http://adicodes.com/move-folders-of-sharepoint-document-library-using-object-model/

    You may also take help from below given earlier discussed threads having suggested solutions:

    Move files between document libraries in the same Site Collection

    Move files between document libraries in the same Site Collection

    How can one move folders and files across sites and document libraries in SharePoint and maintain their version histories

    Hope this helps you!


    Easily migrate anything from Office 365, File Servers, Public Folders, OneDrive and SharePoint to SharePoint Online easily with Lepide SharePoint Migrator.

    Tuesday, December 22, 2015 4:48 AM
  • Hi jaydev;

    Your code not working : any change on the metadata ?

    I try also this but the metadata are already not empty :

    foreach ($file in $files)
      {
       try
       {
        $targetFile = $subdossier.URL + "/" + $file.Name
        $file.CopyTo($targetFile, $true)
        [Microsoft.SharePoint.SPFile]$spFile = $web.GetFile($targetFile)

    if($spFile.CheckOutStatus -eq "None")
        {
         $spFile.CheckOut()
         $fileItem = $spFile.Item
         $taxField1 = $fileItem.Fields["Revue"] 
         $taxField2 = $fileItem.Fields["Date"]
         $taxField3 = $fileItem.Fields["Typologie"]
         $taxField1.SetFieldValue($fileItem,"")
         $taxField2.SetFieldValue($fileItem,"")
         $taxField3.SetFieldValue($fileItem,"")
         $fileItem.Update()
         $spFile.CheckIn("mise-à-jour du champ")
        }
        else
        {
         $out = "## Le fichier $fname est vérouillé "
         $out | out-file $traceFile -append
          }

    • Marked as answer by soussou97 Thursday, December 24, 2015 6:15 PM
    Tuesday, December 22, 2015 10:32 AM
  • Please check below link and code

    https://social.technet.microsoft.com/Forums/sharepoint/en-US/d05f8d8e-7a28-4909-b8b4-9c8fc8e0d501/powershell-to-clear-delete-values-in-site-columns-fields?forum=sharepointgeneralprevious

    $web = get-spweb http://my
    $list = $web.Lists["documents"] -as [Microsoft.SharePoint.SPDocumentLibrary]
    $url = [String]::Format("{0}{1}",$web.Url, $item.File.Url)
    $file = $web.GetFile($url)
    $file.CheckOut()
    $fi = $file.Item
    $tf = $fi.Fields["MetadataField"] -as [Microsoft.SharePoint.Taxonomy.TaxonomyField]
    $fv = $tf.GetFieldValue("");
    $tf.SetFieldValue($fi,$fv)
    $fi.Update()
    $file.CheckIn("")
    • Marked as answer by soussou97 Thursday, December 24, 2015 6:15 PM
    Tuesday, December 22, 2015 12:07 PM
  • Hi;

    When I try this code I have the following error on the method « SetFieldValue » :

    Echec to call the method because [Microsoft.SharePoint.SPFieldChoice] not contains method named « SetFieldValue ».

    My field its a field of type ‘Choice’ : how it updated to empty ?

    regards 


    • Edited by soussou97 Tuesday, December 22, 2015 12:43 PM
    • Marked as answer by soussou97 Thursday, December 24, 2015 6:15 PM
    Tuesday, December 22, 2015 12:42 PM