Phantom_strike Phantom_strike - 4 months ago 17
Vb.net Question

Search multiple file names from the database VB.net

My problem is that I'm trying to get all file names from the data base, but this only works if I write specifically the file names, for example, in my db I have a column named "File" and it's only the name of the file without any extension so I call the variable "file_name" and add the extension but doesn't seem to work...

Here is the table:

Id | Court | File | Email
--------+----------------+-----------+-----------------
1 | COURT NUMBER 1 | DocumentA | John@email.com
2 | COURT NUMBER 1 | DocumentB | Judy@email.com
3 | COURT NUMBER 1 | DocumentC | Edgar@email.com
4 | COURT NUMBER 1 | DocumentD | Brit@email.com


And here is the code:

If (Court.ToUpper.Trim = "COURT NUMER 1") Then
Dim di As DirectoryInfo = New DirectoryInfo("\\192.168.1.70\SisInt\Court\Agreements\J102016SEM" & semester)


For Each fi In di.GetFiles(file_name.Trim + "docx")

If fi.Extension.ToUpper = ".DOC" Or fi.Extension.ToUpper = ".DOCX" Then
FileList.Add(fi.FullName)
NameList.Add((fi.Name.Substring(0, fi.Name.Length - fi.Extension.Length)))

End If
Next
End If


But this only works if I write exactly the name of one file with the extension (doesn't work if I put more than one file name)

For Each fi In di.GetFiles("DocumentA.docx")


So, any idea how to call all the file names from the datebase at once?

Answer

Change your search pattern to this:

di.GetFiles(file_name.Trim + ".doc*")

And you can get the file name without the extension in an easier way:

NameList.Add(Path.GetFileNameWithoutExtension(fi.Name))