0

how to sort the files in the directory ?

i'll have more than 500 no of files in the below format.

prod_orders_XXX_<TimeStamp>.dat 

XXX         =  symbol of the product and the length may varies between 3-6.
<TimeStamp> =  date and time

Multiple files for the same XXX are possible with different time stamps.

Here are some examples:

prod_orders_abc_20122001083000.dat 
prod_orders_abc_20122001083111.dat 
prod_orders_xyz_20122001093157.dat 
prod_orders_xyz_20122001083000.dat 
prod_orders_abc_20122001163139.dat 
prod_orders_abc_20122001093137.dat
Santhosh_ms3
  • 110
  • 2
  • 3
  • 15
  • 1
    Check this link: http://stackoverflow.com/questions/52842/sorting-directory-getfiles – Harsh Jan 23 '12 at 13:59
  • Please show an example FileName. How do you want to sort, filename ascending, creationtime descending? – Tim Schmelter Jan 23 '12 at 14:02
  • prod_orders_abc_201220010830000.dat prod_orders_abc_201220010831000.dat prod_orders_xyz_201220010931670.dat prod_orders_xyz_201220010830000.dat prod_orders_abc_201220011631000.dat prod_orders_abc_201220010931670.dat – Santhosh_ms3 Jan 23 '12 at 14:07
  • Need to sort by creationtime decending.. – Santhosh_ms3 Jan 23 '12 at 14:16
  • With creation-time you don't mean the file-creation time but your timestamp in the filename, do you? See my edited answer – Tim Schmelter Jan 23 '12 at 15:20
  • 1
    The timestamp in the file as showed will order the first of february right after the first of january and before the second of january. Is this what you want? – Wilhelm Jan 23 '12 at 15:44
  • You should tell us how you are converting the timestamp to that string, because it looks odd. `yyyyddmmhhmmssm` So either there is no second or there is only a tenth of a second as millis. Apart from that, `201220010931670` would result in 67 seconds what is not allowed. – Tim Schmelter Jan 23 '12 at 18:53

1 Answers1

1

Please provide the correct sample files and requirement right off the next time ;)

Here is what you need:

Dim fileList = (From file In New IO.DirectoryInfo(directoryPath).GetFiles()
          Where file.Name.IndexOf("prod_orders_") > -1
          Let dateIndex = file.Name.LastIndexOf("_") + 1
          Let dateIndexEnd = file.Name.LastIndexOf(".")
          Let datePart = file.Name.Substring(dateIndex, dateIndexEnd - dateIndex)
          Where datePart.Length = 14 AndAlso ULong.TryParse(datePart, 0)
          Let year = Int32.Parse(datePart.Substring(0, 4))
          Let day = Int32.Parse(datePart.Substring(4, 2))
          Let month = Int32.Parse(datePart.Substring(6, 2))
          Let hour = Int32.Parse(datePart.Substring(8, 2))
          Let minute = Int32.Parse(datePart.Substring(10, 2))
          Let second = Int32.Parse(datePart.Substring(12, 2))
          Let timestamp = New Date(year, month, day, hour, minute, second)
          Order By timestamp Descending
          Select file).ToList()
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939