Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have to get a list of 2 different file types from a folder and display it in the body of an email in table format. My code is:

VB
Dim dat_files As String() = Directory.GetFiles("C:\~", "*.dat")
            Dim ctl_files As String() = Directory.GetFiles("C:\~", "*.ctl")

            For Each ctlfiles In ctl_files
                For Each datfiles In dat_files
                    Email(datfiles, ctlfiles)
                Next

            Next


And the code for email is:

VB
Public Shared Sub Email(ByVal datfiles, ByVal ctlfiles)
'some code
 Dim str As New StringBuilder

            Dim index As Integer = 0

            While index < datfiles.Count Or index < ctlfiles.Count
                str.Append("<tr>")
                str.Append("<td>" & datfiles.ElementAtOrDefault(index) & "</td>")
                str.Append("<td>" & ctlfiles.ElementAtOrDefault(index) & "</td>")
                str.Append("</tr>")

                index += 1
            End While
            objMail.Body = sb.ToString


This is not working. The problem is am not getting all the dat & ctl files in the mail thus cannot display them in two different columns.Can anyone help.
Posted

It's the 'Or'.

VB
While index < datfiles.Count Or index < ctlfiles.Count


This While statement will exit when you have finished evaluating the contents of either one of those two objects. It stands to reason that there may be unevaluated items in the other object after the While exits.
 
Share this answer
 
Comments
raghav69 3-May-13 3:00am    
Actually i have got the solution now. Also the while statement displays all the files in the folder so its working fine as well. But only that i get the name of the file along with where its located, such as 'C\~\~\Something.dat' & C:\~\~\Something.ctl'. How do i display the names of the files only. And one more thing, when i display the table, there's some gap between the last row & the other rows. Why so?
pdoxtader 3-May-13 8:14am    
Ok,
Here's a function that I use to get the filename from it's complete path.


Public Function GetFilenameFromPath(ByVal filePath As String, Optional ByVal errMsg As String = "") As String

Dim filePathParts() As String

If filePath.Trim = "" Then
errMsg = "Error: No file path provided."
Return ""
End If

Try
filePathParts = Split(filePath, "\")
Return filePathParts(filePathParts.Length - 1)
Catch ex As Exception
errMsg = ex.Message
Return ""
End Try

End Function


About the odd spacing in your table: You can use <table border="0" cellpadding="0" cellspacing="0"> if your still getting odd spacing in your table, then there's something wrong with the html you are generating, or the elements in your cell have padding or margins that need to be set to 0px with css.

Also, if this is only happening at the last row, then maybe you're creating one last table row with empty cells. This would be my guess.

Oh - and that Or will seem to work correctly if you always have an equal number of files in both objects. But one day you won't... and there will probably be some odd results. You should really make that bit of code more robust.

Hope this helps,
- Pete
My solution:

VB
Module Module1

  Sub Main()

    Dim dat_files As String() = Directory.GetFiles("C:\test\", "*.dat")
    Dim ctl_files As String() = Directory.GetFiles("C:\test\", "*.ctl")
    Email(dat_files, ctl_files)

  End Sub


  Public Sub Email(ByVal datfiles As String(), ByVal ctlfiles As String())

    Dim sb As New System.Text.StringBuilder

    Dim index As Integer = 0

    While index < datfiles.Length Or index < ctlfiles.Length
        sb.Append("<tr>")
        sb.Append("<td>" & Path.GetFileName(datfiles(index)) & "</td>")
        sb.Append("<td>" & Path.GetFileName(ctlfiles(index)) & "</td>")
        sb.Append("</tr>")

        index += 1
    End While
    objMail.Body = sb.ToString
      End Sub
End Module
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900