2012-01-23 99 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:編輯我的回答,下次請提供正確的要求和樣品;) –