List files in directory in order of date and type

I'm trying to create a list of files that selects the "newest" file of a given type for three different file types.

I'm currently listing all files in the directory without any problems.

I know I can use the FileInfo[] array to sort or order the files - but I'm not sure how to select the top 1 (as in SQL) to display in a table.

Any help would be appreciated.
The code I'm using now to get all files in the directory is as follows:

    DirectoryInfo di = new DirectoryInfo("C:\\Inetpub\\Files\\");
    FileInfo[] rgFiles = di.GetFiles("*.pdf");
                
    TableRow tr = new TableRow();
    TableCell td = new TableCell();
    TableCell td2 = new TableCell();
    TableCell td3 = new TableCell();

    foreach (FileInfo fi in rgFiles)
    {
        string File = "<a href='Files/" + fi.Name + "'>" + fi.Name + "</a>";

        tr = new TableRow();
        td = new TableCell();
        td.BorderStyle = BorderStyle.Solid;
        td.BorderWidth = 1;
        td.Text = File;
        td2 = new TableCell();
        td2.BorderStyle = BorderStyle.Solid;
        td2.BorderWidth = 1;
        td2.Text = fi.CreationTime.ToString();
        td3 = new TableCell();
        td3.BorderStyle = BorderStyle.Solid;
        td3.BorderWidth = 1;
        td3.Text = fi.Length.ToString("#,#") + " bytes";
        tr.Cells.Add(td);
        tr.Cells.Add(td2);
        tr.Cells.Add(td3);
        tblOutputs.Rows.Add(tr);
    }

Open in new window

GY1680Asked:
Who is Participating?
 
GY1680Author Commented:
I ended up using parts of this solution:

http://www.experts-exchange.com/Programming/Languages/.NET/LINQ/Q_23471174.html

Specifically, I used the var collection to use a LINQ query to order the files and then used the .Take() to select the top 1 of each kind.  Code attached.

