I'm working on an app for our customer service division. The user will enter a print number, and a mfg date. the app will then do a get files returning results of all files found with that drawing number (multiple revisions). the files are named in this format drawingnumber_rev_pages_6-digit date.pdf
.
Once I have the list of that drawing number, I then take a right(string) of 10 characters to strip the 6-digit date off, and do a Date.ParseExact
to compare to the user input mfg date. I was grabbing anything prior to that date, and showing them in a listbox. the criteria has now changed, and they want me to only show the file that would pertain to that build date. The problem is, I need the first rev prior to that date. and I don't fully understand the (function) portion of my getfiles statement. So I don't know what to search for to even lookup google results. as I search for getfiles, function isn't mentioned, if I look up orderby... function isn't mentioned, is it linq I need to search under? how would you propose I approach this?
original version example
new version, I can filter result and find everything before date ... but what I want is the highest revision before the mfg. date. return a single result.
current version
thank you all- here's my sample code.
Try
Dim results = Directory.GetFiles(fp, f).
OrderByDescending(Function(x) New FileInfo(x).FullName)
For Each result In results
Dim dp As String = ""
Dim d As Date
dp = Strings.Right(result, 10)
dp = Strings.Left(dp, 6)
d = Date.ParseExact(dp, "MMddyy", New Globalization.CultureInfo("en-us"))
Debug.Print(dp)
Debug.Print(d)
Dim udt As String = ""
Dim ud As Date 'user date
udt = Trim(Replace(txtMfgDate.Text, "/", ""))
If udt.ToString.Length = 0 Then lstFound.Items.Add(result)
ud = Date.ParseExact(udt, "MMddyy", New Globalization.CultureInfo("en-us"))
If d.Date <= ud.Date Then
lstFound.Items.Add(result)
End If
Debug.Print(d)
Debug.Print(ud)
If result <> Nothing Then
End If
Next
Catch x As Exception
MessageBox.Show("Error finding file: " & f)
End Try
LINQ can do everything you need with the right conditions. I also tried to clean up some of the code in other ways. I switched the LINQ expression to query comprehension to so I could use Let
.
Try
Dim enusCulture = New Globalization.CultureInfo("en-us") ' consider CultureInfo.CurrentCulture instead?
Dim udt As String = Trim(Replace(txtMfgDate.Text, "/", ""))
Dim ud As Date
If udt.Length > 0 Then ud = Date.ParseExact(udt, "MMddyy", enusCulture) ' user mfg date
' 0071911_a_1_072115.pdf
Dim results = From filename In Directory.GetFiles(fp, f)
Let filedate = Date.ParseExact(filename.Substring(filename.Length - 10, 6), "MMddyy", enusCulture)
Where udt.Length = 0 OrElse filedate.Date < ud.Date
Order By filedate Descending
If udt.Length> 0 Then
results = results.Take(1)
End If
lstFound.Items.AddRange(results)
Catch x As Exception
MessageBox.Show("Error finding file: " & f)
End Try
The Function(x)
syntax is a lambda expression, and it is part of LINQ method syntax for the OrderByDescending
function, if you want to research that.