My code posted below. The routine gets a list of files from a set of directories, loops though and connect to an ftp server moving the documents after each transfer. However, if the connection drops (for example) the code still moves all the files as though the code had completed successfully. Any advise greatfully received. Theworrying part is if I set my after process decision to delete then potentially on an exception documents might be deleted even if not processed correctly.
Code:Me.Timer1.Enabled = False Try 'Set the ftp variables from the application configuration file Dim strftpSourceDirectory As String strftpSourceDirectory = System.Configuration.ConfigurationManager.AppSettings("ftpSourceDirectory") Dim strftpUserName As String strftpUserName = System.Configuration.ConfigurationManager.AppSettings("ftpUserName") Dim strftpPassword As String strftpPassword = System.Configuration.ConfigurationManager.AppSettings("ftpPassword") Dim strftpTargetDirectory As String strftpTargetDirectory = System.Configuration.ConfigurationManager.AppSettings("ftpTargetDirectory") Dim strAfterProcessDecision As String strAfterProcessDecision = System.Configuration.ConfigurationManager.AppSettings("AfterUploadDecision") Dim strProcessedDirectory As String strProcessedDirectory = System.Configuration.ConfigurationManager.AppSettings("ProcessedDirectory") Dim intProcessingStartTime As Integer intProcessingStartTime = System.Configuration.ConfigurationManager.AppSettings("ProcessingStartTime") Dim intProcessingEndTime As Integer intProcessingEndTime = System.Configuration.ConfigurationManager.AppSettings("ProcessingEndTime") Dim intTime As Integer intTime = TimeOfDay.Hour 'Check if processing time is valid If intTime >= intProcessingStartTime And intTime <= intProcessingEndTime Then ' Inumerate list of documents contained within and below the source location Dim dir As New IO.DirectoryInfo(strftpSourceDirectory) Dim files() As IO.FileInfo = dir.GetFiles("*.*", IO.SearchOption.AllDirectories) Dim searchResults As String() = Directory.GetFiles(strftpSourceDirectory, "*.*", SearchOption.AllDirectories) Dim dt As New DataTable dt.Columns.Add("RootDrive", GetType(String)) dt.Columns.Add("SourceFolder", GetType(String)) dt.Columns.Add("UserFolder", GetType(String)) dt.Columns.Add("DocumentName", GetType(String)) dt.Columns.Add("2", GetType(String)) dt.Columns.Add("3", GetType(String)) For Each filename As String In searchResults Dim parts As String() = filename.Split(New Char() {"\"c}) dt.Rows.Add(parts) Next Dim row As DataRow For Each row In dt.Rows Dim strUploadString As String strUploadString = (strftpTargetDirectory) & (row(1)) & "/" & (row(2)) & "/" & (row(3)) Dim strLocalFilePath As String strLocalFilePath = (strftpTargetDirectory) & (row(1)) & "\" & (row(2)) & "\" & (row(3)) Dim strUploadFileName As String strUploadFileName = (row(0) & "\" & row(1) & "\" & row(2) & "\" & row(3)) Dim credential As New NetworkCredential(strftpUserName, strftpPassword) Upload(strUploadFileName, strUploadString, credential) 'Delete or move the fileafter processing Dim AfterProcessDecision As String AfterProcessDecision = strAfterProcessDecision 'MsgBox(AfterProcessDecision) Select Case AfterProcessDecision Case "move" Dim FileToMove As String FileToMove = (strftpSourceDirectory) & row(2) & "\" & row(3) 'MsgBox(FileToMove, MsgBoxStyle.Information, "FileToMove") Dim FileMovePath As String FileMovePath = (strProcessedDirectory) & row(3) 'MsgBox(FileMovePath, MsgBoxStyle.Information, "FileMovePath") File.Move(FileToMove, FileMovePath) Dim tt As StreamWriter tt = New StreamWriter(Application.StartupPath & "\FtpUploaderLog.txt", True) tt.WriteLine("Document Moved " & "- " & Date.Now & " - " & FileToMove) tt.Flush() tt.Close() 'Rename the document to reflect uploaded status Dim MyFileName As String MyFileName = row(3) Dim FileName = Path.GetFileName(FileToMove) Dim FileExt As String = Path.GetExtension(FileToMove) Dim strFileRenamed As String strFileRenamed = "Uploaded" & " " & FileName & " " & row(2) & " " & Format(Date.Now, "ddmmyy HHmmss") & FileExt My.Computer.FileSystem.RenameFile(FileMovePath, strFileRenamed) Case "delete" Dim FileDeletePath As String FileDeletePath = (strftpSourceDirectory) & row(2) & "\" & row(3) File.Delete(FileDeletePath) Dim tt As StreamWriter tt = New StreamWriter(Application.StartupPath & "\FtpUploaderLog.txt", True) tt.WriteLine("Document Deleted " & "- " & Date.Now & " - " & FileDeletePath) tt.Flush() tt.Close() Case Else End Select Next EventLog1.WriteEntry("Process Completed.") dt.Clear() dt.Dispose() End If Catch ex As Exception Dim t As StreamWriter t = New StreamWriter(Application.StartupPath & "\FtpUploaderLog.txt", True) t.WriteLine("Exception Message From Private Sub Timer1_Tick " & "- " & Date.Now & " - " & ex.Message) t.Flush() t.Close() EventLog1.WriteEntry("Process Not Completed. Review The Log File") End Try 'Finally check the size of the log file and replace it if it is 1mb or greater in size Dim strPath As String = (Application.StartupPath) Dim strFileName As String = "FtpUploaderLog.txt" Dim info As New FileInfo(strPath & "\" & strFileName) Dim length As Long = info.Length Dim Count As Integer If Len(Dir$(Application.StartupPath & "\" & "*.txt")) Then Count = Count + 1 Do While Len(Dir$) <> 0 Count = Count + 1 Loop Dim strNewCount As String strNewCount = Count + 1 If length >= 1048576 Then My.Computer.FileSystem.RenameFile(Application.StartupPath & "\" & strFileName, "[" & Count & "]" & strFileName) Dim sw As StreamWriter = File.CreateText(Application.StartupPath & "\" & strFileName) sw.Close() End If Me.Timer1.Enabled = True


Reply With Quote
