
Hi i am trying to add console to my code i am getting this error and log file is saved in Z drive but there is no content in that file and file was empty can help what are the things need to be changes.
Public Sub FileCopy(src As String, dest As String)
Dim strFile As String = "C:\ErrorLog_" & DateTime.Today.ToString("dd-MMM-yyyy") & ".txt"
Dim sw As StreamWriter
Try
If (Not File.Exists(strFile)) Then
sw = File.CreateText(strFile)
Else
sw = File.AppendText(strFile)
End If
sw.WriteLine("Start Error Log for today")
Dim totalFiles As Integer = 0
For Each file__1 As String In Directory.GetFiles(src, "*.pdf", SearchOption.AllDirectories)
Dim lastOneMonth As DateTime = DateTime.Now.AddMonths(-1)
Dim filePath As String = Path.GetDirectoryName(file__1)
Dim fileName As String = Path.GetFileNameWithoutExtension(file__1.ToString())
If fileName.Contains("_I-") OrElse fileName.Contains("_IS") OrElse fileName.Contains("_C-") AndAlso fileName.Length = 10 Then
Dim newFullName As String = dest & "\" & fileName & ".pdf"
File.Copy(file__1.ToString(), newFullName, True)
totalFiles += 1
End If
Next
Dim tFiles As Integer = totalFiles
Catch ex As Exception
End Try
End Sub
Public Sub FileRename(dest As String)
Dim totFiles As Integer = 0
Dim separators As String() = New String() {"_"}
Dim sw As StreamWriter
Try
For Each f As String In Directory.GetFiles(dest, "*.pdf", SearchOption.AllDirectories)
Dim lastOneMonth As DateTime = DateTime.Now.AddMonths(-1)
totFiles += 1
Dim onlyPath As String = Path.GetDirectoryName(f)
sw.WriteLine("OnlyPath: " + onlyPath)
Dim name As String = Path.GetFileNameWithoutExtension(f.ToString())
sw.WriteLine("Name: " + name)
If name.Contains("_I-") OrElse name.Contains("_IS") OrElse name.Contains("_C-") Then
If name.Contains("_I-") Then
Dim arrName As String() = name.Split(separators, StringSplitOptions.None)
For i As Integer = 0 To arrName.Length - 1
If arrName(i).StartsWith("I-") Then
name = arrName(i) & ".pdf"
End If
Next
ElseIf name.Contains("_IS") Then
Dim arrName As String() = name.Split(separators, StringSplitOptions.None)
For i As Integer = 0 To arrName.Length - 1
If arrName(i).StartsWith("IS") Then
name = arrName(i) & ".pdf"
End If
Next
ElseIf name.Contains("_C-") Then
Dim arrName As String() = name.Split(separators, StringSplitOptions.None)
For i As Integer = 0 To arrName.Length - 1
If arrName(i).StartsWith("C-") Then
name = arrName(i) & ".pdf"
End If
Next
End If
Dim newFullName As String = onlyPath & "\" & name
sw.WriteLine("newFullName: " + newFullName)
If File.Exists(newFullName) Then
File.Copy(f.ToString(), newFullName, True)
sw.WriteLine("File copied")
File.Delete(f.ToString())
sw.WriteLine("File Deleted")
Else
File.Move(f.ToString(), onlyPath & "\" & name)
sw.WriteLine("File Moved")
End If
End If
Next
Catch e As System.IO.IOException
Console.WriteLine(e)
sw.WriteLine("Error Message: '" & e.Message & "', Occured at-- " & DateTime.Now)
End Try
Dim NFiles As Integer = totFiles
sw.Close()
' Catch ex As IOException
MsgBox("Error writing to log file.")
' End Try
End Sub