Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have a Sharepoint 2010 Document Library with a range of subfolders and some files within each of them. The problem is, right now every subfolder has the same Modified timestamp, and I want each folder's Modified timestamp to be set to the latest Modified timestamp of the files inside.

My knowledge about the Object Model is pretty limited so far, and I'm having trouble writing a PowerShell script for this.

Here's my code so far:

$web = Get-SPWeb -Identity http://inside.site.be/Tools
$list = $web.GetList("http://inside.site.be/Tools/site_candidate/")
$folderquery =  New-Object Microsoft.SharePoint.SPQuery 
$foldercamlQuery = 
'<Where>
    <Eq>
        <FieldRef Name="ContentType" />
            <Value Type="text">Folder</Value>
    </Eq>
</Where>'
$folderquery.Query = $foldercamlQuery

$folders = $list.GetItems($folderquery)
foreach($folder in $folders)
{
    if($folder.ItemCount -gt 0){
        $oldest = null
        $filequery = New-Object Microsoft.SharePoint.SPQuery
        $filequery.Folder = $folder
        $files = SP.List.getItems($itemquery)
        foreach ($file in $files){
            if($file["Modified"] -ilt $oldest){$oldest = $file["Modified"]}
        }
        $folder["Modified"] = $oldest
        $folder["Created"] = $oldest
        $folder.Update()
        Write-Host "$folder now has date $oldest"
    } else {
        Write-Warning "$folder['Name'] is empty"
    }
}

I get the feeling I'm using the wrong types, but I've tried typecasting (for example, something like

foreach(Folder $folder in $folders){...}

But that doesn't seem to work. I've found pretty meager results about working with folders and files within in document libraries when searching for answers, maybe you guys can point me in the right direction?

Learning resources are also welcome.

share|improve this question

2 Answers 2

up vote 2 down vote accepted

Try this:

$web = Get-SPWeb -Identity http://inside.site.be/Tools 
$list = $web.GetList("http://inside.site.be/Tools/site_candidate/")
$folderquery =  New-Object Microsoft.SharePoint.SPQuery  
$foldercamlQuery =  
'<Where> 
    <Eq> 
        <FieldRef Name="ContentType" /> 
        <Value Type="text">Folder</Value> 
    </Eq> 
</Where>' 
$folderquery.Query = $foldercamlQuery 

$folders = $list.GetItems($folderquery) 
foreach($folderItem in $folders) 
{ 
    $folder = $folderItem.Folder
    if($folder.ItemCount -gt 0){ 
        $oldest = $null
        $files = $folder.Files
        foreach ($file in $files){ 
            if($file.Item["Modified"] -gt $oldest){$oldest = $file.Item["Modified"]} 
        } 
        $folderItem["Modified"] = $oldest 
        $folderItem["Created"] = $oldest 
        $folderItem.Update() 
        Write-Host "$folder now has date $oldest" 
    } else { 
        Write-Warning "$folder['Name'] is empty" 
    } 
} 
share|improve this answer
    
Thank you, this is a great solution! However, it does not seem to work with special file- or folder names (those with periods or dashes in them). Any idea how to include those as well? –  Michael Davids May 31 '12 at 9:07

Have you considererd splitting your content into more document libraries and organizing the documents with metadata instead of using folders? You can at the list level query the SPList.LastItemModifiedDate property to get the modified date and time of the item that was last modified in the list. This piece of information is just not available at the folder level.

Looping through the folders and updating their timestamps is a pretty heavy and fragile solution. Moreover, it will also not be always give you true data as there is a lag from an item is updated until the change is synchronized to the folder.

share|improve this answer
    
I'm afraid this is such a heavily used portion of our intranet (folders and files are added/removed on a continuous basis) that it looks rather impractical to be creating/deleting document libraries the entire time. It could be only a few files in each folder anyway. I realize this is a very heavy solution to roll out but it's a one-time affair just so we could get our timestamps straight after a big move we had recently. My code is by far elegant and not a best practice by a long shot, but that's not what I'm aiming for here. Thanks for the (very!) interesting advice though, I'm learning! :) –  Michael Davids May 30 '12 at 14:21
    
Thanks - I see it might not be feasible to change the existing structure here. If it is a one-time affair and you do not need to resync all the time then your apporach sounds ok to me. But out of curiosity - what is the business case for having this kind of information on the folder level? –  Lars Fastrup May 30 '12 at 18:07
    
To be quite honest that's not 100% clear to me either. It's interesting for the team using that Document Library to be able to see the folders with the most recent activity, yet since all documents are about the same subject I would think something like a Document Set would be appropriate here. I can only assume that either an application that's plugged into the Document Library doesn't play well with Document Sets, or that there's a significant drawback to Document Sets for the way the team uses this list. –  Michael Davids May 31 '12 at 9:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.