Solved

Program failing intermittently

Posted on 2014-11-14
5
92 Views
Last Modified: 2014-11-23
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

0
Comment
Question by:AlHal2
  • 3
5 Comments
 
LVL 15

Expert Comment

by:Haris Djulic
Comment Utility
Friendly, use Code option when posting source code as part of your question, it is more convenient and gives easier copy option...
0
 

Author Comment

by:AlHal2
Comment Utility
Any advice?
0
 

Accepted Solution

by:
AlHal2 earned 0 total points
Comment Utility
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
 

Author Closing Comment

by:AlHal2
Comment Utility
It seems to have worked.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now