Answered by:

$Count not counting! in script to delete files from SharePoint library

Question

I'm trying to delete files from a test library on a dev server with a limit so that a few files remain. The problem is the limit based on the count does not work and all files in the library are deleted. What am I missing?

Answers

Good point, I cut and pasted your code above but restored the actual names for the $web and $docLib variables.

If puzzles are good for your BRAIN then SharePoint will keep it really healthy!

Ramona Maxwell MCPD SharePoint 2010, MCITP SQL Server 2008

It sounds like the loop initializer is re-evaluating the the obejct on every lop. Is it possible the the 'Files' collection is dynamic. If you delete a file does the 'Files.Count' property automatically decrement. If so ther is a
much easier way to do this.

Somehow I do not think this is the best waay to do this. I cannot set up a test sharepoint library at this time but I think we can just delete teh items directly once we have a reference to teh item. The count should then be dynamic so a very
simple loop could be obtained. The same would be try of moving items. Items can also be easily filtered by almost any criteria,

I'm trying to use the $count to set the number of files to leave in the library, so if $count is -gt 84, I want 84 files untouched. If for instance the library started with a hundred files, I want to delete 16.

If puzzles are good for your BRAIN then SharePoint will keep it really healthy!

The following deletes files until there are only 84 files left which is what it looks like teh code is trying to do. There is no control over what gets deleted. Sorting the collection by something like date can control this.

Note that a 'for' loop handles all conditions. If the total number of files is less tham 84 to begin with then the loop is skipped. No amount of logic can do this any better than a well designed 'for' loop initializer.

No preference for this test library, but the next script is to move files to a specific library based on their metadata. New files are routed when uploaded via a custom content type. I have to move files that were in the original library to the new divisional
libraries with the requirement that their tags stay intact.

If puzzles are good for your BRAIN then SharePoint will keep it really healthy!

Good point, I cut and pasted your code above but restored the actual names for the $web and $docLib variables.

If puzzles are good for your BRAIN then SharePoint will keep it really healthy!

Ramona Maxwell MCPD SharePoint 2010, MCITP SQL Server 2008

It sounds like the loop initializer is re-evaluating the the obejct on every lop. Is it possible the the 'Files' collection is dynamic. If you delete a file does the 'Files.Count' property automatically decrement. If so ther is a
much easier way to do this.

Somehow I do not think this is the best waay to do this. I cannot set up a test sharepoint library at this time but I think we can just delete teh items directly once we have a reference to teh item. The count should then be dynamic so a very
simple loop could be obtained. The same would be try of moving items. Items can also be easily filtered by almost any criteria,

Microsoft is conducting an online survey to understand your opinion of the Technet Web site. If you choose to participate, the online survey will be presented to you when you leave the Technet Web site.