110,536
社区成员
发帖
与我相关
我的任务
分享
Regex rgx = new Regex(@"http://www.XXXX.com/file/\d{10}/");
string path = @"H:\", newTxt = @"H:\newTxt.txt";
var query1 = from f in Directory.GetFiles(path)
let arr = File.ReadAllLines(f)
where f.EndsWith(".txt")
select new
{
content = (from c in arr
where rgx.IsMatch(c)
select c)
};
query1.ToList().ForEach(item => File.AppendAllLines(newTxt, item.content));
foreach (var str in File.ReadAllLines(newTxt))
{
Console.WriteLine(str);
}
string path = @"H:\", newTxt = @"H:\newTxt.txt";
List<string> result = new List<string>();
var query1 = from f in Directory.GetFiles(path)
let arr = File.ReadAllLines(f)
where f.EndsWith(".txt")
select new
{
content = (from c in arr
//筛选条件
where c.Contains("111")
select c)
};
query1.ToList().ForEach(item => File.AppendAllLines(newTxt, item.content));
foreach (var str in File.ReadAllLines(newTxt))
{
Console.WriteLine(str);
}
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
string text = ReadTxt("D:\\test", SelectRule);
WriteTxt("D:\\a.txt", text);
Console.Read();
}
private static bool SelectRule(string text)
{
return text.Length > 5 ? true : false;
}
private static void WriteTxt(string path, string text)
{
using (StreamWriter sw = new StreamWriter(path))
{
sw.Write(text);
}
}
private static string ReadTxt(string path, Func<string, bool> selectRule)
{
StringBuilder text = new StringBuilder();
string temp = string.Empty;
DirectoryInfo folder = new DirectoryInfo(path);
FileInfo[] files = folder.GetFiles("*.txt", SearchOption.AllDirectories);
foreach (FileInfo file in files)
{
using (StreamReader sr = file.OpenText())
{
while (sr.Peek() > 0)
{
temp = sr.ReadLine();
if (selectRule(temp))
{
text.AppendLine(temp);
}
}
}
}
return text.ToString();
}
}
}