TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
How to Split File in C# ASP.NET
Pintoo Yadav
Mar 18
2015
Code
6.5
k
0
1
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Linq;
using
System.Text;
using
System.Windows.Forms;
using
System.Data.SqlClient;
using
System.IO;
using
System.Configuration;
using
NACH_Application;
using
System.Data.Odbc;
namespace
H2H
{
public
class
H2HSplitMergeFiles
{
string
m_QryStr, m_Read, M_FLNM, fileaction, SFilename, MFilename;
SqlDataReader DR;
StreamWriter STRWRT;
StreamReader sr;
int
recordcount, rowsize, hd_count_field, hd_amt_field, dt_amt_field, countVFHDfrom, countVFHDto, sumVFHDfrom, sumVFHDto, sumVFDTfrom, sumVFDTto, refVFHDfrom, refVFHDto, refVFDTfrom, refVFDTto;
int
diff_hd_sum, diff_hd_count, diff_dt_sum, diff_hd_ref, diff_dt_ref, loop_count;
string
hd_data, dt_data =
""
;
int
varfld1, count;
int
batchno, maxbatchno;
int
m_TotCount;
string
[] file;
string
fileformat, file_1st, Mfile_1st;
string
[] words;
string
[] Mwords;
string
[] Mfile;
string
[] Mfiles;
List<
string
> files =
new
List<
string
>();
//string[] files;
int
Mm_TotCount;
string
item;
int
s, m;
string
Mfileformat;
List<
string
> MergeFiles =
new
List<
string
>();
string
head_data, row_deatils =
""
;
int
line;
int
index = 0;
string
TXNTYPE =
""
;
string
Destination_Folder;
Class1 DbObj =
new
Class1();
void
Split(
string
Destination)
{
if
(
true
)
{
#region Getting Split for ACH and APB UCO
#region Getting Split Details from Database
DbObj.log_write(
"Clear Temp Table Data......"
);
m_QryStr =
"truncate table temp_header_data"
;
DbObj.gl_ExeNonQuery(m_QryStr);
m_QryStr =
"truncate table temp_details_data"
;
DbObj.gl_ExeNonQuery(m_QryStr);
string
format = fileformat.Replace(
"-"
,
""
);
recordcount = 0;
string
File_MST_ID =
""
;
m_QryStr =
"Select RECORDS,ROW_SIZE,FILE_MST_ID from FILE_MASTER where CATEGORY='"
+ format +
"' and [TXNTYPE]='"
+ TXNTYPE +
"' and FILE_ACTION='SPLIT'"
;
DR = DbObj.gl_ExeReader(m_QryStr);
DbObj.log_write(
"\n Query :"
+ m_QryStr);
if
(DR.Read())
{
recordcount = Convert.ToInt32(DR[
"RECORDS"
].ToString());
rowsize = Convert.ToInt32(DR[
"ROW_SIZE"
].ToString());
File_MST_ID = DR[
"FILE_MST_ID"
].ToString();
}
DR.Close();
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 +
"' "
;
DR = DbObj.gl_ExeReader(m_QryStr);
DbObj.log_write(
"\n Query :"
+ m_QryStr);
if
(DR.Read())
{
countVFHDfrom = Convert.ToInt32(DR[
"VF_HD_FROM"
].ToString());
countVFHDto = Convert.ToInt32(DR[
"VF_HD_TO"
].ToString());
diff_hd_count = (countVFHDto - countVFHDfrom);
}
DR.Close();
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 +
"'"
;
DR = DbObj.gl_ExeReader(m_QryStr);
DbObj.log_write(
"\nQuery :"
+ m_QryStr);
if
(DR.Read())
{
sumVFHDfrom = Convert.ToInt32(DR[
"VF_HD_FROM"
].ToString());
sumVFHDto = Convert.ToInt32(DR[
"VF_HD_TO"
].ToString());
diff_hd_sum = (sumVFHDto - sumVFHDfrom);
sumVFDTfrom = Convert.ToInt32(DR[
"VF_DT_FROM"
].ToString());
sumVFDTto = Convert.ToInt32(DR[
"VF_DT_TO"
].ToString());
diff_dt_sum = (sumVFDTto - sumVFDTfrom);
}
DR.Close();
//--------------------------------refrence---------------------------------------------------------
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'"
;
DR = DbObj.gl_ExeReader(m_QryStr);
DbObj.log_write(
"\n Query :"
+ m_QryStr);
if
(DR.Read())
{
refVFHDfrom = Convert.ToInt32(DR[
"VF_HD_FROM"
].ToString());
refVFHDto = Convert.ToInt32(DR[
"VF_HD_TO"
].ToString());
diff_hd_ref = (refVFHDto - refVFHDfrom);
refVFDTfrom = Convert.ToInt32(DR[
"VF_DT_FROM"
].ToString());
refVFDTto = Convert.ToInt32(DR[
"VF_DT_TO"
].ToString());
diff_dt_ref = (refVFDTto - refVFDTfrom);
}
DR.Close();
//--------------------------------refrence---------------------------------------------------------
if
(recordcount < m_TotCount)
{
int
i = 0;
for
(i = 0; i < m_TotCount; i++)
{
if
((m_Read = File.ReadLines(files[s]).ElementAt(i).ToString()) !=
""
)
{
if
(i == 0)
{
hd_data = m_Read.Substring(0);
hd_count_field = Convert.ToInt32(m_Read.Substring(countVFHDfrom, diff_hd_count));
hd_amt_field = Convert.ToInt32(((diff_hd_sum != 0) ? m_Read.Substring(sumVFHDfrom, diff_hd_sum) : 0 +
""
));
m_QryStr =
"INSERT INTO temp_header_data (hd_data,var_fld_1,var_fld_2)"
+
" VALUES ('"
+ hd_data +
"','"
+ hd_count_field +
"','"
+ hd_amt_field +
"' )"
;
//" TO_DATE('" + M_STLMNTDT + "','DD/MM/YYYY'),'" + M_DESTIFSC + "','" + M_CYCLE
DbObj.gl_ExeNonQuery(m_QryStr);
DbObj.log_write(
"\n Insert Header into temp table Query :"
+ m_QryStr);
}
if
(i > 0)
{
dt_data = m_Read.Substring(0);
dt_amt_field = Convert.ToInt32(((diff_dt_sum != 0) ? m_Read.Substring(sumVFDTfrom, diff_dt_sum) : 0 +
""
));
m_QryStr =
"INSERT INTO temp_details_data (dt_data,var_fld_1)"
+
" VALUES ('"
+ dt_data +
"','"
+ dt_amt_field +
"')"
;
DbObj.gl_ExeNonQuery(m_QryStr);
// DbObj.log_write("\n Insert Recoard into temp table Query :" + m_QryStr);
}
}
}
DbObj.log_write(i +
""
);
}
#endregion
# region "SPLIT"
loop_count = (m_TotCount - 1) / recordcount;
if
((m_TotCount - 1) % recordcount != 0)
{
loop_count = loop_count + 1;
}
//batchno = 1;
// Getting Batch No on file name....
long
batch = 0;
// long.Parse(file[5]);
// batch = (batch == 0) ? 0 : (batch + 1);
//.......................
m_QryStr =
"select * from file_master where CATEGORY='"
+ file[0] + file[1] +
"' AND TXNTYPE='"
+ TXNTYPE +
"'"
;
DataSet DS = DbObj.gl_ExeDataSet(m_QryStr);
DbObj.log_write(
"\n Select File_master Query :"
+ m_QryStr);
if
(DS.Tables[0].Rows.Count > 0)
{
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"
) +
"'"
;
batch = ((DbObj.gl_exeScalar(m_QryStr)).ToString() ==
"0"
) ? 1 :
long
.Parse(DbObj.gl_exeScalar(m_QryStr).ToString()) + 1;
DbObj.log_write(
"\n Select Max Batch No Query :"
+ m_QryStr);
}
for
(
int
c = 0; c < loop_count; c++)
{
string
data = DateTime.Now.ToString(
"dd/MM/yyyy"
);
string
[] datetext = data.Split(
' '
);
string
date = datetext[0];
//MAX BATCH...
//puneet change.....25 jan
//m_QryStr = "select (MAX(Batchno)) as BatchNo from maxbatchbackup where convert(varchar,UploadDate,103)='" + date + "'";
m_QryStr =
"select * from file_master where CATEGORY='"
+ file[0] + file[1] +
"' AND TXNTYPE='"
+ TXNTYPE +
"'"
;
DS = DbObj.gl_ExeDataSet(m_QryStr);
DbObj.log_write(
"\n Select File_master Query :"
+ m_QryStr);
if
(DS.Tables[0].Rows.Count > 0)
{
string
file_batchno =
""
;
file_batchno = batch.ToString().PadLeft(6,
'0'
);
m_QryStr =
"select * from temp_header_data"
;
DataSet dshddetails = DbObj.gl_ExeDataSet(m_QryStr);
DbObj.log_write(
"\n Select Header into temp table Query :"
+ m_QryStr);
if
(dshddetails.Tables[0].Rows.Count > 0)
{
head_data = dshddetails.Tables[0].Rows[0][
"hd_data"
].ToString();
}
//---------------header data + total count--------------------
m_QryStr =
"select top "
+ recordcount +
" * from temp_details_data select top "
+ recordcount +
" * from temp_details_data "
;
DataSet dsdtrowdetails = DbObj.gl_ExeDataSet(m_QryStr);
DbObj.log_write(
"\n Select Recoard into temp table Query :"
+ m_QryStr);
count = dsdtrowdetails.Tables[0].Rows.Count;
//-----------------total sum-----------------------------------
m_QryStr =
"select SUM(var_fld_1) from temp_details_data where dt_id in (select top "
+ recordcount +
" dt_id from temp_details_data)"
;
DataSet dsvarfld1 = DbObj.gl_ExeDataSet(m_QryStr);
DbObj.log_write(
"\n Select Amount into temp table Query :"
+ m_QryStr);
if
(dsvarfld1.Tables[0].Rows.Count > 0)
{
varfld1 = Convert.ToInt32(dsvarfld1.Tables[0].Rows[0][0].ToString());
}
if
(((file[0] + file[1]).ToUpper() ==
"APBCR"
|| (file[0] + file[1]).ToUpper() ==
"ACHCR"
) && (file[5].ToUpper() ==
"INW"
|| file[6].ToUpper() ==
"RTN"
))
{
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]
DbObj.log_write(
"\n File NAme :"
+ M_FLNM);
}
else
{
M_FLNM = Destination +
"\\" + file[0] + "
-
" + file[1] + "
-
" + file[2] + "
-
" + file[3] + "
-
" + batch.ToString().PadLeft(6, '0') + "
-" + TXNTYPE + Path.GetExtension(files[s]);
DbObj.log_write(
"\n File NAme :"
+ M_FLNM);
}
if
(!File.Exists(M_FLNM))
//607066D
{
STRWRT =
new
StreamWriter(M_FLNM);
DbObj.log_write(
"\n Created File :"
+ M_FLNM);
}
else
{
File.Delete(M_FLNM);
STRWRT =
new
StreamWriter(M_FLNM);
}
//------------------
string
fn = Path.GetFileName(M_FLNM);
string
now = DateTime.Now.ToString(
"dd/MM/yyyy"
);
string
fileid =
""
;
m_QryStr =
" select FileID from File_Details where Filename='"
+ SFilename +
"'"
;
DR = DbObj.gl_ExeReader(m_QryStr);
if
(DR.Read())
{
fileid = (DR[
"FileID"
].ToString());
}
DR.Close();
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() +
"')"
;
DbObj.gl_ExeNonQuery(m_QryStr);
//--------------------
string
hd_var_fld_1 = count.ToString().PadLeft(diff_hd_count,
'0'
);
string
hd_var_fld_2 = varfld1.ToString().PadLeft(diff_hd_sum,
'0'
);
var aStringBuilder =
new
StringBuilder(head_data);
aStringBuilder.Remove(countVFHDfrom, diff_hd_count);
aStringBuilder.Insert(countVFHDfrom, hd_var_fld_1);
head_data = aStringBuilder.ToString();
aStringBuilder.Remove(sumVFHDfrom, diff_hd_sum);
aStringBuilder.Insert(sumVFHDfrom, hd_var_fld_2);
head_data = aStringBuilder.ToString();
STRWRT.WriteLine(
string
.Format(
"{0,-"
+ rowsize +
"}"
, head_data));
for
(
int
y = 0; y < dsdtrowdetails.Tables[0].Rows.Count; y++)
{
row_deatils = dsdtrowdetails.Tables[0].Rows[y][
"dt_data"
].ToString();
STRWRT.WriteLine(
string
.Format(
"{0,-"
+ rowsize +
"}"
, row_deatils));
}
STRWRT.Close();
m_QryStr =
"delete top ("
+ recordcount +
") from temp_details_data"
;
DbObj.gl_ExeNonQuery(m_QryStr);
m_QryStr =
"insert into max_batch (Batchno,UploadDate) values("
+ batchno +
",convert(datetime,'"
+ date +
"',103))"
;
DbObj.gl_ExeNonQuery(m_QryStr);
//MessageBox.Show("File Created");
//batchno++;
batch++;
}
}
#endregion
#endregion
File.Delete(files[s]);
}
#region other TXT file formate
else
{
/*
#region Getting Split Details from Database
m_QryStr = "truncate table temp_header_data";
DbObj.gl_ExeNonQuery(m_QryStr);
m_QryStr = "truncate table temp_details_data";
DbObj.gl_ExeNonQuery(m_QryStr);
string format = fileformat.Replace("-", "");
recordcount = 0;
string File_MST_ID = "";
m_QryStr = "Select RECORDS,ROW_SIZE,FILE_MST_ID from FILE_MASTER where CATEGORY='" + format + "' and [TXNTYPE]='" + file[6] + "' and FILE_ACTION='SPLIT'";
DR = DbObj.gl_ExeReader(m_QryStr);
if (DR.Read())
{
recordcount = Convert.ToInt32(DR["RECORDS"].ToString());
rowsize = Convert.ToInt32(DR["ROW_SIZE"].ToString());
File_MST_ID = DR["FILE_MST_ID"].ToString();
}
DR.Close();
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+"' ";
DR = DbObj.gl_ExeReader(m_QryStr);
if (DR.Read())
{
countVFHDfrom = Convert.ToInt32(DR["VF_HD_FROM"].ToString());
countVFHDto = Convert.ToInt32(DR["VF_HD_TO"].ToString());
diff_hd_count = (countVFHDto - countVFHDfrom);
}
DR.Close();
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 + "'";
DR = DbObj.gl_ExeReader(m_QryStr);
if (DR.Read())
{
sumVFHDfrom = Convert.ToInt32(DR["VF_HD_FROM"].ToString());
sumVFHDto = Convert.ToInt32(DR["VF_HD_TO"].ToString());
diff_hd_sum = (sumVFHDto - sumVFHDfrom);
sumVFDTfrom = Convert.ToInt32(DR["VF_DT_FROM"].ToString());
sumVFDTto = Convert.ToInt32(DR["VF_DT_TO"].ToString());
diff_dt_sum = (sumVFDTto - sumVFDTfrom);
}
DR.Close();
//--------------------------------refrence---------------------------------------------------------
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'";
DR = DbObj.gl_ExeReader(m_QryStr);
if (DR.Read())
{
refVFHDfrom = Convert.ToInt32(DR["VF_HD_FROM"].ToString());
refVFHDto = Convert.ToInt32(DR["VF_HD_TO"].ToString());
diff_hd_ref = (refVFHDto - refVFHDfrom);
refVFDTfrom = Convert.ToInt32(DR["VF_DT_FROM"].ToString());
refVFDTto = Convert.ToInt32(DR["VF_DT_TO"].ToString());
diff_dt_ref = (refVFDTto - refVFDTfrom);
}
DR.Close();
//--------------------------------refrence---------------------------------------------------------
if (recordcount < m_TotCount)
{
int i = 0;
for (i = 0; i < m_TotCount; i++)
{
if ((m_Read = File.ReadLines(files[s]).ElementAt(i).ToString()) != "")
{
if (i == 0)
{
hd_data = m_Read.Substring(0);
hd_count_field = Convert.ToInt32(m_Read.Substring(countVFHDfrom, diff_hd_count));
hd_amt_field = Convert.ToInt32(m_Read.Substring(sumVFHDfrom, diff_hd_sum));
m_QryStr = "INSERT INTO temp_header_data (hd_data,var_fld_1,var_fld_2)" +
" VALUES ('" + hd_data + "','" + hd_count_field + "','" + hd_amt_field + "' )";
//" TO_DATE('" + M_STLMNTDT + "','DD/MM/YYYY'),'" + M_DESTIFSC + "','" + M_CYCLE
DbObj.gl_ExeNonQuery(m_QryStr);
}
if (i > 0)
{
dt_data = m_Read.Substring(0);
dt_amt_field = Convert.ToInt32(m_Read.Substring(sumVFDTfrom, diff_dt_sum));
m_QryStr = "INSERT INTO temp_details_data (dt_data,var_fld_1)" +
" VALUES ('" + dt_data + "','" + dt_amt_field + "')";
DbObj.gl_ExeNonQuery(m_QryStr);
}
}
}
DbObj.log_write(" REsult :" + i);
}
#endregion
# region "SPLIT"
/*
loop_count = (m_TotCount - 1) / recordcount;
if ((m_TotCount - 1) % recordcount != 0)
{
loop_count = loop_count + 1;
}
//batchno = 1;
// Getting Batch No on file name....
long batch = 0;// long.Parse(file[5]);
// batch = (batch == 0) ? 0 : (batch + 1);
//.......................
for (int c = 0; c < loop_count; c++)
{
string data = DateTime.Now.ToString("dd/MM/yyyy");
string[] datetext = data.Split(' ');
string date = datetext[0];
//MAX BATCH...
//puneet change.....25 jan
//m_QryStr = "select (MAX(Batchno)) as BatchNo from maxbatchbackup where convert(varchar,UploadDate,103)='" + date + "'";
m_QryStr = "select * from file_master where CATEGORY='" + file[0] + file[1] + "' AND TXNTYPE='" + file[6] + "'";
DataSet DS = DbObj.gl_ExeDataSet(m_QryStr);
if (DS.Tables[0].Rows.Count > 0)
{
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") + "'";
batch = ((DbObj.gl_exeScalar(m_QryStr)).ToString() == "0") ? 1 : long.Parse(DbObj.gl_exeScalar(m_QryStr).ToString())+1;
//.............
string file_batchno = "";
file_batchno = batch.ToString().PadLeft(6, '0');
m_QryStr = "select * from temp_header_data";
DataSet dshddetails = DbObj.gl_ExeDataSet(m_QryStr);
if (dshddetails.Tables[0].Rows.Count > 0)
{
head_data = dshddetails.Tables[0].Rows[0]["hd_data"].ToString();
}
//---------------header data + total count--------------------
m_QryStr = "select top " + recordcount + " * from temp_details_data select top " + recordcount + " * from temp_details_data ";
DataSet dsdtrowdetails = DbObj.gl_ExeDataSet(m_QryStr);
count = dsdtrowdetails.Tables[0].Rows.Count;
//-----------------total sum-----------------------------------
m_QryStr = "select SUM(var_fld_1) from temp_details_data where dt_id in (select top " + recordcount + " dt_id from temp_details_data)";
DataSet dsvarfld1 = DbObj.gl_ExeDataSet(m_QryStr);
if (dsvarfld1.Tables[0].Rows.Count > 0)
{
varfld1 = Convert.ToInt32(dsvarfld1.Tables[0].Rows[0][0].ToString());
}
// @"\SplitOutput\" +
if (((file[0] + file[1]).ToUpper() == "APBCR" || (file[0] + file[1]).ToUpper() == "ACHCR") && (file[6].ToUpper()=="INW" || file[6].ToUpper()=="RTN"))
{
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]
}
else
{
M_FLNM = Destination + "\\"+file[0] + "-" + file[1] + "-" + file[2] + "-" + file[3] + "-" + file[4] + "-" + batch.ToString().PadLeft(6, '0') + "-" + file[6] + Path.GetExtension(files[s]);
}
if (!File.Exists(M_FLNM)) //607066D
{
STRWRT = new StreamWriter(M_FLNM);
}
else
{
File.Delete(M_FLNM);
STRWRT = new StreamWriter(M_FLNM);
}
//------------------
string fn = Path.GetFileName(M_FLNM);
string now = DateTime.Now.ToString("dd/MM/yyyy");
string fileid = "";
m_QryStr = " select FileID from File_Details where Filename='" + SFilename + "'";
DR = DbObj.gl_ExeReader(m_QryStr);
if (DR.Read())
{
fileid = (DR["FileID"].ToString());
}
DR.Close();
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() + "')";
DbObj.gl_ExeNonQuery(m_QryStr);
//------------------
string hd_var_fld_1 = count.ToString().PadLeft(diff_hd_count, '0');
string hd_var_fld_2 = varfld1.ToString().PadLeft(diff_hd_sum, '0');
var aStringBuilder = new StringBuilder(head_data);
aStringBuilder.Remove(countVFHDfrom, diff_hd_count);
aStringBuilder.Insert(countVFHDfrom, hd_var_fld_1);
head_data = aStringBuilder.ToString();
aStringBuilder.Remove(sumVFHDfrom, diff_hd_sum);
aStringBuilder.Insert(sumVFHDfrom, hd_var_fld_2);
head_data = aStringBuilder.ToString();
STRWRT.WriteLine(string.Format("{0,-" + rowsize + "}", head_data));
for (int y = 0; y < dsdtrowdetails.Tables[0].Rows.Count; y++)
{
row_deatils = dsdtrowdetails.Tables[0].Rows[y]["dt_data"].ToString();
STRWRT.WriteLine(string.Format("{0,-" + rowsize + "}", row_deatils));
}
STRWRT.Close();
m_QryStr = "delete top (" + recordcount + ") from temp_details_data";
DbObj.gl_ExeNonQuery(m_QryStr);
m_QryStr = "insert into max_batch (Batchno,UploadDate) values(" + batchno + ",convert(datetime,'" + date + "',103))";
DbObj.gl_ExeNonQuery(m_QryStr);
//MessageBox.Show("File Created");
//batchno++;
batch++;
}
}
#endregion
*/
}
#endregion
}
public
void
FILESPLIT(
string
path1,
string
Destination,
string
original,
out
string
message)
{
DbObj.log_write(path1 +
"\n\n\t"
+ Destination);
message =
""
;
try
{
//-------------File Split Code-------------------------------
if
(Directory.Exists(path1))
{
long
n = 0;
var f = Directory.EnumerateFiles(path1,
"*.*"
).Where(s => s.ToLower().EndsWith(
".txt"
) || s.ToLower().EndsWith(
".csv"
)).ToList();
//Directory.GetFiles(path1, "*.txt|*.csv");//, SearchOption.AllDirectories);
//files = Directory.GetFiles(path1);
index = 0;
foreach
(
string
fl
in
f)
{
files.Add(fl);
index++;
}
if
(files.Count > 0)
{
for
(s = 0; s < files.Count; s++)
{
string
fileextention = Path.GetExtension(files[s].ToUpper());
#region for only APB-CR and ACH CR UCO BANK...
if
(fileextention ==
".TXT"
)
{
SFilename = Path.GetFileName(files[s]);
words = SFilename.Split(
'.'
);
file = words[0].Split(
'-'
);
int
indx = SFilename.LastIndexOf(
'-'
);
TXNTYPE = SFilename.Substring(indx + 1, 3);
if
(indx > 5)
{
file_1st = file[0] +
"-"
+ file[1] +
"-"
+ file[2] +
"-"
+ file[3] +
"-"
+ file[4];
fileformat = file[0] +
"-"
+ file[1];
if
(fileformat == (file[0] +
"-"
+ file[1]))
{
m_TotCount = File.ReadLines(files[s]).Count();
fileaction =
"SPLIT"
;
//---------------- Getting Destination Folder------------
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 +
"'"
;
if
(DbObj.gl_exeScalar(m_QryStr) !=
null
)
//"" : DbObj.gl_exeScalar(m_QryStr).ToString() != null)
{
if
(Directory.Exists(DbObj.gl_exeScalar(m_QryStr).ToString()))
Destination = DbObj.gl_exeScalar(m_QryStr).ToString();
}
string
now = DateTime.Now.ToString(
"dd/MM/yyyy"
);
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] +
"')"
;
DbObj.gl_ExeNonQuery(m_QryStr);
//------------
string
format = fileformat.Replace(
"-"
,
""
);
m_QryStr =
"Select RECORDS from FILE_MASTER where CATEGORY='"
+ format +
"'"
;
DR = DbObj.gl_ExeReader(m_QryStr);
if
(DR.Read())
{
recordcount = Convert.ToInt32(DR[
"RECORDS"
].ToString());
}
DR.Close();
if
(m_TotCount > recordcount)
{
foreach
(
string
item2
in
file)
{
item = item2;
if
(item ==
"INW"
)
{
Split(Destination);
}
if
(item ==
"RTN"
)
{
Split(Destination);
}
if
(item ==
"RES"
)
{
Split(Destination);
}
if
(item ==
"MAP"
)
{
Split(Destination);
}
}
}
else
{
DbObj.log_write(
"Message : "
+
" Spliting not needed"
);
if
(((file[0] + file[1]).ToUpper() ==
"APBCR"
|| (file[0] + file[1]).ToUpper() ==
"ACHCR"
) && (TXNTYPE.ToUpper() ==
"INW"
|| file[5].ToUpper() ==
"RTN"
))
{
string
file_name =
""
;
// Path.GetFileName(M_FLNM);
string
Today_Date = DateTime.Now.ToString(
"dd/MM/yyyy"
);
string
fileid =
""
;
m_QryStr =
" select FileID from File_Details where Filename='"
+ SFilename +
"'"
;
DR = DbObj.gl_ExeReader(m_QryStr);
if
(DR.Read())
{
fileid = (DR[
"FileID"
].ToString());
}
DR.Close();
m_QryStr =
"select * from file_master where CATEGORY='"
+ file[0] + file[1] +
"' AND TXNTYPE='"
+ TXNTYPE +
"'"
;
DataSet DS = DbObj.gl_ExeDataSet(m_QryStr);
if
(DS.Tables[0].Rows.Count > 0)
{
long
Batch = 0;
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"
) +
"'"
;
Batch = ((DbObj.gl_exeScalar(m_QryStr)).ToString() ==
"0"
) ? 0 :
long
.Parse(DbObj.gl_exeScalar(m_QryStr).ToString());
//DataSet DS = DbObj.gl_ExeDataSet(m_QryStr);
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]);
m_QryStr =
"insert into SplitFile_Details (FileID,FileName,Splitlimit,Date) values('"
+ fileid.ToString() +
"','"
+ file_name +
"','"
+ m_TotCount +
"',convert(datetime,'"
+ Today_Date +
"',103))"
;
DbObj.gl_ExeNonQuery(m_QryStr);
File.Move(files[s], Destination +
"\\"
+ file_name);
File.Delete(files[s]);
}
}
else
{
string
file_name =
""
;
// Path.GetFileName(M_FLNM);
string
Today_Date = DateTime.Now.ToString(
"dd/MM/yyyy"
);
string
fileid =
""
;
m_QryStr =
" select FileID from File_Details where Filename='"
+ SFilename +
"'"
;
DR = DbObj.gl_ExeReader(m_QryStr);
if
(DR.Read())
{
fileid = (DR[
"FileID"
].ToString());
}
DR.Close();
m_QryStr =
"select * from file_master where CATEGORY='"
+ file[0] + file[1] +
"' AND TXNTYPE='"
+ TXNTYPE +
"'"
;
DataSet DS = DbObj.gl_ExeDataSet(m_QryStr);
if
(DS.Tables[0].Rows.Count > 0)
{
long
Batch = 0;
m_QryStr =
"select count(FILE_MST_ID) from SplitFile_Details where FILE_MST_ID='"
+ DS.Tables[0].Rows[0][
"FILE_MST_ID"
].ToString() +
"' "
;
Batch = ((DbObj.gl_exeScalar(m_QryStr)).ToString() ==
"0"
) ? 1 :
long
.Parse(DbObj.gl_exeScalar(m_QryStr).ToString() + 1);
file_name = file[0] +
"-"
+ file[1] +
"-"
+ file[2] +
"-"
+ file[3] +
"-"
+ file[4] +
"-"
+ Batch.ToString().PadLeft(6,
'0'
) +
"-"
+ TXNTYPE + Path.GetExtension(files[s]);
m_QryStr =
"insert into SplitFile_Details (FileID,FileName,Splitlimit,Date) values('"
+ fileid.ToString() +
"','"
+ file_name +
"','"
+ m_TotCount +
"',convert(datetime,'"
+ Today_Date +
"',103))"
;
DbObj.gl_ExeNonQuery(m_QryStr);
File.Move(files[s], Destination +
"\\"
+ file_name);
File.Delete(files[s]);
}
}
}
}
}
#endregion
else
{
#region TXT File Split Function
/*
*
if (fileextention == ".TXT")
{
SFilename = Path.GetFileName(files[s]);
words = SFilename.Split('.');
file = words[0].Split('-');
//batchno = Convert.ToInt32(file[5].ToString());
file_1st = file[0] + "-" + file[1] + "-" + file[2] + "-" + file[3] + "-" + file[4];
fileformat = SFilename.Substring(0, 6);
if (fileformat == (file[0] + "-" + file[1]))
{
m_TotCount = File.ReadLines(files[s]).Count();
//----------------
fileaction = "SPLIT";
string now = DateTime.Now.ToString("dd/MM/yyyy");
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] + "')";
DbObj.gl_ExeNonQuery(m_QryStr);
//------------
string format = fileformat.Replace("-", "");
m_QryStr = "Select RECORDS from FILE_MASTER where CATEGORY='" + format + "'";
DR = DbObj.gl_ExeReader(m_QryStr);
if (DR.Read())
{
recordcount = Convert.ToInt32(DR["RECORDS"].ToString());
}
DR.Close();
if (m_TotCount > recordcount)
{
foreach (string item2 in file)
{
item = item2;
if (item == "INW")
{
Split(Destination);
}
if (item == "RTN")
{
Split(Destination);
}
if (item == "RES")
{
Split(Destination);
}
}
}
else
{
DbObj.log_write("Message : " + " Spliting not needed");
if (((file[0] + file[1]).ToUpper() == "APBCR" || (file[0] + file[1]).ToUpper() == "ACHCR") && (file[6].ToUpper() == "INW" || file[6].ToUpper() == "RTN"))
{
string file_name = "";// Path.GetFileName(M_FLNM);
string Today_Date = DateTime.Now.ToString("dd/MM/yyyy");
string fileid = "";
m_QryStr = " select FileID from File_Details where Filename='" + SFilename + "'";
DR = DbObj.gl_ExeReader(m_QryStr);
if (DR.Read())
{
fileid = (DR["FileID"].ToString());
}
DR.Close();
m_QryStr = "select * from file_master where CATEGORY='" + file[0] + file[1] + "' AND TXNTYPE='" + file[6] + "'";
DataSet DS = DbObj.gl_ExeDataSet(m_QryStr);
if (DS.Tables[0].Rows.Count > 0)
{
long Batch = 0;
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") + "'";
Batch = ((DbObj.gl_exeScalar(m_QryStr)).ToString() == "0") ? 0 : long.Parse(DbObj.gl_exeScalar(m_QryStr).ToString());
//DataSet DS = DbObj.gl_ExeDataSet(m_QryStr);
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]);
m_QryStr = "insert into SplitFile_Details (FileID,FileName,Splitlimit,Date) values('" + fileid.ToString() + "','" + file_name + "','" + m_TotCount + "',convert(datetime,'" + Today_Date + "',103))";
DbObj.gl_ExeNonQuery(m_QryStr);
File.Move(files[s], Destination + "\\" + file_name);
File.Delete(files[s]);
}
}
else
{
string file_name = "";// Path.GetFileName(M_FLNM);
string Today_Date = DateTime.Now.ToString("dd/MM/yyyy");
string fileid = "";
m_QryStr = " select FileID from File_Details where Filename='" + SFilename + "'";
DR = DbObj.gl_ExeReader(m_QryStr);
if (DR.Read())
{
fileid = (DR["FileID"].ToString());
}
DR.Close();
m_QryStr = "select * from file_master where CATEGORY='" + file[0] + file[1] + "' AND TXNTYPE='" + file[6] + "'";
DataSet DS = DbObj.gl_ExeDataSet(m_QryStr);
if (DS.Tables[0].Rows.Count > 0)
{
long Batch = 0;
m_QryStr = "select count(FILE_MST_ID) from SplitFile_Details where FILE_MST_ID='" + DS.Tables[0].Rows[0]["FILE_MST_ID"].ToString() + "' ";
Batch = ((DbObj.gl_exeScalar(m_QryStr)).ToString() == "0") ? 1 : long.Parse(DbObj.gl_exeScalar(m_QryStr).ToString() + 1);
file_name = file[0] + "-" + file[1] + "-" + file[2] + "-" + file[3] + "-" + file[4] + "-" + Batch.ToString().PadLeft(6, '0') + "-" + file[6] + Path.GetExtension(files[s]);
m_QryStr = "insert into SplitFile_Details (FileID,FileName,Splitlimit,Date) values('" + fileid.ToString() + "','" + file_name + "','" + m_TotCount + "',convert(datetime,'" + Today_Date + "',103))";
DbObj.gl_ExeNonQuery(m_QryStr);
File.Move(files[s], Destination + "\\" + file_name);
File.Delete(files[s]);
}
}
}
}
}*/
#endregion
}
}
#region .CSV File Split
if
(fileextention ==
".CSV"
)
{
SFilename = Path.GetFileName(files[s]);
DbObj.log_write(
"Split CSV File Process : "
+ files[s]);
words = SFilename.Split(
'.'
);
file = words[0].Split(
'-'
);
//batchno = Convert.ToInt32(file[5].ToString());
file_1st = file[0] +
"-"
+ file[1] +
"-"
+ file[2] +
"-"
+ file[3] +
"-"
+ file[4];
fileformat = SFilename.Substring(0, 6);
if
(fileformat == (file[0] +
"-"
+ file[1]))
{
m_TotCount = File.ReadLines(files[s]).Count();
//-------------------------
string
fileaction =
"Split"
;
string
now = DateTime.Now.ToString(
"dd/MM/yyyy"
);
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] +
"')"
;
DbObj.gl_ExeNonQuery(m_QryStr);
//-------------------------
string
format = fileformat.Replace(
"-"
,
""
);
m_QryStr =
"Select RECORDS from FILE_MASTER where CATEGORY='"
+ format +
"'"
;
DR = DbObj.gl_ExeReader(m_QryStr);
if
(DR.Read())
{
recordcount = Convert.ToInt32(DR[
"RECORDS"
].ToString());
}
DR.Close();
if
(m_TotCount > recordcount)
{
foreach
(
string
item
in
file)
{
if
(item ==
"INW"
)
{
Split(Destination);
}
if
(item ==
"RTN"
)
{
Split(Destination);
}
if
(item ==
"RES"
)
{
Split(Destination);
}
}
}
else
{
DbObj.log_write(
"Message : "
+
" Spliting not needed"
);
string
file_name =
""
;
// Path.GetFileName(M_FLNM);
string
Today_Date = DateTime.Now.ToString(
"dd/MM/yyyy"
);
string
fileid =
""
;
m_QryStr =
" select FileID from File_Details where Filename='"
+ SFilename +
"'"
;
DR = DbObj.gl_ExeReader(m_QryStr);
if
(DR.Read())
{
fileid = (DR[
"FileID"
].ToString());
}
DR.Close();
m_QryStr =
"select * from file_master where CATEGORY='"
+ file[0] + file[1] +
"' AND TXNTYPE='"
+ file[6] +
"'"
;
DataSet DS = DbObj.gl_ExeDataSet(m_QryStr);
if
(DS.Tables[0].Rows.Count > 0)
{
long
Batch = 0;
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"
) +
"'"
;
Batch = ((DbObj.gl_exeScalar(m_QryStr)).ToString() ==
"0"
) ? 1 :
long
.Parse(DbObj.gl_exeScalar(m_QryStr).ToString()) + 1;
file_name = file[0] +
"-"
+ file[1] +
"-"
+ file[2] +
"-"
+ file[3] +
"-"
+ file[4] +
"-"
+ Batch.ToString().PadLeft(6,
'0'
) +
"-"
+ file[6] + Path.GetExtension(files[s]);
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() +
"')"
;
DbObj.gl_ExeNonQuery(m_QryStr);
File.Move(files[s], Destination +
"\\"
+ file_name);
File.Delete(files[s]);
}
}
}
}
#endregion
}
}
else
{
//MessageBox.Show("Files are not Found...");
}
}
else
{
// MessageBox.Show("Folder not Found");
}
string
rootFolderPath = @path1 + @
"\"
;
string
destinationPath = original +
"\\"
;
foreach
(
string
file
in
files)
{
string
filename = Path.GetFileName(file);
string
fileToMove = rootFolderPath + filename;
string
moveTo = destinationPath + filename;
//moving file
File.Move(fileToMove, moveTo);
}
}
catch
(Exception ex)
{
DbObj.log_write(
"Split Text File Exception : "
+ ex.Message);
}
}
void
MMerge()
{
#region INBOX (INW)
m_QryStr =
"truncate table temp_header_data"
;
DbObj.gl_ExeNonQuery(m_QryStr);
m_QryStr =
"truncate table temp_details_data"
;
DbObj.gl_ExeNonQuery(m_QryStr);
string
format = Mfileformat.Replace(
"-"
,
""
);
recordcount = 0;
m_QryStr =
"Select RECORDS,ROW_SIZE from FIRST_MASTER where FILE_ACTION='MERGE' AND CATEGORY='"
+ format +
"'"
;
DR = DbObj.gl_ExeReader(m_QryStr);
if
(DR.Read())
{
recordcount = Convert.ToInt32(DR[
"RECORDS"
].ToString());
rowsize = Convert.ToInt32(DR[
"ROW_SIZE"
].ToString());
}
DR.Close();
m_QryStr =
"Select VF_HD_FROM,VF_HD_TO from SECOND_MASTER where CATEGORY='"
+ format +
"' and ACTION='COUNT'"
;
DR = DbObj.gl_ExeReader(m_QryStr);
if
(DR.Read())
{
countVFHDfrom = Convert.ToInt32(DR[
"VF_HD_FROM"
].ToString());
countVFHDto = Convert.ToInt32(DR[
"VF_HD_TO"
].ToString());
diff_hd_count = (countVFHDto - countVFHDfrom);
}
DR.Close();
m_QryStr =
"select VF_HD_FROM,VF_HD_TO,VF_DT_FROM,VF_DT_TO from SECOND_MASTER where CATEGORY='"
+ format +
"' and ACTION='SUM'"
;
DR = DbObj.gl_ExeReader(m_QryStr);
if
(DR.Read())
{
sumVFHDfrom = Convert.ToInt32(DR[
"VF_HD_FROM"
].ToString());
sumVFHDto = Convert.ToInt32(DR[
"VF_HD_TO"
].ToString());
diff_hd_sum = (sumVFHDto - sumVFHDfrom);
sumVFDTfrom = Convert.ToInt32(DR[
"VF_DT_FROM"
].ToString());
sumVFDTto = Convert.ToInt32(DR[
"VF_DT_TO"
].ToString());
diff_dt_sum = (sumVFDTto - sumVFDTfrom);
}
DR.Close();
int
i, j = 0;
for
(i = 0; i < MergeFiles.Count; i++)
{
int
m_TotCount = File.ReadLines(MergeFiles[i]).Count();
for
(j = 0; j < m_TotCount; j++)
{
if
((m_Read = File.ReadLines(MergeFiles[i].ToString()).ElementAt(j).ToString()) !=
""
)
{
if
(j == 0)
{
hd_data = m_Read.Substring(0);
hd_count_field = Convert.ToInt32(m_Read.Substring(countVFHDfrom, diff_hd_count));
hd_amt_field = Convert.ToInt32(m_Read.Substring(sumVFHDfrom, diff_hd_sum));
m_QryStr =
"INSERT INTO temp_header_data (hd_data,var_fld_1,var_fld_2)"
+
" VALUES ('"
+ hd_data +
"','"
+ hd_count_field +
"','"
+ hd_amt_field +
"' )"
;
DbObj.gl_ExeNonQuery(m_QryStr);
}
if
(j > 0)
{
dt_data = m_Read.Substring(0);
dt_amt_field = Convert.ToInt32(m_Read.Substring(sumVFDTfrom, diff_dt_sum));
m_QryStr =
"INSERT INTO temp_details_data (dt_data,var_fld_1)"
+
" VALUES ('"
+ dt_data +
"','"
+ dt_amt_field +
"')"
;
DbObj.gl_ExeNonQuery(m_QryStr);
}
}
}
}
//MessageBox.Show("uploading done");
# region "MERGE"
m_QryStr =
"select COUNT(*) from temp_details_data"
;
DataSet dsdatacount = DbObj.gl_ExeDataSet(m_QryStr);
int
datacount = Convert.ToInt32(dsdatacount.Tables[0].Rows[0][0].ToString());
//int m_TotalCount = File.ReadLines(txtupload.Text).Count();
loop_count = datacount / recordcount;
if
(datacount % recordcount != 0)
{
loop_count = loop_count + 1;
}
//batchno = 1;
for
(
int
c = 0; c < loop_count; c++)
{
string
data = DateTime.Now.ToString(
"dd/MM/yyyy"
);
string
[] datetext = data.Split(
' '
);
string
date = datetext[0];
m_QryStr =
"select (MAX(Batchno)) as BatchNo from maxbatchbackup where convert(varchar,UploadDate,103)='"
+ date +
"' and CATEGORY='"
+ Mfile[0] + Mfile[1] +
"'"
;
DR = DbObj.gl_ExeReader(m_QryStr);
//Max_Batch
if
(DR.Read())
{
if
(DR[
"BatchNo"
].ToString() ==
""
)
{
batchno = 1;
}
else
{
maxbatchno = Convert.ToInt32(DR[
"BatchNo"
].ToString());
batchno = maxbatchno + 1;
// MessageBox.Show(":-)");
}
}
DR.Close();
string
file_batchno =
""
;
file_batchno = batchno.ToString().PadLeft(6,
'0'
);
m_QryStr =
"select * from temp_header_data"
;
DataSet dshddetails = DbObj.gl_ExeDataSet(m_QryStr);
if
(dshddetails.Tables[0].Rows.Count > 0)
{
head_data = dshddetails.Tables[0].Rows[0][
"hd_data"
].ToString();
}
//---------------header data + total count--------------------
m_QryStr =
"select top "
+ recordcount +
" * from temp_details_data"
;
DataSet dsdtrowdetails = DbObj.gl_ExeDataSet(m_QryStr);
count = dsdtrowdetails.Tables[0].Rows.Count;
//-----------------total sum-----------------------------------
m_QryStr =
"select SUM(var_fld_1) from temp_details_data where dt_id in (select top "
+ recordcount +
" dt_id from temp_details_data)"
;
DataSet dsvarfld1 = DbObj.gl_ExeDataSet(m_QryStr);
if
(dsvarfld1.Tables[0].Rows.Count > 0)
{
varfld1 = Convert.ToInt32(dsvarfld1.Tables[0].Rows[0][0].ToString());
}
//M_FLNM = "C:\\Users\\vijeth\\Desktop\\APB-CR-" + "-" + DateTime.Now.ToString("ddMMyyyy") + "-BatchNo-" + batchno + "-RTN.txt";
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] + "
";
if
(!File.Exists(M_FLNM))
{
STRWRT =
new
StreamWriter(M_FLNM);
}
else
{
File.Delete(M_FLNM);
STRWRT =
new
StreamWriter(M_FLNM);
}
//-----------------------------
string
fn = Path.GetFileName(M_FLNM);
string
now = DateTime.Now.ToString(
"dd/MM/yyyy"
);
//string fileid = "";
//m_QryStr = " select FileID from File_Details where Filename='" + MFilename + "'";
//DR = DbObj.gl_ExeReader(m_QryStr);
//if (DR.Read())
//{
// fileid = (DR["FileID"].ToString());
//}
//DR.Close();
m_QryStr =
"insert into MergeFile_Details (FileName,Mergelimit,Date) values(' "
+ fn +
"','"
+ count +
"','"
+ now +
"')"
;
DbObj.gl_ExeNonQuery(m_QryStr);
//-----------------------------
string
hd_var_fld_1 = count.ToString().PadLeft(diff_hd_count,
'0'
);
string
hd_var_fld_2 = varfld1.ToString().PadLeft(diff_hd_sum,
'0'
);
var aStringBuilder =
new
StringBuilder(head_data);
aStringBuilder.Remove(countVFHDfrom, diff_hd_count);
aStringBuilder.Insert(countVFHDfrom, hd_var_fld_1);
head_data = aStringBuilder.ToString();
aStringBuilder.Remove(sumVFHDfrom, diff_hd_sum);
aStringBuilder.Insert(sumVFHDfrom, hd_var_fld_2);
head_data = aStringBuilder.ToString();
STRWRT.WriteLine(
string
.Format(
"{0,-"
+ rowsize +
"}"
, head_data));
for
(
int
x = 0; x < dsdtrowdetails.Tables[0].Rows.Count; x++)
{
row_deatils = dsdtrowdetails.Tables[0].Rows[x][
"dt_data"
].ToString();
STRWRT.WriteLine(
string
.Format(
"{0,-"
+ rowsize +
"}"
, row_deatils));
}
STRWRT.Close();
m_QryStr =
"delete top ("
+ recordcount +
") from temp_details_data"
;
DbObj.gl_ExeNonQuery(m_QryStr);
m_QryStr =
"insert into max_batch (Batchno,UploadDate,CATEGORY) values("
+ batchno +
",convert(datetime,'"
+ date +
"',103),'"
+ Mfile[0] + Mfile[1] +
"')"
;
DbObj.gl_ExeNonQuery(m_QryStr);
//MessageBox.Show("File Created");
batchno++;
}
#endregion
#endregion
}
public
void
FILEMERGE(
string
path2)
{
if
(Directory.Exists(path2))
{
Mfiles = Directory.GetFiles(path2);
List<
string
> filetypes =
new
List<
string
>();
filetypes.Clear();
foreach
(
string
str
in
Mfiles)
{
string
[] fullname = Path.GetFileName(str).Split(
'.'
);
string
[] type = fullname[0].Split(
'-'
);
if
(!filetypes.Contains(type[0] +
"-"
+ type[1]))
{
filetypes.Add(type[0] +
"-"
+ type[1]);
}
}
if
(Mfiles.Length > 0)
{
for
(
int
n = 0; n < filetypes.Count; n++)
{
for
(m = 0; m < Mfiles.Length; m++)
{
if
(Path.GetFileName(Mfiles[m]).StartsWith(filetypes[n]))
{
//var filename = new FileInfo(Mfiles[m]);
string
fileextention = Path.GetExtension(Mfiles[m].ToUpper());
// ---------------text file code--------------------------------------
if
(fileextention ==
".TXT"
)
{
MFilename = Path.GetFileName(Mfiles[m]);
Mwords = MFilename.Split(
'.'
);
Mfile = Mwords[0].Split(
'-'
);
//batchno = Convert.ToInt32(file[5].ToString());
Mfile_1st = Mfile[0] +
"-"
+ Mfile[1] +
"-"
+ Mfile[2] +
"-"
+ Mfile[3];
// +"-" + Mfile[4];
Mfileformat = MFilename.Substring(0, 6);
if
(Mfileformat == (Mfile[0] +
"-"
+ Mfile[1]))
{
//if (Mfileformat == "ACH-CR")
//{
Mm_TotCount = File.ReadLines(Mfiles[m]).Count();
//---------------------
fileaction =
"Merge"
;
string
now = DateTime.Now.ToString(
"dd/MM/yyyy"
);
m_QryStr =
"insert into File_Details(Filename ,FileMaxlimit,FileAction,Date) values ('"
+ MFilename +
"','"
+ Mm_TotCount +
"','"
+ fileaction +
"','"
+ now +
"')"
;
DbObj.gl_ExeNonQuery(m_QryStr);
//---------------------
string
format = Mfileformat.Replace(
"-"
,
""
);
recordcount = 0;
m_QryStr =
"Select RECORDS from FIRST_MASTER where FILE_ACTION='MERGE' AND CATEGORY='"
+ format +
"'"
;
DR = DbObj.gl_ExeReader(m_QryStr);
if
(DR.Read())
{
recordcount = Convert.ToInt32(DR[
"RECORDS"
].ToString());
}
DR.Close();
if
(Mm_TotCount < recordcount)
{
MergeFiles.Add(Mfiles[m].ToString());
}
else
{
MessageBox.Show(
"File Merging not Needed"
);
}
//}
# region APB
//if (Mfileformat == "APB-CR")
//{
// Mm_TotCount = File.ReadLines(Mfiles[m]).Count();
// if (Mm_TotCount < 22)
// {
// for (int t = 0; t < Mfiles.Length; t++)
// {
// string arrayfilename = Path.GetFileName(Mfiles[t]);
// string[] arraywords = arrayfilename.Split('.');
// string[] arrayfile = arraywords[0].Split('-');
// if (Mfileformat == arrayfile[0] + arrayfile[1] + arrayfile[5].Trim())
// {
// //int rec_count = File.ReadLines(files[t]).Count();
// //totalrec = totalrec + rec_count;
// //if (totalrec < recordcount)
// //{
// ABPMergeFiles.Add(Mfiles[t].ToString());
// //MergeFiles.Add(Mfiles[t].ToString());
// //}
// }
// }
// //call here the merge function
// // MMerge();
// }
// else
// {
// MessageBox.Show("File Merging not Needed");
// }
//}
#endregion
}
}
//-----------------------end text file code-------------------------------------------
if
(fileextention ==
".CSV"
)
{
MFilename = Path.GetFileName(Mfiles[m]);
Mwords = MFilename.Split(
'.'
);
Mfile = Mwords[0].Split(
'-'
);
//batchno = Convert.ToInt32(file[5].ToString());
Mfile_1st = Mfile[0] +
"-"
+ Mfile[1] +
"-"
+ Mfile[2] +
"-"
+ Mfile[3];
// +"-" + Mfile[4];
Mfileformat = MFilename.Substring(0, 6);
if
(Mfileformat == (Mfile[0] +
"-"
+ Mfile[1]))
{
Mm_TotCount = File.ReadLines(Mfiles[m]).Count();
//--------------------
fileaction =
"Merge"
;
String now = DateTime.Now.ToString(
"dd/MM/yyyy"
);
m_QryStr =
"insert into File_Details(Filename ,FileMaxlimit,FileAction,Date) values ('"
+ MFilename +
"','"
+ Mm_TotCount +
"','"
+ fileaction +
"','"
+ now +
"')"
;
DbObj.gl_ExeNonQuery(m_QryStr);
//--------------------
string
format = Mfileformat.Replace(
"-"
,
""
);
recordcount = 0;
m_QryStr =
"Select RECORDS from FIRST_MASTER where FILE_ACTION='MERGE' AND CATEGORY='"
+ format +
"'"
;
DR = DbObj.gl_ExeReader(m_QryStr);
if
(DR.Read())
{
recordcount = Convert.ToInt32(DR[
"RECORDS"
].ToString());
}
DR.Close();
if
(Mm_TotCount < recordcount)
{
MergeFiles.Add(Mfiles[m].ToString());
}
else
{
MessageBox.Show(
"File Merging not Needed"
);
}
//}
# region APB
//if (Mfileformat == "APB-CR")
//{
// Mm_TotCount = File.ReadLines(Mfiles[m]).Count();
// if (Mm_TotCount < 22)
// {
// for (int t = 0; t < Mfiles.Length; t++)
// {
// string arrayfilename = Path.GetFileName(Mfiles[t]);
// string[] arraywords = arrayfilename.Split('.');
// string[] arrayfile = arraywords[0].Split('-');
// if (Mfileformat == arrayfile[0] + arrayfile[1] + arrayfile[5].Trim())
// {
// //int rec_count = File.ReadLines(files[t]).Count();
// //totalrec = totalrec + rec_count;
// //if (totalrec < recordcount)
// //{
// ABPMergeFiles.Add(Mfiles[t].ToString());
// //MergeFiles.Add(Mfiles[t].ToString());
// //}
// }
// }
// //call here the merge function
// // MMerge();
// }
// else
// {
// MessageBox.Show("File Merging not Needed");
// }
//}
#endregion
}
}
}
}
MMerge();
string
rootFolderPath = @path2 + @
"\"
;
string
destinationPath = @
"C:\Users\ACSPL\Desktop\Final H2H Project\H2H Testing Apllication\H2H Testing Apllication\bin\Debug\TextSplitMerge\MergeFileBackup\"
;
foreach
(
string
file
in
MergeFiles)
{
string
filename = Path.GetFileName(file);
string
fileToMove = rootFolderPath + filename;
string
moveTo = destinationPath + filename;
//moving file
File.Move(fileToMove, moveTo);
}
MergeFiles.Clear();
}
// second for loop end
}
}
else
{
MessageBox.Show(
"Folder Not Found..."
);
}
}
}
}
ASP.NET Split File
Split File