c# - Filter files in a directory according to a date in the file name -
i need filter files in directory based on date in file name.
the name has 4 groups of numbers:
9999.99999.201305021219.99999999.txt
when there more 1 file second group of numbers repeated want use newer 1 based on date on third group of numbers (yyyymmddhhmm).
if there these files in directory:
1 1100.04037.201305090945.04542592.txt 2 1100.04041.201305091108.04542707.txt 3 1100.04041.201305091117.60563353.txt 4 1100.04047.201305080942.04541666.txt 5 1100.24084.201305021658.04539125.txt 6 1100.24084.201305061731.04540560.txt
i want in lines 2 , 5 discarded. how managed iterate on directory files:
public static void main(string[] args) { string directory = @"\\some\net\path"; string[] arquivos = directory.getfiles(directory, "1100.*.txt", searchoption.topdirectoryonly); foreach (string arquivo in arquivos) { console.writeline(arquivo.substring(directory.length + 1)); } console.readline(); }
var files = arquivos .select(f => new{orgname = f, parts = new fileinfo(f).name.split('.')}) .groupby(x=>x.parts[1]) .select(g=>g.orderbydescending(x=>x.parts[2]).first().orgname); foreach (string arquivo in files) { ..... }
Comments
Post a Comment