Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Program failing intermittently

Posted on 2014-11-14
5
Medium Priority
?
101 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

704 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