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
7
  • 1
    Check this link: stackoverflow.com/questions/52842/sorting-directory-getfiles Commented Jan 23, 2012 at 13:59
  • Please show an example FileName. How do you want to sort, filename ascending, creationtime descending? Commented Jan 23, 2012 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 Commented Jan 23, 2012 at 14:07
  • Need to sort by creationtime decending.. Commented Jan 23, 2012 at 14:16
  • 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? Commented Jan 23, 2012 at 15:44

1 Answer 1

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()
Sign up to request clarification or add additional context in comments.

2 Comments

Hello Tim, i just reedited my post. Sorry for the Inconvenience.
@Santhosh: Edited my answer, please provide the correct requirement and samples next time ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.