Find oldest document in a document set including subfolders

Hi,

I am looking to find the oldest document in a document set sorted by metadata column as Modified date. The oldest document could be directly inside the document set or subfolders.

I do not want to traverse through each file in for each loop which may cost performance problem when I run into thousand of documents. This has to be done through Powershell.

Thanks,

AB


July 21st, 2015 10:00pm

I have managed to write a Powershell but $documentsetfolder.Files is not getting into subfolders in the documentset. Is there a way I can traverse through files in subfolders without using foreach loop due to performance cost.

#Find all documents Set that are more than four years old based on Date Created
# get the current date
Write-Host $documentsetfolder.Item.Title "--DocumentSet " $datecreated "--DateCreated"
$oldest = $documentsetfolder.Files | Sort-Object -Property TimeLastModified -Verbose -Descending | Select-Object -Last 1

***************************************************************************************
#Complete Script from here

#Declare variables
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.DocumentManagement")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

$Url = ''
$Url = $Url.Replace("'", "")
$site = new-object Microsoft.SharePoint.SPSite($Url)
# set minimum age of files and folders
$min_years = "-4"
$StartDate=(GET-DATE)

Write-Host $site -ForegroundColor Blue;

foreach ($web in $site.AllWebs)
{

Write-Host $web -ForegroundColor Cyan;

foreach ($list in $web.Lists)
{
foreach ($contenttype in $list.contenttypes)
{
if($contenttype.name -eq "Legal Matter Document Set"){ # If list has content type as Legal Matter Document Set
$rootFolder = $list.RootFolder
foreach($documentsetfolder in $rootFolder.SubFolders) {
if($documentsetfolder.Properties['vti_progid'] -eq "Sharepoint.DocumentSet")
{
$datecreated = $documentsetfolder.Properties['vti_timecreated']
# determine how far back we go based on current date
$archivedate = $StartDate.AddYears($min_years)
if($datecreated -lt $archivedate)
{
#Find all documents Set that are more than four years old based on Date Created
# get the current date
Write-Host $documentsetfolder.Item.Title "--DocumentSet " $datecreated "--DateCreated"
$oldest = $documentsetfolder.Files | Sort-Object -Property TimeLastModified -Verbose -Descending | Select-Object -Last 1

Write-Host +in : $oldest.Name
$datemodified = $oldest.TimeLastModified
# determine how far back we go based on current date
$archivedate = $StartDate.AddYears($min_years)
if($datemodified -lt $archivedate)
{
#Find all documents Set that are more than four years old based on Date Created
# get the current date
$readytoarchive = "Yes"
write-Host `t $file.Name $datemodified "--DateModified " $readytoarchive "--ReadyToArchive"
}
else
{
$readytoarchive = "No"
}
if($readytoarchive -eq "Yes")
{
Write-Host $documentsetfolder.Properties["vti_title"] " is ready to archive....."
$documentsetfolder.Properties["ReadyToArchive"] = $readytoarchive
$documentsetfolder.Update()
}

}
}
}
}
}

} # end of for-each loop for document library
} # end of for-each loop for all webs

$web.Dispose()
$site.Dispose() 

Free Windows Admin Tool Kit Click here and download it now
July 22nd, 2015 12:00am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics