How to Split File in C# ASP.NET

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using System.Data.SqlClient;  
  10. using System.IO;  
  11. using System.Configuration;  
  12. using NACH_Application;  
  13. using System.Data.Odbc;  
  14. namespace H2H  
  15. {  
  16.     public class H2HSplitMergeFiles  
  17.     {  
  18.         string m_QryStr, m_Read, M_FLNM, fileaction, SFilename, MFilename;  
  19.         SqlDataReader DR;  
  20.         StreamWriter STRWRT;  
  21.         StreamReader sr;  
  22.         int recordcount, rowsize, hd_count_field, hd_amt_field, dt_amt_field, countVFHDfrom, countVFHDto, sumVFHDfrom, sumVFHDto, sumVFDTfrom, sumVFDTto, refVFHDfrom, refVFHDto, refVFDTfrom, refVFDTto;  
  23.         int diff_hd_sum, diff_hd_count, diff_dt_sum, diff_hd_ref, diff_dt_ref, loop_count;  
  24.         string hd_data, dt_data = "";  
  25.         int varfld1, count;  
  26.         int batchno, maxbatchno;  
  27.         int m_TotCount;  
  28.         string[] file;  
  29.         string fileformat, file_1st, Mfile_1st;  
  30.         string[] words;  
  31.         string[] Mwords;  
  32.         string[] Mfile;  
  33.         string[] Mfiles;  
  34.         List<string> files = new List<string>();  
  35.         //string[] files;  
  36.         int Mm_TotCount;  
  37.         string item;  
  38.         int s, m;  
  39.         string Mfileformat;  
  40.         List<string> MergeFiles = new List<string>();  
  41.         string head_data, row_deatils = "";  
  42.         int line;  
  43.         int index = 0;  
  44.         string TXNTYPE = "";  
  45.         string Destination_Folder;  
  46.         Class1 DbObj = new Class1();  
  47.         void Split(string Destination)  
  48.         {  
  49.             if (true)  
  50.             {  
  51.                 #region Getting Split for ACH and APB UCO  
  52.                 #region Getting Split Details from Database  
  53.                 DbObj.log_write("Clear Temp Table Data......");  
  54.                 m_QryStr = "truncate table temp_header_data";  
  55.                 DbObj.gl_ExeNonQuery(m_QryStr);  
  56.                 m_QryStr = "truncate table temp_details_data";  
  57.                 DbObj.gl_ExeNonQuery(m_QryStr);  
  58.                 string format = fileformat.Replace("-""");  
  59.                 recordcount = 0;  
  60.                 string File_MST_ID = "";  
  61.                 m_QryStr = "Select RECORDS,ROW_SIZE,FILE_MST_ID from FILE_MASTER where CATEGORY='" + format + "' and [TXNTYPE]='" + TXNTYPE + "' and FILE_ACTION='SPLIT'";  
  62.                 DR = DbObj.gl_ExeReader(m_QryStr);  
  63.                 DbObj.log_write("\n Query :" + m_QryStr);  
  64.                 if (DR.Read())  
  65.                 {  
  66.                     recordcount = Convert.ToInt32(DR["RECORDS"].ToString());  
  67.                     rowsize = Convert.ToInt32(DR["ROW_SIZE"].ToString());  
  68.                     File_MST_ID = DR["FILE_MST_ID"].ToString();  
  69.                 }  
  70.                 DR.Close();  
  71.                 m_QryStr = "Select VF_HD_FROM,VF_HD_TO from SPLIT_FILE_MASTER where CATEGORY='" + format + "' and ACTION='COUNT' and File_MST_ID ='" + File_MST_ID + "' ";  
  72.                 DR = DbObj.gl_ExeReader(m_QryStr);  
  73.                 DbObj.log_write("\n Query :" + m_QryStr);  
  74.                 if (DR.Read())  
  75.                 {  
  76.                     countVFHDfrom = Convert.ToInt32(DR["VF_HD_FROM"].ToString());  
  77.                     countVFHDto = Convert.ToInt32(DR["VF_HD_TO"].ToString());  
  78.                     diff_hd_count = (countVFHDto - countVFHDfrom);  
  79.                 }  
  80.                 DR.Close();  
  81.                 m_QryStr = "select VF_HD_FROM,VF_HD_TO,VF_DT_FROM,VF_DT_TO from SPLIT_FILE_MASTER where CATEGORY='" + format + "' and ACTION='SUM' and File_MST_ID ='" + File_MST_ID + "'";  
  82.                 DR = DbObj.gl_ExeReader(m_QryStr);  
  83.                 DbObj.log_write("\nQuery :" + m_QryStr);  
  84.                 if (DR.Read())  
  85.                 {  
  86.                     sumVFHDfrom = Convert.ToInt32(DR["VF_HD_FROM"].ToString());  
  87.                     sumVFHDto = Convert.ToInt32(DR["VF_HD_TO"].ToString());  
  88.                     diff_hd_sum = (sumVFHDto - sumVFHDfrom);  
  89.                     sumVFDTfrom = Convert.ToInt32(DR["VF_DT_FROM"].ToString());  
  90.                     sumVFDTto = Convert.ToInt32(DR["VF_DT_TO"].ToString());  
  91.                     diff_dt_sum = (sumVFDTto - sumVFDTfrom);  
  92.                 }  
  93.                 DR.Close();  
  94.                 //--------------------------------refrence---------------------------------------------------------  
  95.                 m_QryStr = "select VF_HD_FROM,VF_HD_TO,VF_DT_FROM,VF_DT_TO from SPLIT_FILE_MASTER where CATEGORY='" + format + "' and ACTION='REFNO'";  
  96.                 DR = DbObj.gl_ExeReader(m_QryStr);  
  97.                 DbObj.log_write("\n Query :" + m_QryStr);  
  98.                 if (DR.Read())  
  99.                 {  
  100.                     refVFHDfrom = Convert.ToInt32(DR["VF_HD_FROM"].ToString());  
  101.                     refVFHDto = Convert.ToInt32(DR["VF_HD_TO"].ToString());  
  102.                     diff_hd_ref = (refVFHDto - refVFHDfrom);  
  103.                     refVFDTfrom = Convert.ToInt32(DR["VF_DT_FROM"].ToString());  
  104.                     refVFDTto = Convert.ToInt32(DR["VF_DT_TO"].ToString());  
  105.                     diff_dt_ref = (refVFDTto - refVFDTfrom);  
  106.                 }  
  107.                 DR.Close();  
  108.                 //--------------------------------refrence---------------------------------------------------------  
  109.                 if (recordcount < m_TotCount)  
  110.                 {  
  111.                     int i = 0;  
  112.                     for (i = 0; i < m_TotCount; i++)  
  113.                     {  
  114.                         if ((m_Read = File.ReadLines(files[s]).ElementAt(i).ToString()) != "")  
  115.                         {  
  116.                             if (i == 0)  
  117.                             {  
  118.                                 hd_data = m_Read.Substring(0);  
  119.                                 hd_count_field = Convert.ToInt32(m_Read.Substring(countVFHDfrom, diff_hd_count));  
  120.                                 hd_amt_field = Convert.ToInt32(((diff_hd_sum != 0) ? m_Read.Substring(sumVFHDfrom, diff_hd_sum) : 0 + ""));  
  121.                                 m_QryStr = "INSERT INTO temp_header_data (hd_data,var_fld_1,var_fld_2)" +  
  122.                                 " VALUES ('" + hd_data + "','" + hd_count_field + "','" + hd_amt_field + "' )";  
  123.                                 //" TO_DATE('" + M_STLMNTDT + "','DD/MM/YYYY'),'" + M_DESTIFSC + "','" + M_CYCLE  
  124.                                 DbObj.gl_ExeNonQuery(m_QryStr);  
  125.                                 DbObj.log_write("\n Insert Header into temp table Query :" + m_QryStr);  
  126.                             }  
  127.                             if (i > 0)  
  128.                             {  
  129.                                 dt_data = m_Read.Substring(0);  
  130.                                 dt_amt_field = Convert.ToInt32(((diff_dt_sum != 0) ? m_Read.Substring(sumVFDTfrom, diff_dt_sum) : 0 + ""));  
  131.                                 m_QryStr = "INSERT INTO temp_details_data (dt_data,var_fld_1)" +  
  132.                                 " VALUES ('" + dt_data + "','" + dt_amt_field + "')";  
  133.                                 DbObj.gl_ExeNonQuery(m_QryStr);  
  134.                                 // DbObj.log_write("\n Insert Recoard into temp table Query :" + m_QryStr);  
  135.                             }  
  136.                         }  
  137.                     }  
  138.                     DbObj.log_write(i + "");  
  139.                 }  
  140.                 #endregion  
  141.                 # region "SPLIT"  
  142.                 loop_count = (m_TotCount - 1) / recordcount;  
  143.                 if ((m_TotCount - 1) % recordcount != 0)  
  144.                 {  
  145.                     loop_count = loop_count + 1;  
  146.                 }  
  147.                 //batchno = 1;  
  148.                 // Getting Batch No on file name....  
  149.                 long batch = 0;// long.Parse(file[5]);  
  150.                 // batch = (batch == 0) ? 0 : (batch + 1);  
  151.                 //.......................  
  152.                 m_QryStr = "select * from file_master where CATEGORY='" + file[0] + file[1] + "' AND TXNTYPE='" + TXNTYPE + "'";  
  153.                 DataSet DS = DbObj.gl_ExeDataSet(m_QryStr);  
  154.                 DbObj.log_write("\n Select File_master Query :" + m_QryStr);  
  155.                 if (DS.Tables[0].Rows.Count > 0)  
  156.                 {  
  157.                     m_QryStr = "select count(FILE_MST_ID) from SplitFile_Details where FILE_MST_ID='" + DS.Tables[0].Rows[0]["FILE_MST_ID"].ToString() + "' and Convert (varchar(10),[date],103) ='" + DateTime.Now.ToString("dd/MM/yyyy") + "'";  
  158.                     batch = ((DbObj.gl_exeScalar(m_QryStr)).ToString() == "0") ? 1 : long.Parse(DbObj.gl_exeScalar(m_QryStr).ToString()) + 1;  
  159.                     DbObj.log_write("\n Select Max Batch No Query :" + m_QryStr);  
  160.                 }  
  161.                 for (int c = 0; c < loop_count; c++)  
  162.                 {  
  163.                     string data = DateTime.Now.ToString("dd/MM/yyyy");  
  164.                     string[] datetext = data.Split(' ');  
  165.                     string date = datetext[0];  
  166.                     //MAX BATCH...  
  167.                     //puneet change.....25 jan  
  168.                     //m_QryStr = "select (MAX(Batchno)) as BatchNo from maxbatchbackup where convert(varchar,UploadDate,103)='" + date + "'";  
  169.                     m_QryStr = "select * from file_master where CATEGORY='" + file[0] + file[1] + "' AND TXNTYPE='" + TXNTYPE + "'";  
  170.                     DS = DbObj.gl_ExeDataSet(m_QryStr);  
  171.                     DbObj.log_write("\n Select File_master Query :" + m_QryStr);  
  172.                     if (DS.Tables[0].Rows.Count > 0)  
  173.                     {  
  174.                         string file_batchno = "";  
  175.                         file_batchno = batch.ToString().PadLeft(6, '0');  
  176.                         m_QryStr = "select * from temp_header_data";  
  177.                         DataSet dshddetails = DbObj.gl_ExeDataSet(m_QryStr);  
  178.                         DbObj.log_write("\n Select Header into temp table Query :" + m_QryStr);  
  179.                         if (dshddetails.Tables[0].Rows.Count > 0)  
  180.                         {  
  181.                             head_data = dshddetails.Tables[0].Rows[0]["hd_data"].ToString();  
  182.                         }  
  183.                         //---------------header data + total count--------------------  
  184.                         m_QryStr = "select top " + recordcount + " * from temp_details_data select top " + recordcount + " * from temp_details_data ";  
  185.                         DataSet dsdtrowdetails = DbObj.gl_ExeDataSet(m_QryStr);  
  186.                         DbObj.log_write("\n Select Recoard into temp table Query :" + m_QryStr);  
  187.                         count = dsdtrowdetails.Tables[0].Rows.Count;  
  188.                         //-----------------total sum-----------------------------------  
  189.                         m_QryStr = "select SUM(var_fld_1) from temp_details_data where dt_id in (select top " + recordcount + " dt_id from temp_details_data)";  
  190.                         DataSet dsvarfld1 = DbObj.gl_ExeDataSet(m_QryStr);  
  191.                         DbObj.log_write("\n Select Amount into temp table Query :" + m_QryStr);  
  192.                         if (dsvarfld1.Tables[0].Rows.Count > 0)  
  193.                         {  
  194.                             varfld1 = Convert.ToInt32(dsvarfld1.Tables[0].Rows[0][0].ToString());  
  195.                         }  
  196.                         if (((file[0] + file[1]).ToUpper() == "APBCR" || (file[0] + file[1]).ToUpper() == "ACHCR") && (file[5].ToUpper() == "INW" || file[6].ToUpper() == "RTN"))  
  197.                         {  
  198.                             M_FLNM = Destination + "\\" + DS.Tables[0].Rows[0]["CBS_PFX_NAME"].ToString() + file[3] + batch.ToString().PadLeft(6, '0') + DS.Tables[0].Rows[0]["CBS_SFX_NAME"].ToString() + "." + words[1] + ""; //file[6]  
  199.                             DbObj.log_write("\n File NAme :" + M_FLNM);  
  200.                         }  
  201.                         else  
  202.                         {  
  203.                             M_FLNM = Destination + "\\" + file[0] + "-" + file[1] + "-" + file[2] + "-" + file[3] + "-" + batch.ToString().PadLeft(6, '0') + "-" + TXNTYPE + Path.GetExtension(files[s]);  
  204.                             DbObj.log_write("\n File NAme :" + M_FLNM);  
  205.                         }  
  206.                         if (!File.Exists(M_FLNM)) //607066D  
  207.                         {  
  208.                             STRWRT = new StreamWriter(M_FLNM);  
  209.                             DbObj.log_write("\n Created File :" + M_FLNM);  
  210.                         }  
  211.                         else  
  212.                         {  
  213.                             File.Delete(M_FLNM);  
  214.                             STRWRT = new StreamWriter(M_FLNM);  
  215.                         }  
  216.                         //------------------  
  217.                         string fn = Path.GetFileName(M_FLNM);  
  218.                         string now = DateTime.Now.ToString("dd/MM/yyyy");  
  219.                         string fileid = "";  
  220.                         m_QryStr = " select FileID from File_Details where Filename='" + SFilename + "'";  
  221.                         DR = DbObj.gl_ExeReader(m_QryStr);  
  222.                         if (DR.Read())  
  223.                         {  
  224.                             fileid = (DR["FileID"].ToString());  
  225.                         }  
  226.                         DR.Close();  
  227.                         m_QryStr = "insert into SplitFile_Details (FileID,FileName,Splitlimit,Date,FILE_MST_ID) values('" + fileid.ToString() + "','" + fn + "','" + count + "',convert(datetime,'" + now + "',103),'" + DS.Tables[0].Rows[0]["FILE_MST_ID"].ToString() + "')";  
  228.                         DbObj.gl_ExeNonQuery(m_QryStr);  
  229.                         //--------------------  
  230.                         string hd_var_fld_1 = count.ToString().PadLeft(diff_hd_count, '0');  
  231.                         string hd_var_fld_2 = varfld1.ToString().PadLeft(diff_hd_sum, '0');  
  232.                         var aStringBuilder = new StringBuilder(head_data);  
  233.                         aStringBuilder.Remove(countVFHDfrom, diff_hd_count);  
  234.                         aStringBuilder.Insert(countVFHDfrom, hd_var_fld_1);  
  235.                         head_data = aStringBuilder.ToString();  
  236.                         aStringBuilder.Remove(sumVFHDfrom, diff_hd_sum);  
  237.                         aStringBuilder.Insert(sumVFHDfrom, hd_var_fld_2);  
  238.                         head_data = aStringBuilder.ToString();  
  239.                         STRWRT.WriteLine(string.Format("{0,-" + rowsize + "}", head_data));  
  240.                         for (int y = 0; y < dsdtrowdetails.Tables[0].Rows.Count; y++)  
  241.                         {  
  242.                             row_deatils = dsdtrowdetails.Tables[0].Rows[y]["dt_data"].ToString();  
  243.                             STRWRT.WriteLine(string.Format("{0,-" + rowsize + "}", row_deatils));  
  244.                         }  
  245.                         STRWRT.Close();  
  246.                         m_QryStr = "delete top (" + recordcount + ") from temp_details_data";  
  247.                         DbObj.gl_ExeNonQuery(m_QryStr);  
  248.                         m_QryStr = "insert into max_batch (Batchno,UploadDate) values(" + batchno + ",convert(datetime,'" + date + "',103))";  
  249.                         DbObj.gl_ExeNonQuery(m_QryStr);  
  250.                         //MessageBox.Show("File Created");  
  251.                         //batchno++;  
  252.                         batch++;  
  253.                     }  
  254.                 }  
  255.                 #endregion  
  256.                 #endregion  
  257.                 File.Delete(files[s]);  
  258.             }  
  259.             #region other TXT file formate  
  260.             else  
  261.             {/* 
  262. #region Getting Split Details from Database 
  263. m_QryStr = "truncate table temp_header_data"; 
  264. DbObj.gl_ExeNonQuery(m_QryStr); 
  265. m_QryStr = "truncate table temp_details_data"; 
  266. DbObj.gl_ExeNonQuery(m_QryStr); 
  267. string format = fileformat.Replace("-", ""); 
  268. recordcount = 0; 
  269. string File_MST_ID = ""; 
  270. m_QryStr = "Select RECORDS,ROW_SIZE,FILE_MST_ID from FILE_MASTER where CATEGORY='" + format + "' and [TXNTYPE]='" + file[6] + "' and FILE_ACTION='SPLIT'"; 
  271. DR = DbObj.gl_ExeReader(m_QryStr); 
  272. if (DR.Read()) 
  273. { 
  274. recordcount = Convert.ToInt32(DR["RECORDS"].ToString()); 
  275. rowsize = Convert.ToInt32(DR["ROW_SIZE"].ToString()); 
  276. File_MST_ID = DR["FILE_MST_ID"].ToString(); 
  277. } 
  278. DR.Close(); 
  279. m_QryStr = "Select VF_HD_FROM,VF_HD_TO from SPLIT_FILE_MASTER where CATEGORY='" + format + "' and ACTION='COUNT' and File_MST_ID ='"+File_MST_ID+"' "; 
  280. DR = DbObj.gl_ExeReader(m_QryStr); 
  281. if (DR.Read()) 
  282. { 
  283. countVFHDfrom = Convert.ToInt32(DR["VF_HD_FROM"].ToString()); 
  284. countVFHDto = Convert.ToInt32(DR["VF_HD_TO"].ToString()); 
  285. diff_hd_count = (countVFHDto - countVFHDfrom); 
  286. } 
  287. DR.Close(); 
  288. m_QryStr = "select VF_HD_FROM,VF_HD_TO,VF_DT_FROM,VF_DT_TO from SPLIT_FILE_MASTER where CATEGORY='" + format + "' and ACTION='SUM' and File_MST_ID ='" + File_MST_ID + "'"; 
  289. DR = DbObj.gl_ExeReader(m_QryStr); 
  290. if (DR.Read()) 
  291. { 
  292. sumVFHDfrom = Convert.ToInt32(DR["VF_HD_FROM"].ToString()); 
  293. sumVFHDto = Convert.ToInt32(DR["VF_HD_TO"].ToString()); 
  294. diff_hd_sum = (sumVFHDto - sumVFHDfrom); 
  295. sumVFDTfrom = Convert.ToInt32(DR["VF_DT_FROM"].ToString()); 
  296. sumVFDTto = Convert.ToInt32(DR["VF_DT_TO"].ToString()); 
  297. diff_dt_sum = (sumVFDTto - sumVFDTfrom); 
  298. } 
  299. DR.Close(); 
  300. //--------------------------------refrence--------------------------------------------------------- 
  301. m_QryStr = "select VF_HD_FROM,VF_HD_TO,VF_DT_FROM,VF_DT_TO from SPLIT_FILE_MASTER where CATEGORY='" + format + "' and ACTION='REFNO'"; 
  302. DR = DbObj.gl_ExeReader(m_QryStr); 
  303. if (DR.Read()) 
  304. { 
  305. refVFHDfrom = Convert.ToInt32(DR["VF_HD_FROM"].ToString()); 
  306. refVFHDto = Convert.ToInt32(DR["VF_HD_TO"].ToString()); 
  307. diff_hd_ref = (refVFHDto - refVFHDfrom); 
  308. refVFDTfrom = Convert.ToInt32(DR["VF_DT_FROM"].ToString()); 
  309. refVFDTto = Convert.ToInt32(DR["VF_DT_TO"].ToString()); 
  310. diff_dt_ref = (refVFDTto - refVFDTfrom); 
  311. } 
  312. DR.Close(); 
  313. //--------------------------------refrence--------------------------------------------------------- 
  314. if (recordcount < m_TotCount) 
  315. { 
  316. int i = 0; 
  317. for (i = 0; i < m_TotCount; i++) 
  318. { 
  319. if ((m_Read = File.ReadLines(files[s]).ElementAt(i).ToString()) != "") 
  320. { 
  321. if (i == 0) 
  322. { 
  323. hd_data = m_Read.Substring(0); 
  324. hd_count_field = Convert.ToInt32(m_Read.Substring(countVFHDfrom, diff_hd_count)); 
  325. hd_amt_field = Convert.ToInt32(m_Read.Substring(sumVFHDfrom, diff_hd_sum)); 
  326. m_QryStr = "INSERT INTO temp_header_data (hd_data,var_fld_1,var_fld_2)" + 
  327. " VALUES ('" + hd_data + "','" + hd_count_field + "','" + hd_amt_field + "' )"; 
  328. //" TO_DATE('" + M_STLMNTDT + "','DD/MM/YYYY'),'" + M_DESTIFSC + "','" + M_CYCLE 
  329. DbObj.gl_ExeNonQuery(m_QryStr); 
  330. } 
  331. if (i > 0) 
  332. { 
  333. dt_data = m_Read.Substring(0); 
  334. dt_amt_field = Convert.ToInt32(m_Read.Substring(sumVFDTfrom, diff_dt_sum)); 
  335. m_QryStr = "INSERT INTO temp_details_data (dt_data,var_fld_1)" + 
  336. " VALUES ('" + dt_data + "','" + dt_amt_field + "')"; 
  337. DbObj.gl_ExeNonQuery(m_QryStr); 
  338. } 
  339. } 
  340. } 
  341. DbObj.log_write(" REsult :" + i); 
  342. } 
  343. #endregion 
  344. # region "SPLIT" 
  345. /* 
  346. loop_count = (m_TotCount - 1) / recordcount; 
  347. if ((m_TotCount - 1) % recordcount != 0) 
  348. { 
  349. loop_count = loop_count + 1; 
  350. } 
  351. //batchno = 1; 
  352. // Getting Batch No on file name.... 
  353. long batch = 0;// long.Parse(file[5]); 
  354. // batch = (batch == 0) ? 0 : (batch + 1); 
  355. //....................... 
  356. for (int c = 0; c < loop_count; c++) 
  357. { 
  358. string data = DateTime.Now.ToString("dd/MM/yyyy"); 
  359. string[] datetext = data.Split(' '); 
  360. string date = datetext[0]; 
  361. //MAX BATCH... 
  362. //puneet change.....25 jan 
  363. //m_QryStr = "select (MAX(Batchno)) as BatchNo from maxbatchbackup where convert(varchar,UploadDate,103)='" + date + "'"; 
  364. m_QryStr = "select * from file_master where CATEGORY='" + file[0] + file[1] + "' AND TXNTYPE='" + file[6] + "'"; 
  365. DataSet DS = DbObj.gl_ExeDataSet(m_QryStr); 
  366. if (DS.Tables[0].Rows.Count > 0) 
  367. { 
  368. m_QryStr = "select count(FILE_MST_ID) from SplitFile_Details where FILE_MST_ID='" + DS.Tables[0].Rows[0]["FILE_MST_ID"].ToString() + "' and Convert (varchar(10),[date],103) ='" + DateTime.Now.ToString("dd/MM/yyyy") + "'"; 
  369. batch = ((DbObj.gl_exeScalar(m_QryStr)).ToString() == "0") ? 1 : long.Parse(DbObj.gl_exeScalar(m_QryStr).ToString())+1; 
  370. //............. 
  371. string file_batchno = ""; 
  372. file_batchno = batch.ToString().PadLeft(6, '0'); 
  373. m_QryStr = "select * from temp_header_data"; 
  374. DataSet dshddetails = DbObj.gl_ExeDataSet(m_QryStr); 
  375. if (dshddetails.Tables[0].Rows.Count > 0) 
  376. { 
  377. head_data = dshddetails.Tables[0].Rows[0]["hd_data"].ToString(); 
  378. } 
  379. //---------------header data + total count-------------------- 
  380. m_QryStr = "select top " + recordcount + " * from temp_details_data select top " + recordcount + " * from temp_details_data "; 
  381. DataSet dsdtrowdetails = DbObj.gl_ExeDataSet(m_QryStr); 
  382. count = dsdtrowdetails.Tables[0].Rows.Count; 
  383. //-----------------total sum----------------------------------- 
  384. m_QryStr = "select SUM(var_fld_1) from temp_details_data where dt_id in (select top " + recordcount + " dt_id from temp_details_data)"; 
  385. DataSet dsvarfld1 = DbObj.gl_ExeDataSet(m_QryStr); 
  386. if (dsvarfld1.Tables[0].Rows.Count > 0) 
  387. { 
  388. varfld1 = Convert.ToInt32(dsvarfld1.Tables[0].Rows[0][0].ToString()); 
  389. } 
  390. // @"\SplitOutput\" + 
  391. if (((file[0] + file[1]).ToUpper() == "APBCR" || (file[0] + file[1]).ToUpper() == "ACHCR") && (file[6].ToUpper()=="INW" || file[6].ToUpper()=="RTN")) 
  392. { 
  393. M_FLNM = Destination + "\\" + DS.Tables[0].Rows[0]["CBS_PFX_NAME"].ToString() + file[4] + batch.ToString().PadLeft(6, '0') + DS.Tables[0].Rows[0]["CBS_SFX_NAME"].ToString() + "." + words[1] + ""; //file[6] 
  394. } 
  395. else 
  396. { 
  397. M_FLNM = Destination + "\\"+file[0] + "-" + file[1] + "-" + file[2] + "-" + file[3] + "-" + file[4] + "-" + batch.ToString().PadLeft(6, '0') + "-" + file[6] + Path.GetExtension(files[s]); 
  398. } 
  399. if (!File.Exists(M_FLNM)) //607066D 
  400. { 
  401. STRWRT = new StreamWriter(M_FLNM); 
  402. } 
  403. else 
  404. { 
  405. File.Delete(M_FLNM); 
  406. STRWRT = new StreamWriter(M_FLNM); 
  407. } 
  408. //------------------ 
  409. string fn = Path.GetFileName(M_FLNM); 
  410. string now = DateTime.Now.ToString("dd/MM/yyyy"); 
  411. string fileid = ""; 
  412. m_QryStr = " select FileID from File_Details where Filename='" + SFilename + "'"; 
  413. DR = DbObj.gl_ExeReader(m_QryStr); 
  414. if (DR.Read()) 
  415. { 
  416. fileid = (DR["FileID"].ToString()); 
  417. } 
  418. DR.Close(); 
  419. m_QryStr = "insert into SplitFile_Details (FileID,FileName,Splitlimit,Date,FILE_MST_ID) values('" + fileid.ToString() + "','" + fn + "','" + count + "',convert(datetime,'" + now + "',103),'" + DS.Tables[0].Rows[0]["FILE_MST_ID"].ToString() + "')"; 
  420. DbObj.gl_ExeNonQuery(m_QryStr); 
  421. //------------------ 
  422. string hd_var_fld_1 = count.ToString().PadLeft(diff_hd_count, '0'); 
  423. string hd_var_fld_2 = varfld1.ToString().PadLeft(diff_hd_sum, '0'); 
  424. var aStringBuilder = new StringBuilder(head_data); 
  425. aStringBuilder.Remove(countVFHDfrom, diff_hd_count); 
  426. aStringBuilder.Insert(countVFHDfrom, hd_var_fld_1); 
  427. head_data = aStringBuilder.ToString(); 
  428. aStringBuilder.Remove(sumVFHDfrom, diff_hd_sum); 
  429. aStringBuilder.Insert(sumVFHDfrom, hd_var_fld_2); 
  430. head_data = aStringBuilder.ToString(); 
  431. STRWRT.WriteLine(string.Format("{0,-" + rowsize + "}", head_data)); 
  432. for (int y = 0; y < dsdtrowdetails.Tables[0].Rows.Count; y++) 
  433. { 
  434. row_deatils = dsdtrowdetails.Tables[0].Rows[y]["dt_data"].ToString(); 
  435. STRWRT.WriteLine(string.Format("{0,-" + rowsize + "}", row_deatils)); 
  436. } 
  437. STRWRT.Close(); 
  438. m_QryStr = "delete top (" + recordcount + ") from temp_details_data"; 
  439. DbObj.gl_ExeNonQuery(m_QryStr); 
  440. m_QryStr = "insert into max_batch (Batchno,UploadDate) values(" + batchno + ",convert(datetime,'" + date + "',103))"; 
  441. DbObj.gl_ExeNonQuery(m_QryStr); 
  442. //MessageBox.Show("File Created"); 
  443. //batchno++; 
  444. batch++; 
  445. } 
  446. } 
  447. #endregion 
  448. */  
  449.             }  
  450.             #endregion  
  451.         }  
  452.         public void FILESPLIT(string path1, string Destination, string original, out string message)  
  453.         {  
  454.             DbObj.log_write(path1 + "\n\n\t" + Destination);  
  455.             message = "";  
  456.             try  
  457.             {  
  458.                 //-------------File Split Code-------------------------------  
  459.                 if (Directory.Exists(path1))  
  460.                 {  
  461.                     long n = 0;  
  462.                     var f = Directory.EnumerateFiles(path1, "*.*").Where(s => s.ToLower().EndsWith(".txt") || s.ToLower().EndsWith(".csv")).ToList(); //Directory.GetFiles(path1, "*.txt|*.csv");//, SearchOption.AllDirectories);  
  463.                     //files = Directory.GetFiles(path1);  
  464.                     index = 0;  
  465.                     foreach (string fl in f)  
  466.                     {  
  467.                         files.Add(fl);  
  468.                         index++;  
  469.                     }  
  470.                     if (files.Count > 0)  
  471.                     {  
  472.                         for (s = 0; s < files.Count; s++)  
  473.                         {  
  474.                             string fileextention = Path.GetExtension(files[s].ToUpper());  
  475.                             #region for only APB-CR and ACH CR UCO BANK...  
  476.                             if (fileextention == ".TXT")  
  477.                             {  
  478.                                 SFilename = Path.GetFileName(files[s]);  
  479.                                 words = SFilename.Split('.');  
  480.                                 file = words[0].Split('-');  
  481.                                 int indx = SFilename.LastIndexOf('-');  
  482.                                 TXNTYPE = SFilename.Substring(indx + 1, 3);  
  483.                                 if (indx > 5)  
  484.                                 {  
  485.                                     file_1st = file[0] + "-" + file[1] + "-" + file[2] + "-" + file[3] + "-" + file[4];  
  486.                                     fileformat = file[0] + "-" + file[1];  
  487.                                     if (fileformat == (file[0] + "-" + file[1]))  
  488.                                     {  
  489.                                         m_TotCount = File.ReadLines(files[s]).Count();  
  490.                                         fileaction = "SPLIT";  
  491.                                         //---------------- Getting Destination Folder------------  
  492.                                         m_QryStr = "select folder_Path from File_Master where [CATEGORY]='" + file[0] + file[1] + "' and [FILE_TYPE]='" + words[1].ToUpper() + "' and [FILE_ACTION]='SPLIT' and [TXNTYPE]='" + TXNTYPE + "'";  
  493.                                         if (DbObj.gl_exeScalar(m_QryStr) != null)  
  494.                                         //"" : DbObj.gl_exeScalar(m_QryStr).ToString() != null)  
  495.                                         {  
  496.                                             if (Directory.Exists(DbObj.gl_exeScalar(m_QryStr).ToString()))  
  497.                                                 Destination = DbObj.gl_exeScalar(m_QryStr).ToString();  
  498.                                         }  
  499.                                         string now = DateTime.Now.ToString("dd/MM/yyyy");  
  500.                                         m_QryStr = "insert into File_Details(Filename ,FileMaxlimit,FileAction,Date,TXNTYPE,ProcessType) values ('" + SFilename + "','" + m_TotCount + "','" + fileaction + "',convert(datetime,'" + now + "',103),'" + TXNTYPE + "','" + file[0] + file[1] + "')";  
  501.                                         DbObj.gl_ExeNonQuery(m_QryStr);  
  502.                                         //------------  
  503.                                         string format = fileformat.Replace("-""");  
  504.                                         m_QryStr = "Select RECORDS from FILE_MASTER where CATEGORY='" + format + "'";  
  505.                                         DR = DbObj.gl_ExeReader(m_QryStr);  
  506.                                         if (DR.Read())  
  507.                                         {  
  508.                                             recordcount = Convert.ToInt32(DR["RECORDS"].ToString());  
  509.                                         }  
  510.                                         DR.Close();  
  511.                                         if (m_TotCount > recordcount)  
  512.                                         {  
  513.                                             foreach (string item2 in file)  
  514.                                             {  
  515.                                                 item = item2;  
  516.                                                 if (item == "INW")  
  517.                                                 {  
  518.                                                     Split(Destination);  
  519.                                                 }  
  520.                                                 if (item == "RTN")  
  521.                                                 {  
  522.                                                     Split(Destination);  
  523.                                                 }  
  524.                                                 if (item == "RES")  
  525.                                                 {  
  526.                                                     Split(Destination);  
  527.                                                 }  
  528.                                                 if (item == "MAP")  
  529.                                                 {  
  530.                                                     Split(Destination);  
  531.                                                 }  
  532.                                             }  
  533.                                         }  
  534.                                         else  
  535.                                         {  
  536.                                             DbObj.log_write("Message : " + " Spliting not needed");  
  537.                                             if (((file[0] + file[1]).ToUpper() == "APBCR" || (file[0] + file[1]).ToUpper() == "ACHCR") && (TXNTYPE.ToUpper() == "INW" || file[5].ToUpper() == "RTN"))  
  538.                                             {  
  539.                                                 string file_name = "";// Path.GetFileName(M_FLNM);  
  540.                                                 string Today_Date = DateTime.Now.ToString("dd/MM/yyyy");  
  541.                                                 string fileid = "";  
  542.                                                 m_QryStr = " select FileID from File_Details where Filename='" + SFilename + "'";  
  543.                                                 DR = DbObj.gl_ExeReader(m_QryStr);  
  544.                                                 if (DR.Read())  
  545.                                                 {  
  546.                                                     fileid = (DR["FileID"].ToString());  
  547.                                                 }  
  548.                                                 DR.Close();  
  549.                                                 m_QryStr = "select * from file_master where CATEGORY='" + file[0] + file[1] + "' AND TXNTYPE='" + TXNTYPE + "'";  
  550.                                                 DataSet DS = DbObj.gl_ExeDataSet(m_QryStr);  
  551.                                                 if (DS.Tables[0].Rows.Count > 0)  
  552.                                                 {  
  553.                                                     long Batch = 0;  
  554.                                                     m_QryStr = "select count(FILE_MST_ID) from SplitFile_Details where FILE_MST_ID='" + DS.Tables[0].Rows[0]["FILE_MST_ID"].ToString() + "' and Convert (varchar(10),[date],103) ='" + DateTime.Now.ToString("dd/MM/yyyy") + "'";  
  555.                                                     Batch = ((DbObj.gl_exeScalar(m_QryStr)).ToString() == "0") ? 0 : long.Parse(DbObj.gl_exeScalar(m_QryStr).ToString());  
  556.                                                     //DataSet DS = DbObj.gl_ExeDataSet(m_QryStr);  
  557.                                                     file_name = DS.Tables[0].Rows[0]["CBS_PFX_NAME"] + file[4] + Batch.ToString().PadLeft(6, '0') + DS.Tables[0].Rows[0]["CBS_SFX_NAME"] + Path.GetExtension(files[s]);  
  558.                                                     m_QryStr = "insert into SplitFile_Details (FileID,FileName,Splitlimit,Date) values('" + fileid.ToString() + "','" + file_name + "','" + m_TotCount + "',convert(datetime,'" + Today_Date + "',103))";  
  559.                                                     DbObj.gl_ExeNonQuery(m_QryStr);  
  560.                                                     File.Move(files[s], Destination + "\\" + file_name);  
  561.                                                     File.Delete(files[s]);  
  562.                                                 }  
  563.                                             }  
  564.                                             else  
  565.                                             {  
  566.                                                 string file_name = "";// Path.GetFileName(M_FLNM);  
  567.                                                 string Today_Date = DateTime.Now.ToString("dd/MM/yyyy");  
  568.                                                 string fileid = "";  
  569.                                                 m_QryStr = " select FileID from File_Details where Filename='" + SFilename + "'";  
  570.                                                 DR = DbObj.gl_ExeReader(m_QryStr);  
  571.                                                 if (DR.Read())  
  572.                                                 {  
  573.                                                     fileid = (DR["FileID"].ToString());  
  574.                                                 }  
  575.                                                 DR.Close();  
  576.                                                 m_QryStr = "select * from file_master where CATEGORY='" + file[0] + file[1] + "' AND TXNTYPE='" + TXNTYPE + "'";  
  577.                                                 DataSet DS = DbObj.gl_ExeDataSet(m_QryStr);  
  578.                                                 if (DS.Tables[0].Rows.Count > 0)  
  579.                                                 {  
  580.                                                     long Batch = 0;  
  581.                                                     m_QryStr = "select count(FILE_MST_ID) from SplitFile_Details where FILE_MST_ID='" + DS.Tables[0].Rows[0]["FILE_MST_ID"].ToString() + "' ";  
  582.                                                     Batch = ((DbObj.gl_exeScalar(m_QryStr)).ToString() == "0") ? 1 : long.Parse(DbObj.gl_exeScalar(m_QryStr).ToString() + 1);  
  583.                                                     file_name = file[0] + "-" + file[1] + "-" + file[2] + "-" + file[3] + "-" + file[4] + "-" + Batch.ToString().PadLeft(6, '0') + "-" + TXNTYPE + Path.GetExtension(files[s]);  
  584.                                                     m_QryStr = "insert into SplitFile_Details (FileID,FileName,Splitlimit,Date) values('" + fileid.ToString() + "','" + file_name + "','" + m_TotCount + "',convert(datetime,'" + Today_Date + "',103))";  
  585.                                                     DbObj.gl_ExeNonQuery(m_QryStr);  
  586.                                                     File.Move(files[s], Destination + "\\" + file_name);  
  587.                                                     File.Delete(files[s]);  
  588.                                                 }  
  589.                                             }  
  590.                                         }  
  591.                                     }  
  592.                                 }  
  593.                             #endregion  
  594.                                 else  
  595.                                 {  
  596.                                     #region TXT File Split Function  
  597.                                     /* 
  598. * 
  599. if (fileextention == ".TXT") 
  600. { 
  601. SFilename = Path.GetFileName(files[s]); 
  602. words = SFilename.Split('.'); 
  603. file = words[0].Split('-'); 
  604. //batchno = Convert.ToInt32(file[5].ToString()); 
  605. file_1st = file[0] + "-" + file[1] + "-" + file[2] + "-" + file[3] + "-" + file[4]; 
  606. fileformat = SFilename.Substring(0, 6); 
  607. if (fileformat == (file[0] + "-" + file[1])) 
  608. { 
  609. m_TotCount = File.ReadLines(files[s]).Count(); 
  610. //---------------- 
  611. fileaction = "SPLIT"; 
  612. string now = DateTime.Now.ToString("dd/MM/yyyy"); 
  613. m_QryStr = "insert into File_Details(Filename ,FileMaxlimit,FileAction,Date,TXNTYPE,ProcessType) values ('" + SFilename + "','" + m_TotCount + "','" + fileaction + "',convert(datetime,'" + now + "',103),'" + file[6] + "','" + file[0] + file[1] + "')"; 
  614. DbObj.gl_ExeNonQuery(m_QryStr); 
  615. //------------ 
  616. string format = fileformat.Replace("-", ""); 
  617. m_QryStr = "Select RECORDS from FILE_MASTER where CATEGORY='" + format + "'"; 
  618. DR = DbObj.gl_ExeReader(m_QryStr); 
  619. if (DR.Read()) 
  620. { 
  621. recordcount = Convert.ToInt32(DR["RECORDS"].ToString()); 
  622. } 
  623. DR.Close(); 
  624. if (m_TotCount > recordcount) 
  625. { 
  626. foreach (string item2 in file) 
  627. { 
  628. item = item2; 
  629. if (item == "INW") 
  630. { 
  631. Split(Destination); 
  632. } 
  633. if (item == "RTN") 
  634. { 
  635. Split(Destination); 
  636. } 
  637. if (item == "RES") 
  638. { 
  639. Split(Destination); 
  640. } 
  641. } 
  642. } 
  643. else 
  644. { 
  645. DbObj.log_write("Message : " + " Spliting not needed"); 
  646. if (((file[0] + file[1]).ToUpper() == "APBCR" || (file[0] + file[1]).ToUpper() == "ACHCR") && (file[6].ToUpper() == "INW" || file[6].ToUpper() == "RTN")) 
  647. { 
  648. string file_name = "";// Path.GetFileName(M_FLNM); 
  649. string Today_Date = DateTime.Now.ToString("dd/MM/yyyy"); 
  650. string fileid = ""; 
  651. m_QryStr = " select FileID from File_Details where Filename='" + SFilename + "'"; 
  652. DR = DbObj.gl_ExeReader(m_QryStr); 
  653. if (DR.Read()) 
  654. { 
  655. fileid = (DR["FileID"].ToString()); 
  656. } 
  657. DR.Close(); 
  658. m_QryStr = "select * from file_master where CATEGORY='" + file[0] + file[1] + "' AND TXNTYPE='" + file[6] + "'"; 
  659. DataSet DS = DbObj.gl_ExeDataSet(m_QryStr); 
  660. if (DS.Tables[0].Rows.Count > 0) 
  661. { 
  662. long Batch = 0; 
  663. m_QryStr = "select count(FILE_MST_ID) from SplitFile_Details where FILE_MST_ID='" + DS.Tables[0].Rows[0]["FILE_MST_ID"].ToString() + "' and Convert (varchar(10),[date],103) ='" + DateTime.Now.ToString("dd/MM/yyyy") + "'"; 
  664. Batch = ((DbObj.gl_exeScalar(m_QryStr)).ToString() == "0") ? 0 : long.Parse(DbObj.gl_exeScalar(m_QryStr).ToString()); 
  665. //DataSet DS = DbObj.gl_ExeDataSet(m_QryStr); 
  666. file_name = DS.Tables[0].Rows[0]["CBS_PFX_NAME"] + file[4] + Batch.ToString().PadLeft(6, '0') + DS.Tables[0].Rows[0]["CBS_SFX_NAME"] + Path.GetExtension(files[s]); 
  667. m_QryStr = "insert into SplitFile_Details (FileID,FileName,Splitlimit,Date) values('" + fileid.ToString() + "','" + file_name + "','" + m_TotCount + "',convert(datetime,'" + Today_Date + "',103))"; 
  668. DbObj.gl_ExeNonQuery(m_QryStr); 
  669. File.Move(files[s], Destination + "\\" + file_name); 
  670. File.Delete(files[s]); 
  671. } 
  672. } 
  673. else 
  674. { 
  675. string file_name = "";// Path.GetFileName(M_FLNM); 
  676. string Today_Date = DateTime.Now.ToString("dd/MM/yyyy"); 
  677. string fileid = ""; 
  678. m_QryStr = " select FileID from File_Details where Filename='" + SFilename + "'"; 
  679. DR = DbObj.gl_ExeReader(m_QryStr); 
  680. if (DR.Read()) 
  681. { 
  682. fileid = (DR["FileID"].ToString()); 
  683. } 
  684. DR.Close(); 
  685. m_QryStr = "select * from file_master where CATEGORY='" + file[0] + file[1] + "' AND TXNTYPE='" + file[6] + "'"; 
  686. DataSet DS = DbObj.gl_ExeDataSet(m_QryStr); 
  687. if (DS.Tables[0].Rows.Count > 0) 
  688. { 
  689. long Batch = 0; 
  690. m_QryStr = "select count(FILE_MST_ID) from SplitFile_Details where FILE_MST_ID='" + DS.Tables[0].Rows[0]["FILE_MST_ID"].ToString() + "' "; 
  691. Batch = ((DbObj.gl_exeScalar(m_QryStr)).ToString() == "0") ? 1 : long.Parse(DbObj.gl_exeScalar(m_QryStr).ToString() + 1); 
  692. file_name = file[0] + "-" + file[1] + "-" + file[2] + "-" + file[3] + "-" + file[4] + "-" + Batch.ToString().PadLeft(6, '0') + "-" + file[6] + Path.GetExtension(files[s]); 
  693. m_QryStr = "insert into SplitFile_Details (FileID,FileName,Splitlimit,Date) values('" + fileid.ToString() + "','" + file_name + "','" + m_TotCount + "',convert(datetime,'" + Today_Date + "',103))"; 
  694. DbObj.gl_ExeNonQuery(m_QryStr); 
  695. File.Move(files[s], Destination + "\\" + file_name); 
  696. File.Delete(files[s]); 
  697. } 
  698. } 
  699. } 
  700. } 
  701. }*/  
  702.                                     #endregion  
  703.                                 }  
  704.                             }  
  705.                             #region .CSV File Split  
  706.                             if (fileextention == ".CSV")  
  707.                             {  
  708.                                 SFilename = Path.GetFileName(files[s]);  
  709.                                 DbObj.log_write("Split CSV File Process : " + files[s]);  
  710.                                 words = SFilename.Split('.');  
  711.                                 file = words[0].Split('-');  
  712.                                 //batchno = Convert.ToInt32(file[5].ToString());  
  713.                                 file_1st = file[0] + "-" + file[1] + "-" + file[2] + "-" + file[3] + "-" + file[4];  
  714.                                 fileformat = SFilename.Substring(0, 6);  
  715.                                 if (fileformat == (file[0] + "-" + file[1]))  
  716.                                 {  
  717.                                     m_TotCount = File.ReadLines(files[s]).Count();  
  718.                                     //-------------------------  
  719.                                     string fileaction = "Split";  
  720.                                     string now = DateTime.Now.ToString("dd/MM/yyyy");  
  721.                                     m_QryStr = "insert into File_Details(Filename ,FileMaxlimit,FileAction,Date,TXNTYPE,ProcessType) values ('" + SFilename + "','" + m_TotCount + "','" + fileaction + "',convert(datetime,'" + now + "',103),'" + file[6] + "','" + file[0] + file[1] + "')";  
  722.                                     DbObj.gl_ExeNonQuery(m_QryStr);  
  723.                                     //-------------------------  
  724.                                     string format = fileformat.Replace("-""");  
  725.                                     m_QryStr = "Select RECORDS from FILE_MASTER where CATEGORY='" + format + "'";  
  726.                                     DR = DbObj.gl_ExeReader(m_QryStr);  
  727.                                     if (DR.Read())  
  728.                                     {  
  729.                                         recordcount = Convert.ToInt32(DR["RECORDS"].ToString());  
  730.                                     }  
  731.                                     DR.Close();  
  732.                                     if (m_TotCount > recordcount)  
  733.                                     {  
  734.                                         foreach (string item in file)  
  735.                                         {  
  736.                                             if (item == "INW")  
  737.                                             {  
  738.                                                 Split(Destination);  
  739.                                             }  
  740.                                             if (item == "RTN")  
  741.                                             {  
  742.                                                 Split(Destination);  
  743.                                             }  
  744.                                             if (item == "RES")  
  745.                                             {  
  746.                                                 Split(Destination);  
  747.                                             }  
  748.                                         }  
  749.                                     }  
  750.                                     else  
  751.                                     {  
  752.                                         DbObj.log_write("Message : " + " Spliting not needed");  
  753.                                         string file_name = "";// Path.GetFileName(M_FLNM);  
  754.                                         string Today_Date = DateTime.Now.ToString("dd/MM/yyyy");  
  755.                                         string fileid = "";  
  756.                                         m_QryStr = " select FileID from File_Details where Filename='" + SFilename + "'";  
  757.                                         DR = DbObj.gl_ExeReader(m_QryStr);  
  758.                                         if (DR.Read())  
  759.                                         {  
  760.                                             fileid = (DR["FileID"].ToString());  
  761.                                         }  
  762.                                         DR.Close();  
  763.                                         m_QryStr = "select * from file_master where CATEGORY='" + file[0] + file[1] + "' AND TXNTYPE='" + file[6] + "'";  
  764.                                         DataSet DS = DbObj.gl_ExeDataSet(m_QryStr);  
  765.                                         if (DS.Tables[0].Rows.Count > 0)  
  766.                                         {  
  767.                                             long Batch = 0;  
  768.                                             m_QryStr = "select count(FILE_MST_ID) from SplitFile_Details where FILE_MST_ID='" + DS.Tables[0].Rows[0]["FILE_MST_ID"].ToString() + "' and Convert (varchar(10),[date],103) ='" + DateTime.Now.ToString("dd/MM/yyyy") + "'";  
  769.                                             Batch = ((DbObj.gl_exeScalar(m_QryStr)).ToString() == "0") ? 1 : long.Parse(DbObj.gl_exeScalar(m_QryStr).ToString()) + 1;  
  770.                                             file_name = file[0] + "-" + file[1] + "-" + file[2] + "-" + file[3] + "-" + file[4] + "-" + Batch.ToString().PadLeft(6, '0') + "-" + file[6] + Path.GetExtension(files[s]);  
  771.                                             m_QryStr = "insert into SplitFile_Details (FileID,FileName,Splitlimit,Date,FILE_MST_ID) values('" + fileid.ToString() + "','" + file_name + "','" + m_TotCount + "',convert(datetime,'" + Today_Date + "',103),'" + DS.Tables[0].Rows[0]["FILE_MST_ID"].ToString() + "')";  
  772.                                             DbObj.gl_ExeNonQuery(m_QryStr);  
  773.                                             File.Move(files[s], Destination + "\\" + file_name);  
  774.                                             File.Delete(files[s]);  
  775.                                         }  
  776.                                     }  
  777.                                 }  
  778.                             }  
  779.                             #endregion  
  780.                         }  
  781.                     }  
  782.                     else  
  783.                     {  
  784.                         //MessageBox.Show("Files are not Found...");  
  785.                     }  
  786.                 }  
  787.                 else  
  788.                 {  
  789.                     // MessageBox.Show("Folder not Found");  
  790.                 }  
  791.                 string rootFolderPath = @path1 + @"\";  
  792.                 string destinationPath = original + "\\";  
  793.                 foreach (string file in files)  
  794.                 {  
  795.                     string filename = Path.GetFileName(file);  
  796.                     string fileToMove = rootFolderPath + filename;  
  797.                     string moveTo = destinationPath + filename;  
  798.                     //moving file  
  799.                     File.Move(fileToMove, moveTo);  
  800.                 }  
  801.             }  
  802.             catch (Exception ex)  
  803.             {  
  804.                 DbObj.log_write("Split Text File Exception : " + ex.Message);  
  805.             }  
  806.         }  
  807.         void MMerge()  
  808.         {  
  809.             #region INBOX (INW)  
  810.             m_QryStr = "truncate table temp_header_data";  
  811.             DbObj.gl_ExeNonQuery(m_QryStr);  
  812.             m_QryStr = "truncate table temp_details_data";  
  813.             DbObj.gl_ExeNonQuery(m_QryStr);  
  814.             string format = Mfileformat.Replace("-""");  
  815.             recordcount = 0;  
  816.             m_QryStr = "Select RECORDS,ROW_SIZE from FIRST_MASTER where FILE_ACTION='MERGE' AND CATEGORY='" + format + "'";  
  817.             DR = DbObj.gl_ExeReader(m_QryStr);  
  818.             if (DR.Read())  
  819.             {  
  820.                 recordcount = Convert.ToInt32(DR["RECORDS"].ToString());  
  821.                 rowsize = Convert.ToInt32(DR["ROW_SIZE"].ToString());  
  822.             }  
  823.             DR.Close();  
  824.             m_QryStr = "Select VF_HD_FROM,VF_HD_TO from SECOND_MASTER where CATEGORY='" + format + "' and ACTION='COUNT'";  
  825.             DR = DbObj.gl_ExeReader(m_QryStr);  
  826.             if (DR.Read())  
  827.             {  
  828.                 countVFHDfrom = Convert.ToInt32(DR["VF_HD_FROM"].ToString());  
  829.                 countVFHDto = Convert.ToInt32(DR["VF_HD_TO"].ToString());  
  830.                 diff_hd_count = (countVFHDto - countVFHDfrom);  
  831.             }  
  832.             DR.Close();  
  833.             m_QryStr = "select VF_HD_FROM,VF_HD_TO,VF_DT_FROM,VF_DT_TO from SECOND_MASTER where CATEGORY='" + format + "' and ACTION='SUM'";  
  834.             DR = DbObj.gl_ExeReader(m_QryStr);  
  835.             if (DR.Read())  
  836.             {  
  837.                 sumVFHDfrom = Convert.ToInt32(DR["VF_HD_FROM"].ToString());  
  838.                 sumVFHDto = Convert.ToInt32(DR["VF_HD_TO"].ToString());  
  839.                 diff_hd_sum = (sumVFHDto - sumVFHDfrom);  
  840.                 sumVFDTfrom = Convert.ToInt32(DR["VF_DT_FROM"].ToString());  
  841.                 sumVFDTto = Convert.ToInt32(DR["VF_DT_TO"].ToString());  
  842.                 diff_dt_sum = (sumVFDTto - sumVFDTfrom);  
  843.             }  
  844.             DR.Close();  
  845.             int i, j = 0;  
  846.             for (i = 0; i < MergeFiles.Count; i++)  
  847.             {  
  848.                 int m_TotCount = File.ReadLines(MergeFiles[i]).Count();  
  849.                 for (j = 0; j < m_TotCount; j++)  
  850.                 {  
  851.                     if ((m_Read = File.ReadLines(MergeFiles[i].ToString()).ElementAt(j).ToString()) != "")  
  852.                     {  
  853.                         if (j == 0)  
  854.                         {  
  855.                             hd_data = m_Read.Substring(0);  
  856.                             hd_count_field = Convert.ToInt32(m_Read.Substring(countVFHDfrom, diff_hd_count));  
  857.                             hd_amt_field = Convert.ToInt32(m_Read.Substring(sumVFHDfrom, diff_hd_sum));  
  858.                             m_QryStr = "INSERT INTO temp_header_data (hd_data,var_fld_1,var_fld_2)" +  
  859.                             " VALUES ('" + hd_data + "','" + hd_count_field + "','" + hd_amt_field + "' )";  
  860.                             DbObj.gl_ExeNonQuery(m_QryStr);  
  861.                         }  
  862.                         if (j > 0)  
  863.                         {  
  864.                             dt_data = m_Read.Substring(0);  
  865.                             dt_amt_field = Convert.ToInt32(m_Read.Substring(sumVFDTfrom, diff_dt_sum));  
  866.                             m_QryStr = "INSERT INTO temp_details_data (dt_data,var_fld_1)" +  
  867.                             " VALUES ('" + dt_data + "','" + dt_amt_field + "')";  
  868.                             DbObj.gl_ExeNonQuery(m_QryStr);  
  869.                         }  
  870.                     }  
  871.                 }  
  872.             }  
  873.             //MessageBox.Show("uploading done");  
  874.             # region "MERGE"  
  875.             m_QryStr = "select COUNT(*) from temp_details_data";  
  876.             DataSet dsdatacount = DbObj.gl_ExeDataSet(m_QryStr);  
  877.             int datacount = Convert.ToInt32(dsdatacount.Tables[0].Rows[0][0].ToString());  
  878.             //int m_TotalCount = File.ReadLines(txtupload.Text).Count();  
  879.             loop_count = datacount / recordcount;  
  880.             if (datacount % recordcount != 0)  
  881.             {  
  882.                 loop_count = loop_count + 1;  
  883.             }  
  884.             //batchno = 1;  
  885.             for (int c = 0; c < loop_count; c++)  
  886.             {  
  887.                 string data = DateTime.Now.ToString("dd/MM/yyyy");  
  888.                 string[] datetext = data.Split(' ');  
  889.                 string date = datetext[0];  
  890.                 m_QryStr = "select (MAX(Batchno)) as BatchNo from maxbatchbackup where convert(varchar,UploadDate,103)='" + date + "' and CATEGORY='" + Mfile[0] + Mfile[1] + "'";  
  891.                 DR = DbObj.gl_ExeReader(m_QryStr); //Max_Batch  
  892.                 if (DR.Read())  
  893.                 {  
  894.                     if (DR["BatchNo"].ToString() == "")  
  895.                     {  
  896.                         batchno = 1;  
  897.                     }  
  898.                     else  
  899.                     {  
  900.                         maxbatchno = Convert.ToInt32(DR["BatchNo"].ToString());  
  901.                         batchno = maxbatchno + 1;  
  902.                         // MessageBox.Show(":-)");  
  903.                     }  
  904.                 }  
  905.                 DR.Close();  
  906.                 string file_batchno = "";  
  907.                 file_batchno = batchno.ToString().PadLeft(6, '0');  
  908.                 m_QryStr = "select * from temp_header_data";  
  909.                 DataSet dshddetails = DbObj.gl_ExeDataSet(m_QryStr);  
  910.                 if (dshddetails.Tables[0].Rows.Count > 0)  
  911.                 {  
  912.                     head_data = dshddetails.Tables[0].Rows[0]["hd_data"].ToString();  
  913.                 }  
  914.                 //---------------header data + total count--------------------  
  915.                 m_QryStr = "select top " + recordcount + " * from temp_details_data";  
  916.                 DataSet dsdtrowdetails = DbObj.gl_ExeDataSet(m_QryStr);  
  917.                 count = dsdtrowdetails.Tables[0].Rows.Count;  
  918.                 //-----------------total sum-----------------------------------  
  919.                 m_QryStr = "select SUM(var_fld_1) from temp_details_data where dt_id in (select top " + recordcount + " dt_id from temp_details_data)";  
  920.                 DataSet dsvarfld1 = DbObj.gl_ExeDataSet(m_QryStr);  
  921.                 if (dsvarfld1.Tables[0].Rows.Count > 0)  
  922.                 {  
  923.                     varfld1 = Convert.ToInt32(dsvarfld1.Tables[0].Rows[0][0].ToString());  
  924.                 }  
  925.                 //M_FLNM = "C:\\Users\\vijeth\\Desktop\\APB-CR-" + "-" + DateTime.Now.ToString("ddMMyyyy") + "-BatchNo-" + batchno + "-RTN.txt";  
  926.                 M_FLNM = @"C:\Users\ACSPL\Desktop\Final H2H Project\H2H Testing Apllication\H2H Testing Apllication\bin\Debug\TextSplitMerge\MergeOutput\" + Mfile_1st + "-" + file_batchno + "-" + Mfile[5] + "." + Mwords[1] + "";  
  927.                 if (!File.Exists(M_FLNM))  
  928.                 {  
  929.                     STRWRT = new StreamWriter(M_FLNM);  
  930.                 }  
  931.                 else  
  932.                 {  
  933.                     File.Delete(M_FLNM);  
  934.                     STRWRT = new StreamWriter(M_FLNM);  
  935.                 }  
  936.                 //-----------------------------  
  937.                 string fn = Path.GetFileName(M_FLNM);  
  938.                 string now = DateTime.Now.ToString("dd/MM/yyyy");  
  939.                 //string fileid = "";  
  940.                 //m_QryStr = " select FileID from File_Details where Filename='" + MFilename + "'";  
  941.                 //DR = DbObj.gl_ExeReader(m_QryStr);  
  942.                 //if (DR.Read())  
  943.                 //{  
  944.                 // fileid = (DR["FileID"].ToString());  
  945.                 //}  
  946.                 //DR.Close();  
  947.                 m_QryStr = "insert into MergeFile_Details (FileName,Mergelimit,Date) values(' " + fn + "','" + count + "','" + now + "')";  
  948.                 DbObj.gl_ExeNonQuery(m_QryStr);  
  949.                 //-----------------------------  
  950.                 string hd_var_fld_1 = count.ToString().PadLeft(diff_hd_count, '0');  
  951.                 string hd_var_fld_2 = varfld1.ToString().PadLeft(diff_hd_sum, '0');  
  952.                 var aStringBuilder = new StringBuilder(head_data);  
  953.                 aStringBuilder.Remove(countVFHDfrom, diff_hd_count);  
  954.                 aStringBuilder.Insert(countVFHDfrom, hd_var_fld_1);  
  955.                 head_data = aStringBuilder.ToString();  
  956.                 aStringBuilder.Remove(sumVFHDfrom, diff_hd_sum);  
  957.                 aStringBuilder.Insert(sumVFHDfrom, hd_var_fld_2);  
  958.                 head_data = aStringBuilder.ToString();  
  959.                 STRWRT.WriteLine(string.Format("{0,-" + rowsize + "}", head_data));  
  960.                 for (int x = 0; x < dsdtrowdetails.Tables[0].Rows.Count; x++)  
  961.                 {  
  962.                     row_deatils = dsdtrowdetails.Tables[0].Rows[x]["dt_data"].ToString();  
  963.                     STRWRT.WriteLine(string.Format("{0,-" + rowsize + "}", row_deatils));  
  964.                 }  
  965.                 STRWRT.Close();  
  966.                 m_QryStr = "delete top (" + recordcount + ") from temp_details_data";  
  967.                 DbObj.gl_ExeNonQuery(m_QryStr);  
  968.                 m_QryStr = "insert into max_batch (Batchno,UploadDate,CATEGORY) values(" + batchno + ",convert(datetime,'" + date + "',103),'" + Mfile[0] + Mfile[1] + "')";  
  969.                 DbObj.gl_ExeNonQuery(m_QryStr);  
  970.                 //MessageBox.Show("File Created");  
  971.                 batchno++;  
  972.             }  
  973.             #endregion  
  974.             #endregion  
  975.         }  
  976.         public void FILEMERGE(string path2)  
  977.         {  
  978.             if (Directory.Exists(path2))  
  979.             {  
  980.                 Mfiles = Directory.GetFiles(path2);  
  981.                 List<string> filetypes = new List<string>();  
  982.                 filetypes.Clear();  
  983.                 foreach (string str in Mfiles)  
  984.                 {  
  985.                     string[] fullname = Path.GetFileName(str).Split('.');  
  986.                     string[] type = fullname[0].Split('-');  
  987.                     if (!filetypes.Contains(type[0] + "-" + type[1]))  
  988.                     {  
  989.                         filetypes.Add(type[0] + "-" + type[1]);  
  990.                     }  
  991.                 }  
  992.                 if (Mfiles.Length > 0)  
  993.                 {  
  994.                     for (int n = 0; n < filetypes.Count; n++)  
  995.                     {  
  996.                         for (m = 0; m < Mfiles.Length; m++)  
  997.                         {  
  998.                             if (Path.GetFileName(Mfiles[m]).StartsWith(filetypes[n]))  
  999.                             {  
  1000.                                 //var filename = new FileInfo(Mfiles[m]);  
  1001.                                 string fileextention = Path.GetExtension(Mfiles[m].ToUpper());  
  1002.                                 // ---------------text file code--------------------------------------  
  1003.                                 if (fileextention == ".TXT")  
  1004.                                 {  
  1005.                                     MFilename = Path.GetFileName(Mfiles[m]);  
  1006.                                     Mwords = MFilename.Split('.');  
  1007.                                     Mfile = Mwords[0].Split('-');  
  1008.                                     //batchno = Convert.ToInt32(file[5].ToString());  
  1009.                                     Mfile_1st = Mfile[0] + "-" + Mfile[1] + "-" + Mfile[2] + "-" + Mfile[3];// +"-" + Mfile[4];  
  1010.                                     Mfileformat = MFilename.Substring(0, 6);  
  1011.                                     if (Mfileformat == (Mfile[0] + "-" + Mfile[1]))  
  1012.                                     {  
  1013.                                         //if (Mfileformat == "ACH-CR")  
  1014.                                         //{  
  1015.                                         Mm_TotCount = File.ReadLines(Mfiles[m]).Count();  
  1016.                                         //---------------------  
  1017.                                         fileaction = "Merge";  
  1018.                                         string now = DateTime.Now.ToString("dd/MM/yyyy");  
  1019.                                         m_QryStr = "insert into File_Details(Filename ,FileMaxlimit,FileAction,Date) values ('" + MFilename + "','" + Mm_TotCount + "','" + fileaction + "','" + now + "')";  
  1020.                                         DbObj.gl_ExeNonQuery(m_QryStr);  
  1021.                                         //---------------------  
  1022.                                         string format = Mfileformat.Replace("-""");  
  1023.                                         recordcount = 0;  
  1024.                                         m_QryStr = "Select RECORDS from FIRST_MASTER where FILE_ACTION='MERGE' AND CATEGORY='" + format + "'";  
  1025.                                         DR = DbObj.gl_ExeReader(m_QryStr);  
  1026.                                         if (DR.Read())  
  1027.                                         {  
  1028.                                             recordcount = Convert.ToInt32(DR["RECORDS"].ToString());  
  1029.                                         }  
  1030.                                         DR.Close();  
  1031.                                         if (Mm_TotCount < recordcount)  
  1032.                                         {  
  1033.                                             MergeFiles.Add(Mfiles[m].ToString());  
  1034.                                         }  
  1035.                                         else  
  1036.                                         {  
  1037.                                             MessageBox.Show("File Merging not Needed");  
  1038.                                         }  
  1039.                                         //}  
  1040.                                         # region APB  
  1041.                                         //if (Mfileformat == "APB-CR")  
  1042.                                         //{  
  1043.                                         // Mm_TotCount = File.ReadLines(Mfiles[m]).Count();  
  1044.                                         // if (Mm_TotCount < 22)  
  1045.                                         // {  
  1046.                                         // for (int t = 0; t < Mfiles.Length; t++)  
  1047.                                         // {  
  1048.                                         // string arrayfilename = Path.GetFileName(Mfiles[t]);  
  1049.                                         // string[] arraywords = arrayfilename.Split('.');  
  1050.                                         // string[] arrayfile = arraywords[0].Split('-');  
  1051.                                         // if (Mfileformat == arrayfile[0] + arrayfile[1] + arrayfile[5].Trim())  
  1052.                                         // {  
  1053.                                         // //int rec_count = File.ReadLines(files[t]).Count();  
  1054.                                         // //totalrec = totalrec + rec_count;  
  1055.                                         // //if (totalrec < recordcount)  
  1056.                                         // //{  
  1057.                                         // ABPMergeFiles.Add(Mfiles[t].ToString());  
  1058.                                         // //MergeFiles.Add(Mfiles[t].ToString());  
  1059.                                         // //}  
  1060.                                         // }  
  1061.                                         // }  
  1062.                                         // //call here the merge function  
  1063.                                         // // MMerge();  
  1064.                                         // }  
  1065.                                         // else  
  1066.                                         // {  
  1067.                                         // MessageBox.Show("File Merging not Needed");  
  1068.                                         // }  
  1069.                                         //}  
  1070.                                         #endregion  
  1071.                                     }  
  1072.                                 }  
  1073.                                 //-----------------------end text file code-------------------------------------------  
  1074.                                 if (fileextention == ".CSV")  
  1075.                                 {  
  1076.                                     MFilename = Path.GetFileName(Mfiles[m]);  
  1077.                                     Mwords = MFilename.Split('.');  
  1078.                                     Mfile = Mwords[0].Split('-');  
  1079.                                     //batchno = Convert.ToInt32(file[5].ToString());  
  1080.                                     Mfile_1st = Mfile[0] + "-" + Mfile[1] + "-" + Mfile[2] + "-" + Mfile[3];// +"-" + Mfile[4];  
  1081.                                     Mfileformat = MFilename.Substring(0, 6);  
  1082.                                     if (Mfileformat == (Mfile[0] + "-" + Mfile[1]))  
  1083.                                     {  
  1084.                                         Mm_TotCount = File.ReadLines(Mfiles[m]).Count();  
  1085.                                         //--------------------  
  1086.                                         fileaction = "Merge";  
  1087.                                         String now = DateTime.Now.ToString("dd/MM/yyyy");  
  1088.                                         m_QryStr = "insert into File_Details(Filename ,FileMaxlimit,FileAction,Date) values ('" + MFilename + "','" + Mm_TotCount + "','" + fileaction + "','" + now + "')";  
  1089.                                         DbObj.gl_ExeNonQuery(m_QryStr);  
  1090.                                         //--------------------  
  1091.                                         string format = Mfileformat.Replace("-""");  
  1092.                                         recordcount = 0;  
  1093.                                         m_QryStr = "Select RECORDS from FIRST_MASTER where FILE_ACTION='MERGE' AND CATEGORY='" + format + "'";  
  1094.                                         DR = DbObj.gl_ExeReader(m_QryStr);  
  1095.                                         if (DR.Read())  
  1096.                                         {  
  1097.                                             recordcount = Convert.ToInt32(DR["RECORDS"].ToString());  
  1098.                                         }  
  1099.                                         DR.Close();  
  1100.                                         if (Mm_TotCount < recordcount)  
  1101.                                         {  
  1102.                                             MergeFiles.Add(Mfiles[m].ToString());  
  1103.                                         }  
  1104.                                         else  
  1105.                                         {  
  1106.                                             MessageBox.Show("File Merging not Needed");  
  1107.                                         }  
  1108.                                         //}  
  1109.                                         # region APB  
  1110.                                         //if (Mfileformat == "APB-CR")  
  1111.                                         //{  
  1112.                                         // Mm_TotCount = File.ReadLines(Mfiles[m]).Count();  
  1113.                                         // if (Mm_TotCount < 22)  
  1114.                                         // {  
  1115.                                         // for (int t = 0; t < Mfiles.Length; t++)  
  1116.                                         // {  
  1117.                                         // string arrayfilename = Path.GetFileName(Mfiles[t]);  
  1118.                                         // string[] arraywords = arrayfilename.Split('.');  
  1119.                                         // string[] arrayfile = arraywords[0].Split('-');  
  1120.                                         // if (Mfileformat == arrayfile[0] + arrayfile[1] + arrayfile[5].Trim())  
  1121.                                         // {  
  1122.                                         // //int rec_count = File.ReadLines(files[t]).Count();  
  1123.                                         // //totalrec = totalrec + rec_count;  
  1124.                                         // //if (totalrec < recordcount)  
  1125.                                         // //{  
  1126.                                         // ABPMergeFiles.Add(Mfiles[t].ToString());  
  1127.                                         // //MergeFiles.Add(Mfiles[t].ToString());  
  1128.                                         // //}  
  1129.                                         // }  
  1130.                                         // }  
  1131.                                         // //call here the merge function  
  1132.                                         // // MMerge();  
  1133.                                         // }  
  1134.                                         // else  
  1135.                                         // {  
  1136.                                         // MessageBox.Show("File Merging not Needed");  
  1137.                                         // }  
  1138.                                         //}  
  1139.                                         #endregion  
  1140.                                     }  
  1141.                                 }  
  1142.                             }  
  1143.                         }  
  1144.                         MMerge();  
  1145.                         string rootFolderPath = @path2 + @"\";  
  1146.                         string destinationPath = @"C:\Users\ACSPL\Desktop\Final H2H Project\H2H Testing Apllication\H2H Testing Apllication\bin\Debug\TextSplitMerge\MergeFileBackup\";  
  1147.                         foreach (string file in MergeFiles)  
  1148.                         {  
  1149.                             string filename = Path.GetFileName(file);  
  1150.                             string fileToMove = rootFolderPath + filename;  
  1151.                             string moveTo = destinationPath + filename;  
  1152.                             //moving file  
  1153.                             File.Move(fileToMove, moveTo);  
  1154.                         }  
  1155.                         MergeFiles.Clear();  
  1156.                     } // second for loop end  
  1157.                 }  
  1158.             }  
  1159.             else  
  1160.             {  
  1161.                 MessageBox.Show("Folder Not Found...");  
  1162.             }  
  1163.         }  
  1164.     }  
  1165. }