Error happens because the FoldersWithDates is actually empty and does not have item index 0.
Your If condition is also wrong. You have "Length of %FoldersWithDates%" > 0 which is always true since it will check if the text "Lenght of % + variable is more than 0.
For example:
That will always be true since "test test" is longer than 0 always.
So remove the 'Length of ' text from the If condition and have it check %FoldersWithDate.Count%
Like so:
Also is there a reason you are using PowerShell script? The CurrentItem should have .LastModified property to check the last modified time.
So you could do like this to delete only the oldest folder in the AllItems folder path:
Also you could already have the "Get subfolders in folder" to sort the folders by "Last modified" time. So then the AllItems will have the folder list from oldest to newest.
Then since that sorts the folder list from oldest to newest then the oldest folder is at the top of AllItems -list. Then you can just delete the first item in that folder list:
Was this reply helpful?YesNo
Under review
Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.