Home > Software design >  return index of file in folder based on file creation date
return index of file in folder based on file creation date

Time:10-19

I have a project in C# language. which should take the name of a folder and a file. If the file does not exist in the folder, the file not exist will be printed. If there is a file, based on the file creation date, determine how many files have a creation date smaller than this file. My code is as follows:

  1. First, it is checked if the file does not exist in the folder, the file not exist is printed

    DirectoryInfo dir = new DirectoryInfo(@"c:\mydirectory");
    string fileName = "myfile.png";
    FileInfo[] files = dir.GetFiles("*.*");
    
    Boolean fileFound = false;
    
    for(int i=0; i<files.Length; i  )
    {
       if(files[i].Name == fileName)
      {
          fileFound = true;
          break;
      }
    }
    
    if(fileFound==false)
    {
        Console.WriteLine("the file not exist");
        return;
    }
    
  1. The files are then sorted by creation date:

        for (int i = 0; i < files.Length; i  )
        {
            for (int n = i; n < files.Length; n  )
            {
                if (files[n].CreationTime < files[i].CreationTime)
                {
                    var temp = files[i];
                    files[i] = files[n];
                    files[n] = temp;
                }
            }
        }
    

3.Finally, I find the index of the file in the list and it is printed:

        for (int i = 0; i < files.Length; i  )
        {
            if (files[i].Name == fileName)
            {
                Console.WriteLine("index of this file is: "  i);
                break;
            }
        }

My project works fine, but I think there must be an easier way with fewer lines. Can anyone guide me?

CodePudding user response:

You can easily do all these steps using linq:

 //1. if the file does not exist in the folder, `the file not exist` is printed

 if (!files.Any(f => f.Name == fileName))
 {
     Console.WriteLine("the file not exist");
 }
 else
 {
     //2 ,3 : sort the list and find file index

     var indexOfFile = files.OrderBy(item => item.CreationTime)
              .Select((item, i) => new { Item = item.Name, Index = i })
              .First(x => x.Item == fileName).Index;

     Console.WriteLine("index of this file is: {0}", indexOfFile);
 }

CodePudding user response:

There is, using LINQ. You'll need to have a List<FileInfo> instead of an array for this to work.

List<FileInfo> files = dir.GetFiles("*.*").ToList();
string fileName = "filetofind.txt";

First, sort the files by creation time. There's also OrderByDescending if you want to reverse the order.

files = files.OrderBy(o => o.CreationTime).ToList();

Then get the index of the file by its name.

int index = files.FindIndex(o => o.Name == fileName);

If you need to have an array at the end of this process, you can create one from the List.

FileInfo[] fileArray = files.ToArray();
  • Related