How do I read from all files in a given folder?
The program below parses one of the so-called RAW files of Dwarf Fortress. The code works fine, but with my current implementation, I need to run the program once for every file, manually changing the source file each time. Is there someway I could instead parse through all text files in the given folder?
(Do note that currently the output file is in the same folder as the input file. I'm not quite sure what would happen if the program tried to open the file it was in the middle of writing to, but it's something to bear in mind).
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.ReadLin开发者_开发百科e());
}
}
// close the stream
tr.Close();
tw.Close();
}
}
}
You can use Directory.EnumerateFiles
to get all files within a directory and loop through them. You can provide search specs and whether the search should be recursive, depending on the parameters and overload you use.
BTW - you should be wrapping your streams in using
statements to ensure proper disposal:
using(TextReader tr = new StreamReader(filePath))
{
using(TextWriter tw = new StreamWriter(destPath))
{
....
}
}
Check Directory.EnumerateFiles
it lists you all files in a directory. You can choose if it works recursively using the options parameter.
Then simply load the files it gives you one by one.
what version of .Net? You can use linq against Directory.GetFiles to exclude the file you're writing to. I don't have VS right now, may be some errors in there but you hopefully get the idea.
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();
}
}
}
}
精彩评论