none
SharePoint script to delete files issue... RRS feed

  • Question

  • Hello everyone,

           I have this script to delete files in SharePoint that are older than a certain date. I am getting an error that I feel like I should know the solution for, but it eludes me. If someone can look at what I have and the error and help me out that would be great.

    Here is the code:

    $site= Get-SPSite URL
    $targetDate = (Get-Date -Year 2014 -Month 3 -Day 24)

    Function DeleteMyFiles($folder)
    {
        foreach($file in $folder.Files)
        {
            if(($targetDate -gt $file.TimeLastModified) -and ($file.web -like "*Administration*") )
            {
                #Write-Host “`t” $file.Name, $file.TimeLastModified, $file.Web

                $file.Delete()
            }
        }....

    And here is the error:

    Collection was modified; enumeration operation may not execute.
    At line:7 char:13
    +     foreach($file in $folder.Files)
    +             ~~~~~
        + CategoryInfo          : OperationStopped: (:) [], InvalidOperationException
        + FullyQualifiedErrorId : System.InvalidOperationException

    So, basically because the collection of files changed the foreach loop won't function. Any ideas would be greatly appreciated. Thanks.

    Tuesday, April 26, 2016 10:53 PM

Answers

  • You are welcome.  I hope I made sense.  Grab a list of the files by name and get them one at a time by name and delete.

    You can also, at times, delete by collection index in reverse.  I believe that the issue is because we delete the first element in a collection that is being enumerated which invalidates the enumerator because the index is not updated in the enum.  Some collections have ways to avoid this. SP developers will likely know the best methods.


    \_(ツ)_/

    • Marked as answer by RedBeardOne Tuesday, April 26, 2016 11:58 PM
    Tuesday, April 26, 2016 11:49 PM

All replies

  • You cannot alter any collection that is being enumerated. Collect the members of the  collection  that you want to delete and retrieve them one at a time by name.

    You should post SP specific questions in the SP forum. They can point you at more detailed in formation.


    \_(ツ)_/

    Tuesday, April 26, 2016 11:41 PM
  • Thanks for your help.
    Tuesday, April 26, 2016 11:44 PM
  • You are welcome.  I hope I made sense.  Grab a list of the files by name and get them one at a time by name and delete.

    You can also, at times, delete by collection index in reverse.  I believe that the issue is because we delete the first element in a collection that is being enumerated which invalidates the enumerator because the index is not updated in the enum.  Some collections have ways to avoid this. SP developers will likely know the best methods.


    \_(ツ)_/

    • Marked as answer by RedBeardOne Tuesday, April 26, 2016 11:58 PM
    Tuesday, April 26, 2016 11:49 PM
  • Here is one take on how to do it without the enum issues.

    function DeleteFiles {
        param($folderUrl)
        $folder = $web.GetFolder($folderUrl)
        foreach ($file in $folder.Files) {
            # Delete file by deleting parent SPListItem
            Write-Host("DELETED FILE: " + $file.name)
            $list.Items.DeleteItemById($file.Item.Id)
        }
    }

    This deletes by ID.  I am not certain it works on  the latest version of SP bu try it.

    I have done it by just getting a static list of IDs and enumerating that list of strings/


    \_(ツ)_/

    Wednesday, April 27, 2016 12:05 AM