locked
deleting all files but the four newest ones

    Question

  • At this time the code deletes all files but the newest but I want to keep a couple more than the newest yet get rid of the oldest past #4 of the list. How would I do this?

     

     

    string

     

     

    [] files = Directory.GetFiles(apppath, companyName + "VlForeOutLocation" + siteLocationSN + "_" + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString().PadLeft(2, '0') + "**" + "**" + "**" + ".txt", SearchOption.TopDirectoryOnly); //DateTime.Now.Day.ToString().PadLeft(2, '0')

     

     

     

    if (files.Length != 0 && File.Exists(files[files.Length - 1]))

    {

     

     

    foreach (string i in files)

    {

     

     

    File.Delete(i);

    }

     

     

    // //string[] vlFileTemp = File.ReadAllLines(vlFile);

     

     

    // string[] vlFileTemp = File.ReadAllLines(files[files.Length - 1]);

     

     

    //string[] files = System.IO.Directory.GetFiles(@"C:\Folder", "*.xml");

     

     

    //foreach (string file in files)

     

     

    //{

     

     

    // if (System.IO.File.Exists(file))

     

     

    // {

     

     

    // System.IO.File.Delete(file);

     

     

    // }

     

     

    //}

    }

    Friday, June 04, 2010 9:04 PM

Answers

  • Nevermind, I found a simple way to do it and should have thought of it earlier. Thanks for the help. if (files.Length != 0 && File.Exists(files[files.Length - 1])) { int x = 0; foreach (string i in files) { if (x <= files.Length - 2) { File.Delete(i); } x++; } }
    • Edited by tfcsd Monday, June 07, 2010 7:58 PM better code
    • Marked as answer by Harry Zhu Friday, June 11, 2010 6:02 AM
    Monday, June 07, 2010 5:23 PM
  • if (files.Length != 0 && File.Exists(files[files.Length - 1]))
                {
                  int x = 0;
                  foreach (string i in files)
                  {
                    if (x <= files.Length - 4)
                    {
                      File.Delete(i);
                    }
                    x++;
                  }
    This does it.
    • Marked as answer by Harry Zhu Friday, June 11, 2010 6:02 AM
    Monday, June 07, 2010 8:17 PM

All replies

  • Hi,

    in this way you will get an array of found file's creation time. Sort it and remove what you wont.

    DateTime[] =filesDateTime;
    DirectoryInfo dir = new DirectoryInfo("YourDir");
    FileInfo[] pfiles = dir.GetFiles("*.*");
    for(int i=0; i< pfiles.lenght; i++)
     filesDateTime[i]= f.CreationTime;

     

    Regards  

    Saturday, June 05, 2010 4:04 PM
  •     private void BindListView()
        {
          lstBackUp.Clear();
          string strPath = this.GetBackUpPath();
          DirectoryInfo pathInfo = new DirectoryInfo(GetBackUpPath()); //get path
          string[] files = Directory.GetFiles(Convert.ToString(pathInfo.FullName)); //get files 
          int count = pathInfo.GetFiles().Length; //files count
    
          //if count >4 delete files
          if (count > 4)
          {
            int redandunce = count - 4;
            for (int j = 0; j < redandunce; j++)
            {
    
              File.Delete(files[j].ToString());
            }
          }
    
          //reload files
          files = Directory.GetFiles(Convert.ToString(pathInfo.FullName));
          count = pathInfo.GetFiles().Length;
          for (int i = 0; i < count; i++)
          {
            string fullname = Path.GetFileNameWithoutExtension(files[i]); // example: file full name 2010_06_06.xls
            string[] parts = fullname.Split(new char[] { ' ' });
            string name = parts[0] + " " + parts[1].ToString().Replace('_', ':'); //replace '_' to ':'
            listview1.Items.Add(Convert.ToString(i), name, 0);
          }
        }

    If this is helpful
      { Please Mark as Answered }
    else if it is not helpful
      { Un-Mark as Answered }


    Best Regards
    Huan Li, Code Blog: Small is New Big In C#
    Monday, June 07, 2010 8:47 AM
  • Nevermind, I found a simple way to do it and should have thought of it earlier. Thanks for the help. if (files.Length != 0 && File.Exists(files[files.Length - 1])) { int x = 0; foreach (string i in files) { if (x <= files.Length - 2) { File.Delete(i); } x++; } }
    • Edited by tfcsd Monday, June 07, 2010 7:58 PM better code
    • Marked as answer by Harry Zhu Friday, June 11, 2010 6:02 AM
    Monday, June 07, 2010 5:23 PM
  • if (files.Length != 0 && File.Exists(files[files.Length - 1]))
                {
                  int x = 0;
                  foreach (string i in files)
                  {
                    if (x <= files.Length - 4)
                    {
                      File.Delete(i);
                    }
                    x++;
                  }
    This does it.
    • Marked as answer by Harry Zhu Friday, June 11, 2010 6:02 AM
    Monday, June 07, 2010 8:17 PM