Files
panaceantech/QuickFTP/ContactParser/Program.cs

168 lines
6.2 KiB
C#

using Sdaleo;
using Sdaleo.Systems.SQLServer;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using Yaulw.Assembly;
using Yaulw.File;
using Yaulw.Registry;
namespace ContactParser
{
class Program
{
public static string DataRet_Retrieve(object o)
{
if(o != null && !String.IsNullOrEmpty(o.ToString()))
{
return o.ToString().Trim();
}
return "";
}
static void Main(string[] args)
{
string fpath = AssemblyW.SpecializedAssemblyInfo.GetAssemblyPath(AssemblyW.AssemblyST.Executing);
// Write outFile
string fname = DateTime.Now.ToShortDateString().Replace('/', '_');
FileWriter fw = new FileWriter(fname, "csv", fpath);
SQLServerCredential cred = new SQLServerCredential("localhost", "SQLEXPRESS");
if(!cred.IsValid)
{
Console.WriteLine("SQLServer connection invalid");
return;
}
string[] fFiles = Directory.GetFiles(fpath, "*.csv");
if(fFiles == null || fFiles.Length == 0)
{
Console.WriteLine("No *.csv files found in this directory");
return;
}
string fFile = "DUMMY.csv";
if(fFiles.Length >= 1)
{
foreach (string f in fFiles)
{
if (Path.GetFileName(f).ToLower() != fname + ".csv")
{
fFile = f;
break;
}
}
}
string fileCSV = ReadFile(fFile);
if (String.IsNullOrEmpty(fileCSV))
return;
string[] FileCSV_lines = fileCSV.Split('\n');
DB db = DB.Create(cred);
bool bWriteHeaderColumns = true;
String[] columns = { "" };
// skip first line
for (int i = 1; i < FileCSV_lines.Length; ++i)
{
string[] csvValues = FileCSV_lines[i].Split(',');
if (csvValues.Length > 24)
{
string email = csvValues[0].Replace("\"", "");
bool Clicked = (int.Parse(csvValues[24].Replace("\"","")) == 1 ? true : false);
string query1 = String.Format("SELECT [Register Name], [Dealer Name], [Contact Name], [Street], [City],[State],[Zip Code],[Phone],[Email Address] FROM [VARInfo].[dbo].[RGEmails] where [Email Address] like '%{0}%'", email);
DBRetVal retVal = db.FillDataTable(query1);
if (!retVal.IsValid)
{
if (!bWriteHeaderColumns)
{
fw.WriteLineA(String.Format(",,,,,,,,{0},NOTFOUND", email));
continue;
}
else
{
Console.WriteLine(retVal.ErrorMsg);
return;
}
}
else
{
try
{
DataTable dt = retVal.GetDataTableRetVal();
// Write out Header / Fetch columns
if (bWriteHeaderColumns)
{
StringBuilder sbColumns = new StringBuilder();
foreach (DataColumn c in dt.Columns)
{
sbColumns.Append(c.ColumnName);
sbColumns.Append(',');
}
String columnHeader = sbColumns.ToString().Substring(0, sbColumns.Length - 1);
columns = columnHeader.Split(',');
// Write it to file
fw.WriteLineA(columnHeader + ",Clicked"); // adding click info
bWriteHeaderColumns = false;
}
foreach (DataRow r in dt.Rows)
{
StringBuilder sbRow = new StringBuilder();
for (int j = 0; j < columns.Length; ++j)
{
sbRow.Append(DataRet_Retrieve(r[columns[j]]).Replace(","," "));
sbRow.Append(',');
}
String rowValues = sbRow.ToString().Substring(0, sbRow.Length - 1);
fw.WriteLineA(rowValues + String.Format(",{0}", Clicked)); // adding click info
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return;
}
}
}
}
Process.GetCurrentProcess().Close();
}
public static string ReadFile(string filename)
{
try
{ // Open the text file using a stream reader.
using (StreamReader sr = new StreamReader(filename))
{
// Read the stream to a string, and write the string to the console.
String line = sr.ReadToEnd();
return line;
}
}
catch (Exception e)
{
Console.WriteLine(String.Format("The file could not be read: {0}", filename));
Console.WriteLine(e.Message);
}
return "";
}
}
}