Solved

Program failing intermittently

Posted on 2014-11-14
5
95 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
ID: 40442261
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
ID: 40446984
Any advice?
0
 

Accepted Solution

by:
AlHal2 earned 0 total points
ID: 40447526
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
ID: 40460476
It seems to have worked.
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

809 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