?
Solved

List files in directory in order of date and type

Posted on 2009-12-17
5
Medium Priority
?
659 Views
Last Modified: 2012-05-08
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

0
Comment
Question by:GY1680
5 Comments
 
LVL 29

Expert Comment

by:Kumaraswamy R
ID: 26076966
0
 
LVL 10

Expert Comment

by:anv
ID: 26078110
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
 
LVL 4

Expert Comment

by:maheshsnkr
ID: 26079534
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
 

Author Comment

by:GY1680
ID: 26081724
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
 

Accepted Solution

by:
GY1680 earned 0 total points
ID: 26083063
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Loops Section Overview
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month16 days, 10 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question