Click here to Skip to main content
15,884,537 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi,
In the code below the string array fileNames is populated with files from a directory.
The directory has files for each day. i.e. FileAxxx.csv where xxx refers to YYYYmmdd
Question:
How can this string array be populated only with files for today's date?
Thanks

String[] fileNames;

//populate fileNames string array...

ArrayList fileNamesArray = new ArrayList(fileNames);
Posted
Comments
Herman<T>.Instance 16-Feb-12 3:52am    
When using DirectoryInfo object you can add searchpattern
krumia 16-Feb-12 3:58am    
Or the naive way, you can create the ArrayList object empty, loop through the first array, and match the filename. And if there's a match add the string to the ArrayList.

Try:
C#
var query = from file in fileNames
            where (new FileInfo(file)).LastWriteTime.Date == DateTime.Now.Date
            select file;
fileNames = query.ToArray<string>();



"Hi, Thanks for the reply But I forgot to say that I do not want to use LINQ for project reasons."


Then try:
C#
List<string> todayFiles = new List<string>();
foreach (string file in files)
    {
    if ((new FileInfo(file)).LastWriteTime.Date == DateTime.Now.Date)
        {
        todayFiles.Add(file);
        }
    }
files = todayFiles.ToArray<string>();
 
Share this answer
 
v2
Comments
arkiboys 16-Feb-12 4:04am    
Hi,
Thanks for the reply But I forgot to say that I do not want to use LINQ for project reasons.
OriginalGriff 16-Feb-12 4:23am    
Answer updated
ProEnggSoft 16-Feb-12 4:06am    
My 5!
You may try the following code

C#
string[] fileNames = {"abcdef20120216.csv","ijklmn20120215.csv","pqrst20120216.csv"};
DateTime today = DateTime.Now;
string searchDate = 
	string.Format("{0:0000}{1:00}{2:00}.csv",today.Year,today.Month,today.Day);
ArrayList fileNamesArray = new ArrayList();
foreach(string fileName in fileNames){
	if (fileName.EndsWith(searchDate))
		fileNamesArray.Add(fileName);
}


If your problem is solved then you may accept and vote the solution, otherwise please post your queries

PES
 
Share this answer
 
Comments
arkiboys 16-Feb-12 4:28am    
This seems to be good.
How can I now populate the fileNamesArray with only files with particular names such as FileA, FileW, FileK, etc?
Thanks
ProEnggSoft 16-Feb-12 5:01am    
If your search string follows a pattern, then you can replace the search criterion in the above code as below
if(Regex.IsMatch(fileName,@".*file[AWK].*"+searchDate + "$",RegexOptions.IgnoreCase))
Hope, this will meet your requirement

PES
arkiboys 16-Feb-12 5:21am    
Thanks
ProEnggSoft 16-Feb-12 5:44am    
Thanks:)
If your problem is solved, you can mark the solution as accepted, so that other users can know that the question is answered, and they can view it if required.

PES
Try Something like this:

C#
string[] name = Directory.GetFiles("<path>", "*" + DateTime.Now.ToString("yyyyMMdd") + ".csv");</path>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900