I am new to VB.net and I am trying to create a service that will move files from one directory to another where it will be parsed and imported into our EDM system by a third party import engine. Each file needs be processed by the import engine sequentially.
The system that sends the files sends them in batches in no particular order. The import engine doesn't process files in any particular order either. Each file is named like so: nnn-nnnnnnnnnn-XXXX-nnnnnn
df, where n = numbers and X = a document type (DIMP, DIMF, DIPT, DIPP). The problem is DIMPs need to be processed before DIMFs, so normal sorts don't work. I have developed the code to test in a normal windows form application which runs like i had hoped. When the button is clicked everything is parsed and moved correctly.
However, when run as a service the program sometimes moves one, sometimes 2 or 3, and then eventually errors out with a file not found error. Every time the file has already been moved and the program tries to move it again causing the exception to occur. When I debug and press F8 to step through it appears to execute the loops in a random sequence. Which is causing even more confusion
Any help would be greatly appreciated!! I have read through numerous user posts, blogs, news groups, and documentation and no luck.
I am new to vb.net so please forgive any weird coding techniques :)
Public Class CCS_DI_Monitor
Private oTimer As System.Threading.Timer
Protected Overrides Sub OnStart(ByVal args() As String)
Dim oCallback As New TimerCallback(AddressOf OnTimedEvent)
oTimer = New System.Threading.Timer(oCallback, Nothing, 30000, 30000)
Protected Overrides Sub OnStop()
Private Sub OnTimedEvent(ByVal state As Object)
Dim strOrgPath As String = "C:\Temp\CCS_DITest\"
Dim strDestPath As String = "C:\Temp\CCS_DITest\CCS_DI_Import\"
Dim intFileCount As Integer = 0
Dim strFileName As String = ""
Dim strFileVer As String = ""
Dim strDocType As String = ""
Dim strAcctNum As String = ""
Dim strClinID As String = ""
Dim sortFileName As String = ""
Dim i As Integer = 0
Dim arrFileName(i) As String
Dim strDestFile As String = ""
Dim strOrgFile As String
intFileCount = System.IO.Directory.GetFiles(strOrgPath).Length
If intFileCount >= 1 Then
For Each file In System.IO.Directory.GetFiles(strOrgPath)
strFileName = System.IO.Path.GetFileName(file)
strFileVer = Mid(strFileName, 21, 14)
strDocType = Mid(strFileName, 16, 4)
strAcctNum = Mid(strFileName, 5, 10)
strClinID = LSet(strFileName, 3)
If strDocType = "DIMP" Then
strDocType = "DIMC"
sortFileName = strClinID & strFileVer & strAcctNum & strDocType
ReDim Preserve arrFileName(i)
arrFileName(i) = sortFileName
i = i + 1
For i = 0 to Ubound(arrFileName)
strDocType = Mid(arrFileName(i), 28, 4)
strAcctNum = Mid(arrFileName(i), 18, 10)
strFileVer = Mid(arrFileName(i), 4, 14)
strClinID = LSet(arrFileName(i), 3)
If strDocType = "DIMC" Then
strDocType = "DIMP"
strDestFile = strDestPath & strClinID & "-" & strAcctNum & "-" & strDocType & "-" & strFileVer & ".pdf"
strOrgFile = strOrgPath & strClinID & "-" & strAcctNum & "-" & strDocType & "-" & strFileVer & ".pdf"
System.IO.File.Move(strOrgPath & strClinID & "-" & strAcctNum & "-" & strDocType & "-" & strFileVer & ".pdf", strDestPath & strClinID & "-" & strAcctNum & "-" & strDocType & "-" & strFileVer & ".pdf")
Catch ex As Exception