[英]Find new file in two folders with a cross check
I am trying to sort two folders in to a patched folder, finding which file is new in the new folder and marking it as new, so i can transfer that file only. 我正在尝试将两个文件夹归类到一个已修补的文件夹中,在新文件夹中找到哪个文件是新文件,并将其标记为新文件,因此我只能传输该文件。 i dont care about dates or hash changes. 我不在乎日期或哈希更改。 just what file is in the new folder that is not in the old folder. 就是新文件夹中没有旧文件夹中的文件。
somehow the line 线路
pf.NFile = !( oldPatch.FindAll(s => s.Equals(f)).Count() == 0);
is always returning false. 总是返回false。 is there something wrong with my logic of cross checking? 我的交叉检查逻辑有什么问题吗?
List<string> newPatch = DirectorySearch(_newFolder);
List<string> oldPatch = DirectorySearch(_oldFolder);
foreach (string f in newPatch)
{
string filename = Path.GetFileName(f);
string Dir = (Path.GetDirectoryName(f).Replace(_newFolder, "") + @"\");
PatchFile pf = new PatchFile();
pf.Dir = Dir;
pf.FName = filename;
pf.NFile = !( oldPatch.FindAll(s => s.Equals(f)).Count() == 0);
nPatch.Files.Add(pf);
}
foreach (string f in oldPatch)
{
string filename = Path.GetFileName(f);
string Dir = (Path.GetDirectoryName(f).Replace(_oldFolder, "") + @"\");
PatchFile pf = new PatchFile();
pf.Dir = Dir;
pf.FName = filename;
if (!nPatch.Files.Exists(item => item.Dir == pf.Dir &&
item.FName == pf.FName))
{
nPatch.removeFiles.Add(pf);
}
}
I don't have the classes you are using (like DirectorySearch
and PatchFile
), so i can't compile your code, but IMO the line _oldPatch.FindAll(...
doesn't return anything because you are comparing the full path ( c:\\oldpatch\\filea.txt
is not c:\\newpatch\\filea.txt
) and not the file name only. IMO your algorithm could be simplified, something like this pseudocode (using List.Contains
instead of List.FindAll
): 我没有您正在使用的类(例如DirectorySearch
和PatchFile
),所以我无法编译您的代码,但是IMO _oldPatch.FindAll(...
行不返回任何内容,因为您正在比较完整路径( c:\\oldpatch\\filea.txt
不是c:\\newpatch\\filea.txt
,也不只是文件名c:\\newpatch\\filea.txt
可以简化您的算法,类似于此伪代码 (使用List.Contains
代替List.FindAll
):
var _newFolder = "d:\\temp\\xml\\b";
var _oldFolder = "d:\\temp\\xml\\a";
List<FileInfo> missing = new List<FileInfo>();
List<FileInfo> nPatch = new List<FileInfo>();
List<FileInfo> newPatch = new DirectoryInfo(_newFolder).GetFiles().ToList();
List<FileInfo> oldPatch = new DirectoryInfo(_oldFolder).GetFiles().ToList();
// take all files in new patch
foreach (var f in newPatch)
{
nPatch.Add(f);
}
// search for hits in old patch
foreach (var f in oldPatch)
{
if (!nPatch.Select (p => p.Name.ToLower()).Contains(f.Name.ToLower()))
{
missing.Add(f);
}
}
// new files are in missing
One possible solution with less code would be to select the file names, put them into a list an use the predefined List.Except or if needed List.Intersect methods. 一种使用较少代码的可能解决方案是选择文件名,使用预定义的List.Except或必要时使用List.Intersect方法将它们放入列表中。 This way a solution to which file is in A
but not in B
could be solved fast like this: 这样,可以像这样快速解决A
而不是B
文件的解决方案:
var locationA = "d:\\temp\\xml\\a";
var locationB = "d:\\temp\\xml\\b";
// takes file names from A and B and put them into lists
var filesInA = new DirectoryInfo(locationA).GetFiles().Select (n => n.Name).ToList();
var filesInB = new DirectoryInfo(locationB).GetFiles().Select (n => n.Name).ToList();
// Except retrieves all files that are in A but not in B
foreach (var file in filesInA.Except(filesInB).ToList())
{
Console.WriteLine(file);
}
I have 1.xml, 2.xml, 3.xml
in A
and 1.xml, 3.xml
in B
. 我在A
有1.xml, 2.xml, 3.xml
1.xml, 3.xml
在B
中A
1.xml, 3.xml
。 The output is 2.xml
- missing in B
. 输出为2.xml
- B
缺少。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.