文字檔合併

摘要:文字檔合併

剛好User 需要使用,

本來要用cmd直接檔案合併 

copy d:\t1\*.txt d:\t2\合併.txt

結果日期竟然寫在檔名上內文只有時間和進出資訊

就幫User寫一個小程式方便他使用整理

 


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
           try
           {
               List myList = new List();

               if (textBox1.Text.Length != 0 && textBox2.Text.Length != 0)
               {
                   string folderName = textBox1.Text;
                   string outputfolder = textBox2.Text;
                   // 取得資料夾內所有檔案
                   //fname 為路徑+檔案名稱
                   foreach (string fname in System.IO.Directory.GetFiles(folderName))
                   {
                       string line;
                       string sfname;
                       System.IO.StreamReader file = new System.IO.StreamReader(fname, System.Text.Encoding.Default);
                       while ((line = file.ReadLine()) != null)
                       {
                           sfname = Path.GetFileName(fname); //取檔案名稱
                           myList.Add(sfname + "," + line.Trim());
                       }
                       file.Close();
                   }
                   File.WriteAllLines(outputfolder+ DateTime.Now.ToString("yyyyMMdd")+".txt", myList.ToArray(), Encoding.UTF8);
                   MessageBox.Show("合併完成");
               }
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message.ToString());
           }
           
            

        }
    }
}
 

file.zip

 

 

 

 

大家一起加入blogads 賺零用錢!!