Answered file limit in folder

ExEdzy

Active member
Joined
Nov 25, 2010
Messages
37
Programming Experience
3-5
Hey guys. :)
So i make a program that saves text files in one folder, but what i need is to make the file limit in folder..

For example, i have limit 200 files, and when i add a new file, how do i delete the oldest one, or if the limit is changed to 100, how do i delete all the old ones, to make the limit 100?

I hope you understand what my problem is..
Hope someone can help me..
 
Last edited:
Basically use DirectoryInfo and GetFiles to get FileInfo objects.
VB.NET:
Dim dir As New IO.DirectoryInfo("d:\path")
Dim files = dir.GetFiles()
then Linq is the simplest option to query the selections
VB.NET:
Dim keep = From x In files Order By x.LastWriteTime Descending Take 100
Dim old = files.Except(keep)
Then I'd perhaps do
VB.NET:
old.ToList.ForEach(Sub(x) x.Delete())
but you have to do
VB.NET:
For Each file In old
    file.Delete()
Next
 
Back
Top