1

我在阅读每个 1-20kb 的文本文件时遇到问题。该文件夹有大约 400,000 个文件。我已将程序限制为仅读取每个文本文件的第一行,但它仍然很慢。在读取文件之前,程序会从我选择的文件夹中获取文件名,检查文件名是否是我想要的,然后读取第一行并检查是否正确,最后将文件复制到我的某个地方想。

class FileChoose
{
    public string chooseFolder()
    {
        FolderBrowserDialog Fld = new FolderBrowserDialog();
        Fld.ShowNewFolderButton = false;
        if (Fld.ShowDialog() == DialogResult.OK)
        {
            return Fld.SelectedPath;
        }
        return "";
    }
    public List<string> getFileName(string path)
    {
        string[] filePaths = Directory.GetFiles(@path, "*.log");
        List<string> listPath = new List<string>();
        foreach (var item in filePaths)
        {
            string[] itemSplit = item.Split('\\');
            string year = itemSplit[itemSplit.Length - 1].Substring(0, 4);
            string month = itemSplit[itemSplit.Length - 1].Substring(4, 2);
            if ((year == "2013") && (month == "08"))
            {
            //    string fileNamePDF = itemSplit[itemSplit.Length - 1];
                listPath.Add(item);
            }
        }
        return listPath;
    }
    public bool isDrawing(string drawing, string path)
    {
        string drawingRead = readLog(path);
        if (drawingRead == drawing)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public string readLog(string path)
    {
        StreamReader sr = new StreamReader(path);
        string line;
        line = sr.ReadLine();
        string checkDrawing = line.Substring(1, 8);
        return checkDrawing;
    }
}

主班

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

    }
  //  public string pathGlobal = "D:\\OMT\\OMT1";
  //  public string pathGlobal2 = "D:\\OMT\\OMT2";

    public string pathGlobal = "D:\\logfileProductionline\\RD Team\\Production logfile\\Grundfos\\OMT2-1";
    public string pathGlobal2 = "D:\\logfileProductionline\\RD Team\\Production logfile\\Grundfos\\OMT3";
    List<string> listPath = new List<string>();
    List<string> listPath2 = new List<string>();
    List<string> listFile = new List<string>();
    FileChoose.FileChoose folder = new FileChoose.FileChoose();
    public string folderPath;
   // public string folderPath;
    private void button1_Click(object sender, EventArgs e)
    {
        string folderPathIN = folder.chooseFolder();
        //label1.Text = folderPathIN;
        this.folderPath = folderPathIN;
    }

    private void button2_Click(object sender, EventArgs e)
    {
  //      listPath = folder.getFileName(folderPath);
        listPath = folder.getFileName(pathGlobal);
        foreach (var item in listPath)
        {
         //   string pathFile = folderPath+item;
            bool check = folder.isDrawing("96642678", item);
            if (check)
                copyFile(item);

        }
        listPath2 = folder.getFileName(pathGlobal2);
        foreach (var item in listPath2)
        {
            //   string pathFile = folderPath+item;
            bool check = folder.isDrawing("96642678", item);
            if (check)
                copyFileSeparate(item);

        }
        MessageBox.Show("Success", "Success");
        label1.Text = "Copied files are in D:\\OMT_NEW";
    }
    public void copyFileSeparate(string item)
    {
        string[] splitItem = item.Split('\\');
        string folderName = splitItem[splitItem.Length - 1].Substring(0, 8);
        try
        {
            bool isExists = System.IO.Directory.Exists("D:\\OMTSeparate");
            if (!isExists)
                System.IO.Directory.CreateDirectory("D:\\OMTSeparate");
            isExists = System.IO.Directory.Exists("D:\\OMTSeparate\\"+folderName);
            if (!isExists)
                System.IO.Directory.CreateDirectory("D:\\OMTSeparate\\"+folderName);
            File.Copy(item, "D:\\OMTSeparate\\"+folderName+"\\" + splitItem[splitItem.Length - 1]);
        }
        catch (Exception)
        {
        }
    }
    public void copyFile(string item)
    {
        string[] splitItem = item.Split('\\');
        try
        {
            bool isExists = System.IO.Directory.Exists("D:\\OMT_NEW");
            if (!isExists)
                System.IO.Directory.CreateDirectory("D:\\OMT_NEW");
            File.Copy(item, "D:\\OMT_NEW\\" + splitItem[splitItem.Length - 1]);

        }
        catch(Exception)
        {
        }
    }
   // 
}
4

1 回答 1

3

那是很多文件。

第一步使用 System.IO.Directory.EnumerateFiles 而不是 GetFiles。然后让 GetFilename 使用 yield return 返回 IEnumerable。这将使您不必为 400,000 个文件名分配空间。打开文件仍然需要很多时间。您可以线程打开和读取,很大程度上取决于您的处理器和磁盘子系统,您可以做多少以及它有多少帮助。在一个小得多的测试用例上运行一些测试,并使用它来确定所需的大致时间,并确保放入某种进度指示器,以便您了解事情的进展情况。偶尔创建记录您的进度的检查文件也可能很有用,因此如果发生某些事情,您不必从头开始重新启动。

于 2013-10-01T02:46:55.700 回答