What is the rule for the ordering?
According to the split, use Split("_"c)
to get an array:
Dim tokens = "TOM_here_was".Split("_"c)
Now you have all parts, if you want a random order for example (since it's not clear):
tokens = tokens.OrderBy(Function(s) Guid.NewGuid()).ToArray()
Update acc. your comment:
I have a file name with a client number then the number that follows is a starting date and the last number is an end date. eg 1111_20140201_20140228. tom was here probably not a good example
Dim path = "C:\Temp\1111_20140201_20140228.txt"
Dim fileName = System.IO.Path.GetFileNameWithoutExtension(path)
Dim tokens = fileName.Split("_"c)
If tokens.Length = 3 Then
Dim client = tokens(0)
Dim startDate, endDate As Date
Dim parsableStart = Date.TryParseExact(tokens(1),
"yyyyMMdd",
Globalization.CultureInfo.InvariantCulture,
Globalization.DateTimeStyles.None,
startDate)
Dim parsableEnd = Date.TryParseExact(tokens(2),
"yyyyMMdd",
Globalization.CultureInfo.InvariantCulture,
Globalization.DateTimeStyles.None,
endDate)
If parsableStart AndAlso parsableEnd Then
Console.WriteLine("Client: {0} Start: {1} End: {2}", client, startDate, endDate)
End If
End If
If you want to order the files in a directory you could use LINQ:
Dim startDate, endDate As Date
Dim fileNames = System.IO.Directory.EnumerateFiles("C:\Temp\", "*.*", SearchOption.TopDirectoryOnly)
Dim orderedFilenames =
From path In fileNames
Let fileName = System.IO.Path.GetFileNameWithoutExtension(path)
Let tokens = fileName.Split("_"c)
Where tokens.Length = 3
Let client = tokens(0)
Let startDateParsable = Date.TryParseExact(tokens(1), "yyyyMMdd", Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.None, startDate)
Let endDateparsable = Date.TryParseExact(tokens(2), "yyyyMMdd", Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.None, endDate)
Where startDateParsable AndAlso endDateparsable
Order By startDate, endDate
Select New With { fileName, client, startDate, endDate }
For Each fn In orderedFilenames
Console.WriteLine("File: {0} Client: {1} Start: {2} End: {3}", fn.fileName, fn.client, fn.startDate, fn.endDate)
Next