Thank you all for your help.
                DirectoryInfo diFiles = new DirectoryInfo("C:\\Inetpub\\Files\\");
                FileInfo[] fiFiles = diFiles.GetFiles("*.pdf");
                FileInfo[] fiFiles1 = diFiles.GetFiles("1*.pdf");
                FileInfo[] fifiles2 = diFiles.GetFiles("2*.pdf");
                FileInfo[] fiFiles3 = diFiles.GetFiles("3*.pdf");

                var onefiles = from onefile in onefiles
                                  orderby onefile.CreationTime descending
                                  select onefile;

                var twofiles = from twofile in twofiles
                                  orderby twofile.CreationTime descending
                                  select twofile;

                var threefiles = from threefile in threefiles
                                  orderby threefile.CreationTime descending
                                  select threefile;

                var toponefiless = notefiles.Take(1);
                var toptwofiles = twofiles.Take(1);
                var topthreefiles = threefiles.Take(1);
                
                TableRow tr = new TableRow();
                TableCell td = new TableCell();
                TableCell td2 = new TableCell();
                TableCell td3 = new TableCell();

                foreach (FileInfo fi in toponefiless)
                {
                    string File = "<a href='Files/" + fi.Name + "'>" + fi.Name + "</a>";

                    tr = new TableRow();
                    td = new TableCell();
                    td.BorderStyle = BorderStyle.Solid;
                    td.BorderWidth = 1;
                    td.Text = File;
                    td2 = new TableCell();
                    td2.BorderStyle = BorderStyle.Solid;
                    td2.BorderWidth = 1;
                    td2.Text = fi.CreationTime.ToString();
                    td3 = new TableCell();
                    td3.BorderStyle = BorderStyle.Solid;
                    td3.BorderWidth = 1;
                    td3.Text = fi.Length.ToString("#,#") + " bytes";
                    tr.Cells.Add(td);
                    tr.Cells.Add(td2);
                    tr.Cells.Add(td3);
                    tblOutputs2.Rows.Add(tr);
                }

                foreach (FileInfo fi in toptwofiles)
                {
                    string File = "<a href='Files/" + fi.Name + "'>" + fi.Name + "</a>";

                    tr = new TableRow();
                    td = new TableCell();
                    td.BorderStyle = BorderStyle.Solid;
                    td.BorderWidth = 1;
                    td.Text = File;
                    td2 = new TableCell();
                    td2.BorderStyle = BorderStyle.Solid;
                    td2.BorderWidth = 1;
                    td2.Text = fi.CreationTime.ToString();
                    td3 = new TableCell();
                    td3.BorderStyle = BorderStyle.Solid;
                    td3.BorderWidth = 1;
                    td3.Text = fi.Length.ToString("#,#") + " bytes";
                    tr.Cells.Add(td);
                    tr.Cells.Add(td2);
                    tr.Cells.Add(td3);
                    tblOutputs2.Rows.Add(tr);
                }

                foreach (FileInfo fi in topthreefiles)
                {
                    string File = "<a href='Files/" + fi.Name + "'>" + fi.Name + "</a>";

                    tr = new TableRow();
                    td = new TableCell();
                    td.BorderStyle = BorderStyle.Solid;
                    td.BorderWidth = 1;
                    td.Text = File;
                    td2 = new TableCell();
                    td2.BorderStyle = BorderStyle.Solid;
                    td2.BorderWidth = 1;
                    td2.Text = fi.CreationTime.ToString();
                    td3 = new TableCell();
                    td3.BorderStyle = BorderStyle.Solid;
                    td3.BorderWidth = 1;
                    td3.Text = fi.Length.ToString("#,#") + " bytes";
                    tr.Cells.Add(td);
                    tr.Cells.Add(td2);
                    tr.Cells.Add(td3);
                    tblOutputs2.Rows.Add(tr);
                }

                var files2 = from file in fiFiles
                            orderby file.CreationTime descending
                            select file;

                foreach (FileInfo fi in files2)
                {
                    string File = "<a href='Files/" + fi.Name + "'>" + fi.Name + "</a>";

                    tr = new TableRow();
                    td = new TableCell();
                    td.BorderStyle = BorderStyle.Solid;
                    td.BorderWidth = 1;
                    td.Text = File;
                    td2 = new TableCell();
                    td2.BorderStyle = BorderStyle.Solid;
                    td2.BorderWidth = 1;
                    td2.Text = fi.CreationTime.ToString();
                    td3 = new TableCell();
                    td3.BorderStyle = BorderStyle.Solid;
                    td3.BorderWidth = 1;
                    td3.Text = fi.Length.ToString("#,#") + " bytes";
                    tr.Cells.Add(td);
                    tr.Cells.Add(td2);
                    tr.Cells.Add(td3);
                    tblOutputs.Rows.Add(tr);
                }
            }
            catch (Exception ex)
            {
                TableRow tr = new TableRow();
                TableCell td = new TableCell();
                td.BorderStyle = BorderStyle.Solid;
                td.BorderWidth = 1;
                td.ColumnSpan = 3;
                td.Text = "No files to display";
                tr.Cells.Add(td);
                tblOutputs.Rows.Add(tr);
            }

Open in new window

0
 
Kumaraswamy RCommented:
0
 
anvCommented:
using System.IO;

public class clsCompareFileInfo:IComparer
{
    public int Compare(object x, object y)
{
        FileInfo File1;

        FileInfo File2 ;

 

        File1 = (FileInfo)x;

        File2 = (FileInfo)y;

 

        Compare = DateTime.Compare(File1.LastWriteTime, File2.LastWriteTime)

    }

}
 

Then to use it&

 

DirectoryInfo dirinfo;

FileInfo[] allFiles;

 

dirinfo = new DirectoryInfo(sSelPath);

allFiles = dirinfo.GetFiles("*.xml");

Array.Sort(allFiles, new clsCompareFileInfo);

foreach FileInfo  fl in allFiles

//your code.
Next
0
 
maheshsnkrCommented:
hi  
GY1680,

