我想在多个文件中删除相同的行(58个html文件!)
例如,从所有文件中删除第56至305行和第314行至第320行。
怎么去?
答案 0 :(得分:1)
c#一次性计划。 (您可以免费获得Visual社区)
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DelteLines
{
class Program
{
static void Main(string[] args)
{
var dir = @"<your path to html files>";
var files = Directory.GetFiles(dir, "*.html", SearchOption.AllDirectories);
var excludeRanges = new List<Tuple<int, int>>();
excludeRanges.Add(new Tuple<int, int>(56, 305));
excludeRanges.Add(new Tuple<int, int>(314, 320));
foreach (var f in files)
{
var lines = File.ReadAllLines(f);
var newLines = new List<string>();
for (int i = 0; i < lines.Length; i++)
{
var lNumber = i + 1; //Assuming you count from 1 and not from 0 your lines
var toExclude = excludeRanges.Where(x => lNumber >= x.Item1 && lNumber <= x.Item2).Any();
if (toExclude)
{
continue;
}
newLines.Add(lines[i]);
}
File.WriteAllLines(f, newLines);
}
}
}
}