public void Run() { try { string timeStamp = DateTime.Now.ToString("yyyyMMddHHmmssffff"); string LogFileName = LogFolder + "XMLToRead_" + timeStamp + ".txt"; FileStream fs = new FileStream(LogFileName, FileMode.CreateNew, FileAccess.Write); StreamWriter sw = new StreamWriter(fs); sw.WriteLine(DateTime.Now); int AffectedFileCount = 0; DirectoryInfo desFolder = new DirectoryInfo(DestinationFolder); int filesInTemonous = desFolder.GetFiles().Count(); if (filesInTemonous == 0) { if (Directory.Exists(SourceFolder) && Directory.Exists(DestinationFolder)) { using (SqlConnection connection = new SqlConnection(ConnectionString)) { connection.Open(); DirectoryInfo info = new DirectoryInfo(SourceFolder); FileInfo[] files = info.GetFiles().OrderBy(p => p.Name).Take(500).ToArray(); foreach (FileInfo file in files) { if (File.Exists(file.FullName)) { sw.Write(file.FullName); try { string AccountNumber, YearMonth, Amount, FileId; AccountNumber = YearMonth = Amount = FileId = "N/A"; doc.Load(file.FullName); File.Copy(file.FullName, RawXMLBackUp + "//" + file.Name, true); XmlNodeList elements = doc.GetElementsByTagName("CREDIT_ACCT_NO"); if (elements.Count > 0) { AccountNumber = elements[0].InnerText; } elements = doc.GetElementsByTagName("DEBIT_AMOUNT"); if (elements.Count > 0) { Amount = elements[0].InnerText; } elements = doc.GetElementsByTagName("entereddatetime"); if (elements.Count > 0) { YearMonth = elements[0].InnerText; doc.DocumentElement.RemoveChild(elements[0]); } elements = doc.GetElementsByTagName("UniqueId"); if (elements.Count > 0) { FileId = elements[0].InnerText; doc.DocumentElement.RemoveChild(elements[0]); } string Tmp = $"SELECT FileName FROM IHRMBatchFileTrack WHERE FileId = '{FileId}';"; SqlCommand cmd = new SqlCommand(Tmp, connection); string fileIDCheck = (string)cmd.ExecuteScalar(); if (fileIDCheck != null) { if (File.Exists(ProcessedFolder + "//" + file.Name)) { File.Delete(ProcessedFolder + "//" + file.Name); } File.Move(file.FullName, ProcessedFolder + "//" + file.Name); Tmp = $" INSERT INTO IHRMGateKeeperLog(FileName, Remarks, DateTime) VALUES('{file.Name}', 'File Blocked With status Duplicate ID', getdate());"; cmd = new SqlCommand(Tmp, connection); cmd.ExecuteScalar(); continue; } Tmp = $"SELECT Status FROM IHRMBatchLog WHERE YearMonth = '{YearMonth}' AND AccountNumber = '{AccountNumber}' AND Amount = '{Amount}';"; cmd = new SqlCommand(Tmp, connection); string fileStatusCheck = (string)cmd.ExecuteScalar(); string ACStarts = "N/A"; if (AccountNumber != null && AccountNumber.Length >= 3) { ACStarts = AccountNumber.Substring(0, 3); } if (ACStarts == "BDT" || fileStatusCheck == null || fileStatusCheck == "fail") { doc.Save(file.FullName); File.Copy(file.FullName, DestinationFolder + "\\" + file.Name, true); sw.Write(" | Coppied successfully"); if (File.Exists(BackupFolder + "\\" + file.Name)) { File.Delete(BackupFolder + "\\" + file.Name); } File.Move(file.FullName, BackupFolder + "\\" + file.Name); sw.WriteLine(" | Moved successfully"); AffectedFileCount++; string MyTemmp = fileStatusCheck == null ? "New File" : fileStatusCheck; if (ACStarts == "BDT" || fileStatusCheck == null) { Tmp = $"INSERT INTO IHRMBatchLog (FileName, AccountNumber, YearMonth, Amount, Status, InitialDateTime) VALUES('{file.Name}','{AccountNumber}','{YearMonth}','{Amount}','posted',getdate());"; cmd = new SqlCommand(Tmp, connection); cmd.ExecuteScalar(); } else { Tmp = $"Update IHRMBatchLog SET FileName = '{file.Name}', Status='posted' WHERE YearMonth = '{YearMonth}' AND AccountNumber = '{AccountNumber}' AND Amount = '{Amount}';"; cmd = new SqlCommand(Tmp, connection); cmd.ExecuteScalar(); } Tmp = $"INSERT INTO IHRMBatchFileTrack (FileId, FileName, DateTime) VALUES('{FileId}','{file.Name}',getdate());"; cmd = new SqlCommand(Tmp, connection); cmd.ExecuteScalar(); Tmp = $" INSERT INTO IHRMGateKeeperLog(FileName, Remarks, DateTime) VALUES('{file.Name}', 'File Pass With status {MyTemmp}', getdate());"; cmd = new SqlCommand(Tmp, connection); cmd.ExecuteScalar(); } else if (fileStatusCheck == "success") { if (File.Exists(DuplicateFolder + "//" + file.Name)) { File.Delete(DuplicateFolder + "//" + file.Name); } File.Move(file.FullName, DuplicateFolder + "//" + file.Name); Tmp = $" INSERT INTO IHRMGateKeeperLog(FileName, Remarks, DateTime) VALUES('{file.Name}', 'File blocked With status Already Success', getdate());"; cmd = new SqlCommand(Tmp, connection); cmd.ExecuteScalar(); } } catch (IOException e) { sw.WriteLine(e.Message); Console.WriteLine(e.Message); } } else { sw.WriteLine("File Cn't Found While moving"); } } } } else { sw.WriteLine("Directory Missing.."); } Console.WriteLine(DateTime.Now + " | Done."); } else { Console.WriteLine(DateTime.Now + " | Temonous Is Busy."); sw.WriteLine("Temonous In Busy Now"); } DirectoryInfo Temp = new DirectoryInfo(SourceFolder); Console.WriteLine("Files In Queue: " + Temp.GetFiles().Count()); sw.Flush(); sw.Close(); fs.Close(); if (AffectedFileCount == 0) { File.Delete(LogFileName); } } catch (Exception e) { Console.WriteLine(e.Message); } }