2012-01-23 135 views
0

如何对目录中的文件进行排序?使用Visual Basic进行文件分类

我将有超过500个以下格式的文件。

prod_orders_XXX_<TimeStamp>.dat 

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

对于同一个XXX的多个文件可能具有不同的时间戳。

下面是一些例子:

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

检查此链接: http://stackoverflow.com/questions/52842/sorting-directory-getfiles – Harsh

+0

请显示一个示例FileName。你想如何排序,文件名升序,创建时间降序? –

+0

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 –

回答

1

请提供正确的示例文件,并要求在下一次马上;)

这里是你需要的东西:

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() 
+0

你好蒂姆,我刚刚重新编辑我的帖子。抱歉给你带来不便。 –

+0

@Santhosh:编辑我的回答,下次请提供正确的要求和样品;) –