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.IO;
using System.Text.RegularExpressions; namespace WindowsFormsApplication5
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Console.WriteLine("start....");
            ToMerge toMerge = ToMerge.getToMerge();
            toMerge.Init();
            Console.WriteLine("Done!!!");
                    }
    }    partial class ToMerge
    {
        private static ToMerge toMerge;
        private string file_Name = "";//Summary文件夹路径
        private string EPCMSTTP = "";//EPCMSTTP.txt文件路径
        private string fileName = Application.StartupPath;//当前路径
        private string appName = Application.ProductName;        public static ToMerge getToMerge()
        {
            if (toMerge == null)
            {
                toMerge = new ToMerge();
            }
            return toMerge;
        }        /// <summary>
        /// 运行方法
        /// </summary>
        public void Init()
        {
            checkSummary();
            checkEPCMSTTP();
            try
            {
                bool b = false;
                DirectoryInfo TheFolder = new DirectoryInfo(fileName);                List<string> prefixNameList = PrefixName(TheFolder);                if (prefixNameList.Count > 0)
                {
                    foreach (string pfName in prefixNameList)
                    {
                        string newFile_Name = file_Name + @"\" + pfName;
                        if (!Directory.Exists(newFile_Name))
                        {
                            Directory.CreateDirectory(newFile_Name);
                        }                        foreach (FileInfo NextFile in TheFolder.GetFiles())
                        {
                            //Regex r = new Regex(name);
                            Regex r = new Regex(pfName);
                            Match m = r.Match(NextFile.Name);
                            if (m.Success && m.Index == 0)
                            {
                                NextFile.CopyTo(newFile_Name + @"\" + NextFile.Name, true);
                                b = true;
                            }                        }                        foreach (DirectoryInfo NextFolder in TheFolder.GetDirectories())
                        {
                            //Regex r = new Regex(name);
                            Regex r = new Regex(pfName);
                            Match m = r.Match(NextFolder.Name);
                            if (m.Success && m.Index == 0)
                            {
                                toFor(NextFolder.Name, NextFolder.FullName, newFile_Name);                                b = true;
                            }                        }                        if (!File.Exists(file_Name + @"\EPCMSTTP.txt"))
                        {
                            File.Copy(EPCMSTTP, file_Name + @"\EPCMSTTP.txt");
                            
                        }                    }
                }
                if (b)
                {
                    MessageBox.Show("整合完成!");
                }
                else
                    MessageBox.Show("没有符合相同前缀的文件!");                //file_Name = "";
            }            catch (Exception ex)
            {
                throw ex;
            }        }        /// <summary>
        /// 检测“summary”是否存在,不存在就创建
        /// </summary>
        private void checkSummary()
        {
            if (!Directory.Exists(Path.GetFullPath("./Summary")))
            {
                Directory.CreateDirectory(Path.GetFullPath("./Summary"));
                
            }
            file_Name = Path.GetFullPath("./Summary");
        }        /// <summary>
        /// 检测“EPCMSTTP.txt”是否存在,不存在就创建
        /// </summary>
        private void checkEPCMSTTP()
        {
            if (!File.Exists(fileName + @"\EPCMSTTP.txt"))
            {
                File.Create(fileName + @"\EPCMSTTP.txt");            }
            EPCMSTTP = fileName + @"\EPCMSTTP.txt";
        }        /// <summary>
        /// 递归复制文件
        /// </summary>
        /// <param name="name">文件名</param>
        /// <param name="fileName">完整路径</param>
        /// <param name="addr">目标完整路径</param>
        private void toFor(string name, string fileName, string addr)
        {            string newAddr = addr + @"\" + name;
            if (!Directory.Exists(newAddr))
            {
                Directory.CreateDirectory(newAddr);
                if (!File.Exists(newAddr + @"\EPCMSTTP.txt"))
                {
                    File.Copy(EPCMSTTP, newAddr + @"\EPCMSTTP.txt");
                }
            }            DirectoryInfo TheFolder = new DirectoryInfo(fileName);            foreach (FileInfo NextFile in TheFolder.GetFiles())
            {
                NextFile.CopyTo(newAddr + @"\" + NextFile.Name, true);            }            foreach (DirectoryInfo NextFolder in TheFolder.GetDirectories())
            {
                toFor(NextFolder.Name, NextFolder.FullName, newAddr);            }            if (!File.Exists(addr + @"\EPCMSTTP.txt"))
            {
                File.Copy(EPCMSTTP ,addr + @"\EPCMSTTP.txt");
            }
        }        /// <summary>
        /// 返回同后缀的list
        /// </summary>
        /// <param name="dInFo"></param>
        /// <returns></returns>
        private List<string> PrefixName(DirectoryInfo dInFo)
        {
            List<string> allName_List = new List<string>();
            List<string> sameName_list = new List<string>();            foreach (FileInfo NextFile in dInFo.GetFiles())
            {
                Regex r = new Regex(NextFile.Extension);
                Match m = r.Match(NextFile.Name);
                Regex r1 = new Regex(appName);
                Match m1 = r1.Match(NextFile.Name);
                if (!m1.Success)
                {
                    if (NextFile.Name.Remove(m.Index) != "EPCMSTTP")
                    {
                        allName_List.Add(NextFile.Name.Remove(m.Index));
                    }
                }            }            foreach (DirectoryInfo NextFolder in dInFo.GetDirectories())
            {
                if (NextFolder.Name != "Summary" && NextFolder.Name != appName)
                {
                    allName_List.Add(NextFolder.Name);
                }            }            // string[] patterns = new string[] { "_Sub", "_MDRecord", "_LoadRecord" };             for (int i = 0; i < allName_List.Count; i++)
            {
                string s = "1";
                int k = 0;                Regex r_Sub = new Regex("_Sub");
                Match m_Sub = r_Sub.Match(allName_List[i].ToString());
                Regex r_MDRecord = new Regex("_MDRecord");
                Match m_MDRecord = r_MDRecord.Match(allName_List[i].ToString());
                Regex r_LoadRecord = new Regex("_LoadRecord");
                Match m_LoadRecord = r_LoadRecord.Match(allName_List[i].ToString());
                if (m_Sub.Success || m_MDRecord.Success || m_LoadRecord.Success)
                {
                    s = allName_List[i].ToString().Remove(ToCompare(m_Sub.Index, m_MDRecord.Index, m_LoadRecord.Index));
                }
                else
                {
                    s = allName_List[i].ToString();
                }                for (int j = 1; j < allName_List.Count; j++)
                {
                    string str = "2";                    Regex r1_Sub = new Regex("_Sub");
                    Match m1_Sub = r1_Sub.Match(allName_List[j].ToString());
                    Regex r1_MDRecord = new Regex("_MDRecord");
                    Match m1_MDRecord = r1_MDRecord.Match(allName_List[j].ToString());
                    Regex r1_LoadRecord = new Regex("_LoadRecord");
                    Match m1_LoadRecord = r1_LoadRecord.Match(allName_List[j].ToString());
                    if (m1_Sub.Success || m1_MDRecord.Success || m1_LoadRecord.Success)
                    {
                        str = allName_List[j].ToString().Remove(ToCompare(m_Sub.Index, m_MDRecord.Index, m_LoadRecord.Index));
                    }
                    else
                    {
                        str = allName_List[j].ToString();
                    }                    if (s == str)
                    {
                        k++;
                        allName_List.RemoveAt(j);
                        j--;
                    }
                }                if (k > 0)
                {
                    sameName_list.Add(s);
                    i--;
                }
            }            return sameName_list;        }