Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi there, I'm pretty new to linq and I've decided to make a program which takes in a file directory, read in a file extension and the age of the target file(s). The main objective is to simply delete files using file extension but to only target older files(1 week old, 3 month old or more).

I've tested the winform which houses the linq statement on older files however it seems to be unable to distinguish the old from the new.(of course I created a back up for those files in case some goes horrible wrong).

Any help would be greatly appreciated and sorry for the presentation of the code below as it has taken on a different form after the copy and paste.

What I have tried:

protected void log()
        {
           
            string path = fileLocation.Text;
            string fullPath = string.Format(@"{0}\File_Deletion_Log.txt", path);
            File.AppendAllLines(fullPath, new[] {"File Deletion Complete    " + string.Format("{0:yyyy-MM-dd}   ",DateTime.Now) + string.Format("Files Deleted: {0}",Count)});
            Count = 0;
        }
        protected void button1_Click(object sender, EventArgs e)
        {   
                string extensions = fileExtension.Text;

                    var files = new DirectoryInfo(fileLocation.Text).GetFiles("*", SearchOption.AllDirectories)
                                     .Select(p => new FileInfo(Convert.ToString(p)))
                                     .Where(p => p.CreationTime < DateTime.Now.Subtract(TimeSpan.FromDays(Convert.ToInt32(fileAge.Text))))
                                     .Where(p => string.Compare(p.Extension, extensions, true) == 0);
                    foreach (var file in files)
                    {
                        file.Attributes = FileAttributes.Normal;
                        File.Delete(file.FullName);
                        Count++;
                    }
                    MessageBox.Show(string.Format("Total Number of files deleted:   {0}", Count));
                    log();
Posted
Updated 7-Mar-19 23:41pm

Without your files to test against, we can't do much at all - so start by "breaking down" the Linq into separate statements:
C#
var files = new DirectoryInfo(fileLocation.Text).GetFiles("*", SearchOption.AllDirectories);
var selected = files.Select(p => new FileInfo(Convert.ToString(p))).ToList();
DateTime limit = DateTime.Now.Subtract(TimeSpan.FromDays(Convert.ToInt32(fileAge.Text)));
var where = selected.Where(p => p.CreationTime < limit).ToList();
var where2 = where.Where(p => string.Compare(p.Extension, extensions, true) == 0).ToList();
Then you can step through looking at each stage in the process to see exactly what is being processed and what results you get.

Hopefully, that will give you some info on where teh problem is.
 
Share this answer
 
Comments
Graeme Cooper 8-Mar-19 8:22am    
Thank you, I looked into it more thoroughly with a friend to find my logic slightly backwards on the limit/fileAge section of the linq statement.
OriginalGriff 8-Mar-19 8:31am    
You're welcome!
It is not clear where your fileAge comes from but you could try TimeSpan.FromDays(90) to narrow the problem down. And skip the delete while debugging. Just print the filenames.
 
Share this answer
 
v2
Comments
Graeme Cooper 8-Mar-19 6:40am    
Sorry about that, fileAge.Text is the user input.
fileAge.Text: How old is your file you wish to delete or another way of saying Delete files that are older than x days.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900