File Sorting using Visual Basic

喜你入骨 提交于 2019-12-14 03:15:37

问题


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

回答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()


来源:https://stackoverflow.com/questions/8972654/file-sorting-using-visual-basic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!