Try the code within the code snippet.
I have included filter with different file extension and the output will be those files (whose extensions are filtered) with the Creation Time ( latest modified).
DirectoryInfo di = new DirectoryInfo(Server.MapPath("~/FileUploads/UploadedFiles/"));
        string[] AllowedFiles = { "jpg", "xlsx" };
        
        FileInfo[] rgFiles = null;
        ArrayList files = new ArrayList();
        ArrayList newfiles = new ArrayList();

        foreach (string extension in AllowedFiles)
        {
            rgFiles = di.GetFiles("*." + extension);
            FileInfo lastUpdatedFile = null;
            foreach (var fileName in rgFiles)
            {
                FileInfo currentFile = new FileInfo(fileName.Name);
                if (lastUpdatedFile == null || currentFile.CreationTime > lastUpdatedFile.CreationTime)
                {
                    lastUpdatedFile = currentFile;
                }
            }
            files.AddRange(new FileInfo[] { (FileInfo)lastUpdatedFile });
        }
        rgFiles = (FileInfo[])files.ToArray(typeof(FileInfo));

        TableRow tr = new TableRow();
        TableCell td = new TableCell();
        TableCell td2 = new TableCell();
        TableCell td3 = new TableCell();

        //The rest, you can use your logic and show what ever details you want
        //............
        //...........
        //............
        //.........

Open in new window

0
 
GY1680Author Commented:
maheshsnkr;

Tried a variation of your code, but it excepts after the file.createddate because it has the date at 12/31/1600 for some reason.  I'm not sure where it's being thrown off, but with my old code the file date was picked up ok.  It goes until "td3.Text = fi.Length.ToString("#,#") + " bytes";" and then says it can't find the file specified.

anv;

I'd like to try your suggestion as well, but I'm assuming for "x" and "y" you're suggesting I assign a specific file to each of those variables and I'm a bit unclear how to do that with each and every file in the directory.  Please advise if you have thoughts.

rkworlds;

I've looked at both of your articles, and I understand the nature of a dataadapter, but I'm unclear as to how I would use that to list the files in a datagrid without interacting with a database.  i do not have the filenames stored in a database, nor do i wish to have them.  I'm simply looking to find all files in the directory and then list them in order of date created (preferably in descending order - but I"m sure I can figure that part out given help with the rest).

Thank you all.

    DirectoryInfo di = new DirectoryInfo("C:\\Inetpub\\Files\\");
    FileInfo[] rgFiles = di.GetFiles("*.pdf");

    ArrayList files = new ArrayList();
    ArrayList newfiles = new ArrayList();

    FileInfo lastUpdatedFile = null;
    foreach (var fileName in rgFiles)
    {
	FileInfo currentFile = new FileInfo(fileName.Name);
	if (lastUpdatedFile == null || currentFile.CreationTime > lastUpdatedFile.CreationTime)
	{
	    lastUpdatedFile = currentFile;
	}
    }
    files.AddRange(new FileInfo[] { (FileInfo)lastUpdatedFile });
    FileInfo[] rgFiles2 = (FileInfo[])files.ToArray(typeof(FileInfo));

    try
    {

	TableRow tr = new TableRow();
	TableCell td = new TableCell();
	TableCell td2 = new TableCell();
	TableCell td3 = new TableCell();

	foreach (FileInfo fi in rgFiles2)
	{
	    string File = "<a href='Files/" + fi.Name + "'>" + fi.Name + "</a>";

	    tr = new TableRow();
	    td = new TableCell();
	    td.BorderStyle = BorderStyle.Solid;
	    td.BorderWidth = 1;
	    td.Text = File;
	    td2 = new TableCell();
	    td2.BorderStyle = BorderStyle.Solid;
	    td2.BorderWidth = 1;
	    td2.Text = fi.CreationTime.ToString();
	    td3 = new TableCell();
	    td3.BorderStyle = BorderStyle.Solid;
	    td3.BorderWidth = 1;
	    td3.Text = fi.Length.ToString("#,#") + " bytes";
	    tr.Cells.Add(td);
	    tr.Cells.Add(td2);
	    tr.Cells.Add(td3);
	    tblOutputs.Rows.Add(tr);
	}
    }
    catch (Exception ex)
    {
	TableRow tr = new TableRow();
	TableCell td = new TableCell();
	td.BorderStyle = BorderStyle.Solid;
	td.BorderWidth = 1;
	td.ColumnSpan = 3;
	td.Text = "No files to display";
	tr.Cells.Add(td);
	tblOutputs.Rows.Add(tr);
    }

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.