答案1
这是一个 C# 程序,它能完成您所要求的事情。我实际上不能 100% 确定您想要什么。
用法是:program.exe "outputfolder" "file1.txt" "file2.txt" "file3.txt"
它会重写输出文件夹中列出的文件,并按指定的顺序进行处理。如果之前在任何行或文件中遇到过用户名,它会跳过该行。它不会以任何方式检查电子邮件或电话号码。
using System;
using System.Collections.Generic;
using System.IO;
namespace CreateUniqueFile
{
class Program
{
static void Main(string[] args)
{
string fullpath;
string outpath;
List<string> files = new List<string>();
for (int i = 1; i < args.Length; i++)
{
fullpath = Path.GetFullPath(args[i]);
if (!File.Exists(fullpath))
Console.WriteLine("File not found: {0}", fullpath);
else
{
files.Add(fullpath);
Console.WriteLine(fullpath);
}
}
if (files.Count == 0)
{
Console.WriteLine("No files provided!");
return;
}
else
{
outpath = Path.GetFullPath(args[0]);
Console.WriteLine("Output will go to folder: \"{0}\"", outpath);
Console.WriteLine("Process files in the above order? (Y/N)");
bool yes = false;
while (!yes)
{
switch (Console.ReadKey().Key)
{
case ConsoleKey.Y:
yes = true;
break;
case ConsoleKey.N:
return;
default:
break;
}
}
}
if (!Directory.Exists(outpath))
Directory.CreateDirectory(outpath);
HashSet<string> seennames = new HashSet<string>();
string line, username;
foreach (string path in files)
{
string writepath = outpath + '\\' + Path.GetFileName(path);
if (File.Exists(writepath))
{
writepath = outpath + '\\' + Path.GetFileNameWithoutExtension(path) + " (2)" + Path.GetExtension(path);
// Dodgy thing to increment the number, don't touch!
while (File.Exists(writepath))
{
writepath = writepath.Substring(0, writepath.LastIndexOf('(') + 1) +
(Int32.Parse(writepath.Substring(writepath.LastIndexOf('(') + 1, writepath.LastIndexOf(')') - writepath.LastIndexOf('(') - 1)) + 1) +
writepath.Substring(writepath.LastIndexOf(')'));
}
}
using (StreamWriter writer = new StreamWriter(writepath))
{
using (StreamReader reader = new StreamReader(path))
{
while (!reader.EndOfStream)
{
line = reader.ReadLine();
username = line.Split('-')[0];
if (!seennames.Contains(username))
{
writer.WriteLine(line);
seennames.Add(username);
}
}
Console.WriteLine("{0} processed, output to {1}", path, writepath);
}
}
}
}
}
}