Results 1 to 7 of 7

Thread: Exit Sub if file in array doesn't exist

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Exit Sub if file in array doesn't exist

    I have been trying to figure out how to exit this sub if the number of files in di.GetFiles is 0 but I have not been able to do so. Can someone please help me with this?
    Code:
            Dim di As New IO.DirectoryInfo(strFPath)
            Dim diar1 As IO.FileInfo() = di.GetFiles()
            Dim dra As IO.FileInfo
            Dim message As New MailMessage
    
            message.From = New MailAddress(StrMailFrom)
            message.To.Add(strMailTo1)
            message.To.Add(strMailTo2)
            message.To.Add(strMailTo3)
            message.Subject = StrSubject
            message.Body = StrBody
    
            For Each dra In diar1
                message.Attachments.Add(New Attachment(dra.FullName))
            Next
    
    
            Dim emailClient As New SmtpClient(strMailServer)
            emailClient.Credentials = (SMTP_Cred)
            emailClient.DeliveryMethod = SmtpDeliveryMethod.Network
            emailClient.EnableSsl = True
            Try
                emailClient.Send(message)
            Catch ex As Exception
                MessageBox.Show(ex.Message & vbNewLine & ex.StackTrace)
            End Try

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Exit Sub if file in array doesn't exist

    directly under the GetFiles line:
    VB Code:
    1. If diar1.Length = 0 Then
    2.     Exit Sub
    3. End If
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Exit Sub if file in array doesn't exist

    Multiple return points from a method are generally a sign of spaghetti logic. As a rule of thumb you should aim to have only a single return point: at the end of the function.

    For example, instead of using:
    Code:
    If diar1.Length = 0 Then
      Exit Sub
    End If
    You should use:
    Code:
    If (diar1.Length > 0) Then
      ' rest of code here...
    End If

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: Exit Sub if file in array doesn't exist

    That seems to have worked, although I won't be a 100% sure until I am able to test it in the production environment tomorrow. I do have one other semi-related question though.

    At some point in these three Dim statements

    Dim di As New IO.DirectoryInfo(strFPath)
    Dim diar1 As IO.FileInfo() = di.GetFiles()
    Dim dra As IO.FileInfo

    I am holding the entire path and file name in memory. So now I am trying to use that full path in this Move from the Try Catch Block so that I can move the files to another directory once the email has been sent.
    Code:
                Try
                    emailClient.Send(message)
                    ' Put file and path name here to move file.
                    My.Computer.FileSystem.MoveFile(something here, "K:\Databases\OrderComplete")
                Catch ex As Exception
                    MessageBox.Show(ex.Message & vbNewLine & ex.StackTrace)
                Finally
                    message.Dispose()
                End Try
    What I am doing wrong?
    Last edited by FastEddie; Apr 23rd, 2007 at 03:47 PM.

  5. #5
    Hyperactive Member ProphetBeal's Avatar
    Join Date
    Aug 2006
    Location
    New York
    Posts
    424

    Re: Exit Sub if file in array doesn't exist

    Quote Originally Posted by FastEddie
    I am holding the entire path and file name in memory. So now I am trying to use that full path in this Move from the Try Catch Block so that I can move the files to another directory once the email has been sent.
    Code:
                Try
                    emailClient.Send(message)
                    ' Put file and path name here to move file.
                    My.Computer.FileSystem.MoveFile(something here, "K:\Databases\OrderComplete")
                Catch ex As Exception
                    MessageBox.Show(ex.Message & vbNewLine & ex.StackTrace)
                Finally
                    message.Dispose()
                End Try
    What I am doing wrong?
    I don't have VS 2005, but I know in 2003 you need to define the full path (including the file name) for your destination.

    So you would change this...
    VB Code:
    1. My.Computer.FileSystem.MoveFile(something here, "K:\Databases\OrderComplete")

    To this...
    VB Code:
    1. My.Computer.FileSystem.MoveFile(something here, "K:\Databases\OrderComplete\SomeFileName.txt")

    Helpful Links:
    VB 6 - How to get the "Key" Value in a collection
    VB.NET - File Search Utility || VB.NET - How to compare 2 directories || VB.NET - How to trust a network share
    VB.NET - Create Excel Spreadsheet From Array || VB.NET - Example Code & Hints you may not know
    VB.NET - Save JPEG with a certain quality (image compression) || VB.NET - DragDrop Files, Emails, and Email Attachments

    Please post some of the code you need help with (it makes it easier to help you)
    If your problem has been solved then please mark the thread [RESOLVED].
    Don't forget to Rate this post

    "Pinky, you give a whole new meaning to the phrase, 'counter-intelligence'."-The Brain-

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: Exit Sub if file in array doesn't exist

    I think the Destination Path is OK. It is the source path where I have the 'something here' text that I need help with.

    I would have thought there would be something like a dra.FullName that I could put in the Source File so that the code like this.
    Code:
    My.Computer.FileSystem.MoveFile(dra.FileName, "K:\Databases\OrderComplete")
    I suspect that if I do end up with a problem on the destination side of the Move File I should be able to use the answer to the first question to make it work.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: Exit Sub if file in array doesn't exist

    I got it.
    Code:
    MoveAllFiles(dra.FullName, "K:\Databases\CompletedOrders\" & dra.Name)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width