GY1680
asked on
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.
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);
}
using System.IO;
public class clsCompareFileInfo:ICompar er
{
public int Compare(object x, object y)
{
FileInfo File1;
FileInfo File2 ;
File1 = (FileInfo)x;
File2 = (FileInfo)y;
Compare = DateTime.Compare(File1.Las tWriteTime , 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
public class clsCompareFileInfo:ICompar
{
public int Compare(object x, object y)
{
FileInfo File1;
FileInfo File2 ;
File1 = (FileInfo)x;
File2 = (FileInfo)y;
Compare = DateTime.Compare(File1.Las
}
}
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
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).
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
//............
//...........
//............
//.........
ASKER
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.
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);
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Getting file information:
http://www.codeproject.com/KB/files/file_operations.aspx
Inserting into a data grid:
http://www.dotnetjohn.com/articles.aspx?articleid=104