簡體   English   中英

如何讀取給定文件夾中的所有文件?

[英]How do I read from all files in a given folder?

下面的程序解析“ 矮人要塞”的所謂RAW文件之一 該代碼可以正常工作,但是在我當前的實現中,我需要為每個文件運行一次程序,並每次手動更改源文件。 有什么辦法可以代替我解析給定文件夾中的所有文本文件嗎?

(請注意,當前輸出文件與輸入文件位於同一文件夾中。我不太確定如果程序嘗試在寫入過程中嘗試打開該文件,會發生什么情況,但這是需要注意的心神)。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // create reader & open file
            string filePath = @"C:\foo\Dwarf Fortess\creature_domestic.txt";
            string destPath = @"C:\foo\Dwarf Fortess\eggCreatures.txt";
            string line;
            // 
            TextReader tr = new StreamReader(filePath);
            TextWriter tw = new StreamWriter(destPath);

            // read a line of text
            while ((line = tr.ReadLine()) != null) 
            {

                        if (line.Contains("[CREATURE:"))
                        {
                            tw.WriteLine(line);
                        }
                        if(line.Contains("[LAYS_EGGS]"))
                        {
                            tr.ReadLine();
                            tr.ReadLine();
                            tr.ReadLine();
                            tw.WriteLine(tr.ReadLine());
                            tw.WriteLine(tr.ReadLine());
                        }


            }

            // close the stream
            tr.Close();
            tw.Close();
        }
    }
}

您可以使用Directory.EnumerateFiles來獲取Directory.EnumerateFiles中的所有文件並循環瀏覽它們。 您可以提供搜索規范以及搜索是否應遞歸,具體取決於您使用的參數和重載。

順便說一句-您應該using語句包裝流,以確保正確處理:

using(TextReader tr = new StreamReader(filePath))
{
  using(TextWriter tw = new StreamWriter(destPath))
  {
    ....
  }
}

檢查Directory.EnumerateFiles它列出了目錄中的所有文件。 您可以使用options參數選擇是否遞歸工作。

然后簡單地加載文件,它會為您提供一個接一個的文件。

.Net的哪個版本? 您可以對Directory.GetFiles使用linq來排除要寫入的文件。 我目前沒有VS,可能那里有些錯誤,但希望您能明白。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string destPath = @"C:\foo\Dwarf Fortess\eggCreatures.txt";
            FileInfo fiDest = new FileInfo(destPath);
            DirectoryInfo diDF = new DirectoryInfo(@"C:\foo\Dwarf Fortress");

            var files = from FileInfo f in diDF.GetFiles("*.txt")
                where f.Name != fiDest.Name
                select f
            foreach(FileInfo fiRead in files) 
            {

            // create reader & open file
                string filePath = @"C:\foo\Dwarf Fortess\creature_domestic.txt";
                string line;
                // 
                TextReader tr = fiRead.OpenText();
                TextWriter tw = new StreamWriter(destPath);

                // read a line of text
                while ((line = tr.ReadLine()) != null) 
                {

                            if (line.Contains("[CREATURE:"))
                            {
                                tw.WriteLine(line);
                            }
                            if(line.Contains("[LAYS_EGGS]"))
                            {
                                tr.ReadLine();
                                tr.ReadLine();
                                tr.ReadLine();
                                tw.WriteLine(tr.ReadLine());
                                tw.WriteLine(tr.ReadLine());
                            }


                }

                // close the stream
                tr.Close();
                tw.Close();
            }
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM