Quantcast
Channel: SharePoint 2013 - General Discussions and Questions forum
Viewing all articles
Browse latest Browse all 26374

need to move documents into folders in the same library using powershell

$
0
0

can someone assist in my script? I need to move all my documents where a Column value = "a value"  and put those in a folder named that "A value" and so fourth.

I started this powershell

$WebURL = "https://abc.com/test/";
$ListDisplayName = "Shared Documents";
$ArchiveFolderName = "Archive";
function LoadWSSAssembly{write-host "Loading WSS Assembly..."  
						[System.Reflection.Assembly]::Load("Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")
						 write-host "Done..."  }
[Microsoft.SharePoint.SPSite] $Site = New-Object Microsoft.SharePoint.SPSite($WebURL);
[Microsoft.SharePoint.SPWeb] $Web = $Site.OpenWeb();
[Microsoft.SharePoint.SPList] $List = $Web.Lists[$ListDisplayName]; 
[Microsoft.SharePoint.SPFolder] $FolderToMoveTo = getArchiveFolder $List;
function getArchiveFolder([Microsoft.SharePoint.SPList]$List)
        { 
		#Get Reference to the Archive Root Folder:
		[Microsoft.SharePoint.SPFolder] $archiveRoot = getFolder $List.RootFolder $ArchiveFolderName; 
		#Get Reference to the daily archive folder:
		$nowTimeInString = [System.DateTime]::Now.ToString("yyyyMMdd");
		[Microsoft.SharePoint.SPFolder] $todaysArchiveFolder =  getFolder $archiveRoot $nowTimeInString;
		return $todaysArchiveFolder;
        }
		function getFolder([Microsoft.SharePoint.SPFolder]$Folder, [System.String] $folderName) 
		{ 
		write-host "Searching for folder '" $folderName "' in SPFolder located at " $Folder.Url "...";
		if (!(folderExists $Folder $folderName))
        {     
		write-host "Folder Not Found - Creating a new one...";
		$folderItem = $list.Items.Add($Folder.ServerRelativeUrl,[Microsoft.SharePoint.SPFileSystemObjectType]::Folder,$folderName);
		$folderItem.SystemUpdate();
		$list.Update();
		#Need to get a fresh reference of the folder to ensure we can see the new one just created: 
		$Folder = $Folder.ParentWeb.GetFolder($Folder);
		} 
		return $Folder.SubFolders  where {$_.Name -eq $FolderName};}
		function folderExists([Microsoft.SharePoint.SPFolder] $Folder, [string]$FolderName)
		{  
		return !(($Folder.SubFolders  where {$_.Name -eq $FolderName}) -eq $null);
		}
		function moveItems
		{   
		trap
		{
		#make sure we dispose of these in the event of an error to avoid memory leaks: 
		write-host "Error - disposing of objects...";
        $Web.Dispose();
        $Site.Dispose();
		}
        [Microsoft.SharePoint.SPSite] $Site = New-Object Microsoft.SharePoint.SPSite($WebURL);
		[Microsoft.SharePoint.SPWeb] $Web = $Site.OpenWeb();
		[Microsoft.SharePoint.SPList] $List = $Web.Lists[$ListDisplayName];
		[Microsoft.SharePoint.SPFolder] $FolderToMoveTo = getArchiveFolder $List;
        $ItemMoveCount=0;
		$Query = New-Object Microsoft.SharePoint.SPQuery;
		#$camlquery = '<FieldRef Name='Column' /><Where>"
		#+ "<Eq><FieldRef Name='LastName' /><Value Type='Text'>Smith</Value></Eq>"
		#+ </Where>';
		#$Query.Query = $camlquery;
		$Query.Folder = $list.RootFolder; 
		$List.GetItems($Query)
        Where {$_.ContentType.Name -ne "Folder"}
		foreach-object { 
        #Line below will simply output to console and demonstrates another .NET call
        [System.String]::format("Moving Item {0} with ID {1}...",$_.Title, $_.ID.ToString());
        $Web.GetFile($_.Url).MoveTo([System.String]::format("{0}/{1}_.000",$FolderToMoveTo.Url,$_.ID.ToString()));
        $_.SystemUpdate($false); 
		write-host "Success..."; 
		$ItemMoveCount++;    };
		write-host "===============================================";
		write-host "Complete!  -> Moved " $ItemMoveCount " Items..."; 
		write-host "===============================================";
		#dispose:
		$Web.Dispose();
		$Site.Dispose();



Viewing all articles
Browse latest Browse all 26374

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>