使用Visual Basic进行文件排序 [英] File Sorting using Visual Basic
本文介绍了使用Visual Basic进行文件排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何对目录中的文件进行排序?
我将有超过500个以下格式的文件。
prod_orders_XXX_< TimeStamp> .dat
XXX =产品的符号,长度可能在3-6之间变化。
< TimeStamp> =日期和时间
同一个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
解决方案
请在下次提供正确的示例文件和要求;
你需要:
Dim fileList =(来自文件In New IO.DirectoryInfo(directoryPath).GetFiles()
Where file.Name.IndexOf(prod_orders_)> -1
设定dateIndex = file.Name.LastIndexOf(_)+ 1
设定dateIndexEnd = file.Name.LastIndexOf(。)
让datePart = file.Name.Substring(dateIndex,dateIndexEnd - dateIndex)
where datePart.Length = 14 AndAlso ULong.TryParse(datePart,0)
让year = Int32.Parse(datePart.Substring(0,4))
让day = Int32.Parse datePart.Substring(4,2))
让month = Int32.Parse(datePart.Substring(6,2))
让hour = Int32.Parse(datePart.Substring(8,2))
让minute = Int32.Parse(datePart.Substring(10,2))
让second = Int32.Parse(datePart.Substring(12,2))
让timestamp = New Date(year ,月,日,小时,分钟,秒)
按时间戳降序
选择文件).ToList()
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
解决方案
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()
这篇关于使用Visual Basic进行文件排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文