Just a tiny post after another break of sorting out the house selling junk on eBay.
Today I saw a post on the VbCity Forums where the ORPO wanted to delete a portion of files from a directory using a search pattern. The answer was to utilise the 'IO.Directory' or 'IO.DirectoryInfo' class's 'GetFiles' method then loop through the result deleting the appropriate 'IO.File' or 'IO.FileInfo' object. However, ORPO's being ORPO's wanted to perform this action in one line.
Anyhoo, after coming to the end of my lunch I decided to trawl the intellisense to see if anything jumped out at me. And it did :-)
Now I have to admit that I thought that Generics (introducing with .NET 2.0) was merely a matter of strongly typing Arraylists - however, introductions to the likes of anonymous methods have left me thinking that it can a bigger and more impactive part of the framework that originally thought (at least from a coding perspective).
So - back to the chase. The 'Array' object has a shared method called 'ForEach' that will loop through the array performing the specified function upon each object (identified using a delegate). So - all I had to do was get the list of files and pass the hard work over to the 'IO.File.Delete' method....
' Deletes all files with the extension of '.doc' under the 'MyDirectory' directory.
Array.ForEach(Of String)(IO.Directory.GetFiles("C:\MyDirectory\", "*.doc"), AddressOf IO.File.Delete)
Cool huh?