Creating a function using Dictionary and Datetime help??
-
I am first getting a list of strings from files that have dates in them ex. fileabc_20100612_1020.txt in a certain directory(expression S.ImportFolder) my objective is to get a list of these dates in from a folder. My problem is that there will be some files with same dates in this case I want to select the newest one for that date like closest to 23:59 of that Date. for example in c:\Temp there is fileabc_20100612_1020.txt fileabc_20100613_1020.txt fileabc_20100614_1020.txt fileabc_20100614_2320.txt I would want to only import fileabc_20100612_1020.txt fileabc_20100613_1020.txt fileabc_20100614_2320.txt or exclude fileabc_20100614_1020.txt
string\[\] fileEntries = Directory.GetFiles(S.ImportFolder); foreach (string fileNameWithRoot in fileEntries) { string fileName = System.IO.Path.GetFileName(fileNameWithRoot);
....String manipulation.....
DateTime dtFileName = new DateTime();
try
{
DateTime dtFileName = Convert.ToDateTime(fileName);
}
catch (Exception e)
{
}dicFiles.Add(dtFileName, fileName);
-
I am first getting a list of strings from files that have dates in them ex. fileabc_20100612_1020.txt in a certain directory(expression S.ImportFolder) my objective is to get a list of these dates in from a folder. My problem is that there will be some files with same dates in this case I want to select the newest one for that date like closest to 23:59 of that Date. for example in c:\Temp there is fileabc_20100612_1020.txt fileabc_20100613_1020.txt fileabc_20100614_1020.txt fileabc_20100614_2320.txt I would want to only import fileabc_20100612_1020.txt fileabc_20100613_1020.txt fileabc_20100614_2320.txt or exclude fileabc_20100614_1020.txt
string\[\] fileEntries = Directory.GetFiles(S.ImportFolder); foreach (string fileNameWithRoot in fileEntries) { string fileName = System.IO.Path.GetFileName(fileNameWithRoot);
....String manipulation.....
DateTime dtFileName = new DateTime();
try
{
DateTime dtFileName = Convert.ToDateTime(fileName);
}
catch (Exception e)
{
}dicFiles.Add(dtFileName, fileName);
I don't see a problem, except for choosing one of many possible ways to get there. Here are some ideas: 1. your filename convention is such that sorting them alphabetically also sorts them chronologically, that makes things easy. Start by sorting them alphabetically; if you think they already are, that is only true sometimes, it depends on the file system (NTFS versus FAT). 2. you can convert a datetime string to a DateTime using
DateTime.ParseExact()
orDateTime.TryParseExact()
. Works much better than anyConvert
method. But you don't have to. See below. 3. when scanning the files chronologically, you're only interested in the last one of each date, so compare the date (or the first 16 characters, or all but the last 8 characters) of two consecutive names, when equal do nothing, when different, keep the oldest one. Make sure to also keep the very last file. :)Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
-
I don't see a problem, except for choosing one of many possible ways to get there. Here are some ideas: 1. your filename convention is such that sorting them alphabetically also sorts them chronologically, that makes things easy. Start by sorting them alphabetically; if you think they already are, that is only true sometimes, it depends on the file system (NTFS versus FAT). 2. you can convert a datetime string to a DateTime using
DateTime.ParseExact()
orDateTime.TryParseExact()
. Works much better than anyConvert
method. But you don't have to. See below. 3. when scanning the files chronologically, you're only interested in the last one of each date, so compare the date (or the first 16 characters, or all but the last 8 characters) of two consecutive names, when equal do nothing, when different, keep the oldest one. Make sure to also keep the very last file. :)Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
I am importing another system's files therefor I do cannot change the file naming convention so Im thinking of parsing the file into: datetime, full name How can I compare the each record against each other with the first 8 digits for example 20100719 and if it finds another string node with 20100719 to then look at the substring/node which would be the next 6 digits after the date. then delete the record that is not the most current for that date. Thanks in advance
-
I am importing another system's files therefor I do cannot change the file naming convention so Im thinking of parsing the file into: datetime, full name How can I compare the each record against each other with the first 8 digits for example 20100719 and if it finds another string node with 20100719 to then look at the substring/node which would be the next 6 digits after the date. then delete the record that is not the most current for that date. Thanks in advance
o0romeo0o wrote:
I do cannot change the file naming convention
I never suggested you change it; I said to take advantage of it. I suggest you reread by earlier reply. it is all in there.
o0romeo0o wrote:
How can I compare the each record against each other
There is no need to do that; just sort the array as I said before. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
-
I am importing another system's files therefor I do cannot change the file naming convention so Im thinking of parsing the file into: datetime, full name How can I compare the each record against each other with the first 8 digits for example 20100719 and if it finds another string node with 20100719 to then look at the substring/node which would be the next 6 digits after the date. then delete the record that is not the most current for that date. Thanks in advance
If the filename will always be in the format
name_yyyyMMdd_HHmm.extension
then you could use something like this (needs some [alot of] optimization but works). You can now implement a custom comparer to compare by name and if eqaul, compare by date.
// string wrapper for filename format 'name_yyyyMMdd_HHmm.ext'
public class MyFileData
{
private string fullname;public MyFileData(string fullname) { this.fullname = fullname; } public string Fullname { get { return fullname; } } public string Name { get { return fullname.Substring(0, fullname.IndexOf('\_')); } } public DateTime DateTime { get { return DateTime.ParseExact(fullname.Substring(fullname.IndexOf('\_') + 1, 13), "yyyyMMdd\_HHmm", CultureInfo.InvariantCulture); } } public string Extension { get { return fullname.Substring(fullname.IndexOf(".") + 1); } }
}
Dave
If this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum.(Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
If the filename will always be in the format
name_yyyyMMdd_HHmm.extension
then you could use something like this (needs some [alot of] optimization but works). You can now implement a custom comparer to compare by name and if eqaul, compare by date.
// string wrapper for filename format 'name_yyyyMMdd_HHmm.ext'
public class MyFileData
{
private string fullname;public MyFileData(string fullname) { this.fullname = fullname; } public string Fullname { get { return fullname; } } public string Name { get { return fullname.Substring(0, fullname.IndexOf('\_')); } } public DateTime DateTime { get { return DateTime.ParseExact(fullname.Substring(fullname.IndexOf('\_') + 1, 13), "yyyyMMdd\_HHmm", CultureInfo.InvariantCulture); } } public string Extension { get { return fullname.Substring(fullname.IndexOf(".") + 1); } }
}
Dave
If this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum.(Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)So I created a dictionary DICTIONARY1
dicFilesDT.Add(dateParts[1], (dateParts[1] + dateParts[2] ));
so that I would have just the: KEY as the Date and VALUE as the Date+Time I need help in making another dictionary (DICTIONARY2) that would 1. If there are no Duplicates from checking DICTIONARY2 using foreach keyvaluepair in DICTIONARY1 add Key Pair to DICTIONARY2 2. Check for duplicates of the Key (Date) (thinking ex. dictionary.ContainsKey) if the dictionary has the KEY then Compare the KEY VALUE from DICTIONARY 1 to DICTIONARY 2 to see which one is larger ex. 230132 compared to 100425 if DICTIONARY1 KEY VALUE is larger Delete KeyValuePair from DICTIONARY 2 and add DICTIONARY 1 KEY VALUE else do nothing and continue the loop Thank you folks for helping me wiht this.
-
o0romeo0o wrote:
I do cannot change the file naming convention
I never suggested you change it; I said to take advantage of it. I suggest you reread by earlier reply. it is all in there.
o0romeo0o wrote:
How can I compare the each record against each other
There is no need to do that; just sort the array as I said before. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
-
I am first getting a list of strings from files that have dates in them ex. fileabc_20100612_1020.txt in a certain directory(expression S.ImportFolder) my objective is to get a list of these dates in from a folder. My problem is that there will be some files with same dates in this case I want to select the newest one for that date like closest to 23:59 of that Date. for example in c:\Temp there is fileabc_20100612_1020.txt fileabc_20100613_1020.txt fileabc_20100614_1020.txt fileabc_20100614_2320.txt I would want to only import fileabc_20100612_1020.txt fileabc_20100613_1020.txt fileabc_20100614_2320.txt or exclude fileabc_20100614_1020.txt
string\[\] fileEntries = Directory.GetFiles(S.ImportFolder); foreach (string fileNameWithRoot in fileEntries) { string fileName = System.IO.Path.GetFileName(fileNameWithRoot);
....String manipulation.....
DateTime dtFileName = new DateTime();
try
{
DateTime dtFileName = Convert.ToDateTime(fileName);
}
catch (Exception e)
{
}dicFiles.Add(dtFileName, fileName);
You're making it all way too complex. Here is what I would do (untested!):
string[] files = Directory.GetFiles(S.ImportFolder);
Array.Sort(files);
int count=files.Length;
for(int i=1; i<=count; i++) {
string curr=files[i-1];
string next="_the_end_";
if (i<count) next=files[i];
if (strcmp(curr.Substring(0,curr.LastIndexOf('_')), next.Substring(0,next.LastIndexOf('_'))!=0) {
// process or store curr as it is the last one of this date
}
}Some nasty trick was used to make sure the last file is also included. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
-
Thank you for your reply I don't need the for example the last 4 latest records in the array... as I can have several days and the oldest date has 3 times it was altered creating 3 different files for that day... I would need the newest one.
I did read and understand your original post. I'm not so sure you did read and understand any of my replies so far. Anyway I have added yet another reply with the full code, albeit untested. For me the subject is closed. :|
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
-
You're making it all way too complex. Here is what I would do (untested!):
string[] files = Directory.GetFiles(S.ImportFolder);
Array.Sort(files);
int count=files.Length;
for(int i=1; i<=count; i++) {
string curr=files[i-1];
string next="_the_end_";
if (i<count) next=files[i];
if (strcmp(curr.Substring(0,curr.LastIndexOf('_')), next.Substring(0,next.LastIndexOf('_'))!=0) {
// process or store curr as it is the last one of this date
}
}Some nasty trick was used to make sure the last file is also included. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
Hi Luc, I tried the above code, just trying to debug the last line
if (strcmp(curr.Substring(0,curr.LastIndexOf('_')), next.Substring(0,next.LastIndexOf('_'))!=0) {
strcmp is a c++ concept, is that similiar to StringComparer? also my ide complains !=0 that it cannot be applied to an int and a string. Tried playing around with the brackets and to no function. let me know what I can do. -
Hi Luc, I tried the above code, just trying to debug the last line
if (strcmp(curr.Substring(0,curr.LastIndexOf('_')), next.Substring(0,next.LastIndexOf('_'))!=0) {
strcmp is a c++ concept, is that similiar to StringComparer? also my ide complains !=0 that it cannot be applied to an int and a string. Tried playing around with the brackets and to no function. let me know what I can do.Sorry, my mistake, should not be strcmp, should be
string.Compare
:)Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.