Program failing intermittently

This program runs on a server which received files via sftp.  The files end up in a directory X.
The program search the X directory every few seconds for files.  When it finds files containing certain strings it sends them to another server.  The problem is that it is failing intermittently eg saying it can't access a status file.  I don't believe this is a network issue.  I think it's more likely I'm not releasing some object.  Could someone tell me what is wrong with this program?

Imports system.io
Imports System.Threading

Public Class Form1
    Inherits System.Windows.Forms.Form
    Dim Header As String, StartNextRun As Boolean
    Dim AryTest() As String, Counter As Integer, RDEQCt As Integer
    Dim RtCt, MaxRtCt As Integer
    Dim FilePrefix, Comment As String
    Dim InputFile As String, ArchivedFile As String, WorkingFile As String
    Dim AryFiles() As String
    Dim FilesCt, ChosenFiles As Short
    Dim DtypeCt As Short
    Dim SourceDirectory As String 
    Dim dir As DirectoryInfo 
    Dim files As FileInfo()
    Dim FileStartTime As String, FileEndTime As String
 
    Dim ID As String, Password As String, TunaTable As String

    Dim PasswordFut As String, DestinationDirectory As String, TunaTableFut As String
    Dim PasswordFutCont As String, DestinationDirectoryCont As String, TunaTableFutCont As String
    Dim EmailRecipients As String, AppServer As String, ArchiveDirectory As String
    'Dim DriveLetter As String
    Dim LogFile As String, OldLogFile As String, TimerIntervalFile As String, TimerInterval As Long
    Dim DestinationServer As String, BatchFilePath As String
    Dim AryFileMarkers(100) As String, FileMarkerCt As Integer
    Dim AryEndFileMarkers(100) As String, EndFileMarkerCt As Integer
    Dim FileMarkerName As String, EndFileMarkerName As String
    Dim SupplierName As String, DownloadDirectory As String
 
    'FTP  files to another server

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        FileOpen(1, "Config.txt", OpenMode.Input)
        Header = LineInput(1)
        SourceDirectory = LineInput(1)
        Header = LineInput(1)
        DestinationServer = LineInput(1)
        Header = LineInput(1)
        ID = LineInput(1)
        Header = LineInput(1)
        Password = LineInput(1)
        Header = LineInput(1)
        DestinationDirectory = LineInput(1)
        Header = LineInput(1)
        LogFile = LineInput(1)
        OldLogFile = LogFile.Replace(".txt", "Old.txt")
        Header = LineInput(1)
        TimerIntervalFile = LineInput(1)
        Header = LineInput(1)
        ArchiveDirectory = LineInput(1)
        Header = LineInput(1)
        BatchFilePath = LineInput(1)
        Header = LineInput(1)
        SupplierName = LineInput(1)
        Header = LineInput(1)
        DownloadDirectory = LineInput(1)
        Header = LineInput(1)
        FileMarkerName = LineInput(1)
        Header = LineInput(1)
        EndFileMarkerName = LineInput(1)

        FileClose(1)



        FileMarkerCt = 0
        FileOpen(1, FileMarkerName, OpenMode.Input)
        Input(1, Header)
        Do Until EOF(1)
            Input(1, AryFileMarkers(FileMarkerCt))
            FileMarkerCt = FileMarkerCt + 1
        Loop
        FileClose(1)
        ReDim Preserve AryFileMarkers(FileMarkerCt - 1)

        EndFileMarkerCt = 0
        FileOpen(1, EndFileMarkerName, OpenMode.Input)
        Input(1, Header)
        Do Until EOF(1)
            Input(1, AryEndFileMarkers(EndFileMarkerCt))
            EndFileMarkerCt = EndFileMarkerCt + 1
        Loop
        FileClose(1)
        ReDim Preserve AryEndFileMarkers(EndFileMarkerCt - 1)



        Try
            FileOpen(2, LogFile, OpenMode.Append)
        Catch ex As Exception
            MessageBox.Show(Now & " Error opening status.txt " & ex.Message)
            End
        End Try
        FileOpen(1, TimerIntervalFile, OpenMode.Input)
        Header = LineInput(1)
        Header = LineInput(1)

        TimerInterval = CLng(Header)
        Timer1.Interval = TimerInterval
        FileClose(1)


        Me.Text = "FTP " & SupplierName & " to Coke " & DestinationDirectory
        Me.Show()

        FileClose(2)
        StartNextRun = True
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        'Momentarily close status file to create a copy then 
        'search Check directory for mainframe file
        'Concludes by calling routine to delete old files to stop them clogging up the server

        Try

 
            dir = New DirectoryInfo(SourceDirectory)
            files = dir.GetFiles()
            For Each f As FileInfo In files
                If f.Length = 0 Then
                    f.Delete()
                End If
            Next


            If StartNextRun = True Then
                Timer1.Enabled = False
                dir = New DirectoryInfo(SourceDirectory)
                files = dir.GetFiles()
                If File.Exists(OldLogFile) Then
                    File.Delete(OldLogFile)
                    File.Copy(LogFile, OldLogFile)
                End If
                Try
                    FileOpen(1, LogFile, OpenMode.Append)
                Catch ex As Exception
                    Timer1.Enabled = False
                    MessageBox.Show(Now & " Error opening status.txt " & ex.Message)
                    PrintLine(1, Now & " Error opening status.txt " & ex.Message)
                    End
                End Try
                For Each f As FileInfo In files
                    'Archive supplier data files and send to Coke
                    FileMarkerCt = 0
                    Do Until FileMarkerCt = UBound(AryFileMarkers) Or _
                    InStr(f.FullName, AryFileMarkers(FileMarkerCt), CompareMethod.Text) > 0 
                        FileMarkerCt = FileMarkerCt + 1
                    Loop
                    If InStr(f.FullName, AryFileMarkers(FileMarkerCt), CompareMethod.Text) > 0 Then
                        f.Refresh()
                        If DateDiff(DateInterval.Minute, f.LastWriteTime, Now) > 5 Then 'allow 5 minutes to ensure file has been completely FTPd
                            ArchivedFile = ArchiveDirectory & "\" & f.Name
                            InputFile = Replace(f.FullName, "Check", "Input", , , CompareMethod.Text)
                            If File.Exists(ArchivedFile) Then
                                File.Delete(ArchivedFile)
                            End If

                            File.Copy(InputFile, ArchivedFile)
                            Thread.Sleep(TimerInterval)
                            Call FTPFile(InputFile)
                            Thread.Sleep(TimerInterval)
                            File.Delete(InputFile)
                            Thread.Sleep(TimerInterval)
                        End If
                    End If
                    'Archive confirmation files without sending to Coke
                    EndFileMarkerCt = 0
                    Do Until EndFileMarkerCt = UBound(AryEndFileMarkers) Or InStr(f.FullName, AryEndFileMarkers(EndFileMarkerCt), CompareMethod.Text) > 0
                        EndFileMarkerCt = EndFileMarkerCt + 1
                    Loop
                    If InStr(f.FullName, AryEndFileMarkers(EndFileMarkerCt), CompareMethod.Text) > 0 Then
                        f.Refresh()
                        If DateDiff(DateInterval.Minute, f.LastWriteTime, Now) > 5 Then 'allow 5 minutes to ensure file has been completely FTPd
                            ArchivedFile = ArchiveDirectory & "\" & f.Name
                            InputFile = Replace(f.FullName, "Check", "Input", , , CompareMethod.Text)
                            If File.Exists(ArchivedFile) Then
                                File.Delete(ArchivedFile)
                            End If

                            File.Copy(InputFile, ArchivedFile)
                            
                            Thread.Sleep(TimerInterval)
                            File.Delete(InputFile)
                            Thread.Sleep(TimerInterval)
                        End If
                    End If

                Next


                
                If Minute(Now) = 10 Or Minute(Now) = 40 Then
                    If Second(Now) > 52 Then
                        PrintLine(1, "Cleared all files as at " & Now)
                    End If
                End If

                FileClose(1)

                
                Timer1.Enabled = True
            Else
                'PrintLine(1, "Previous file not cleared as at " & Now)
                'End
            End If
        Catch ex As Exception
            Timer1.Enabled = False
            MessageBox.Show(Now & " Timer Error " & ex.Message)

            PrintLine(1, Now & " Timer Error " & ex.Message)


            End

        End Try
    End Sub
    Sub FTPFile(ByVal InputFile As String)
        Dim ProcessInfo As New ProcessStartInfo(BatchFilePath, InputFile & " " & DestinationDirectory & " " & ID & " " & Password & " " & DownloadDirectory)
        Dim BatchProcess As New Process
        Try


            ProcessInfo.UseShellExecute = False
            BatchProcess.StartInfo = ProcessInfo

            BatchProcess.Start()
            BatchProcess.WaitForExit()

        Catch ex As Exception
            PrintLine(1, InputFile & " Ingest Error " & ex.Message)

            End
        End Try




    End Sub
   
    Private Sub cmdEnd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdEnd.Click

        End
    End Sub
End Class

Open in new window

AlHal2Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Haris DulicCommented:
Friendly, use Code option when posting source code as part of your question, it is more convenient and gives easier copy option...
0
AlHal2Author Commented:
Any advice?
0
AlHal2Author Commented:
I've tried removing the part which deletes the 0kb files.  Perhaps, searching for a file whilst another file is being ftpd to that location causes problems.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AlHal2Author Commented:
It seems to have worked.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.