1

I want to get answer if I can replace old files or not - if files are older I don't want to copy. The problem is - the count of files in every directory can be different.

I don't want to write many methods, just one simply LINQ query, but I am rather weak in LINQ.

I would like to return true or false using LINQ.

System.IO.FileInfo[] filesSource = new System.IO.DirectoryInfo(sources).GetFiles();  
System.IO.FileInfo[] filesTarget = new System.IO.DirectoryInfo(target).GetFiles();  

bool canCopy = ... 
    /* group - if file have the same name
       if can't match and group - simply ignore it */ ...
(x => x.Source.LastWriteTime < x.Target.LastWriteTime).Count() == 0;
4

4 回答 4

1

This is the best i can come up with

Linq Way :

System.IO.FileInfo[] filesSource = new System.IO.DirectoryInfo(source).GetFiles();
            System.IO.FileInfo[] filesTarget = new System.IO.DirectoryInfo(dest).GetFiles();

bool canCopy = !(from fileInfo in filesSource 
                 let tmp = filesTarget.FirstOrDefault(f => f.Name == fileInfo.Name) 
                 where tmp != null && tmp.LastWriteTime > fileInfo.LastWriteTime 
                 select fileInfo).Any();

Normal Way :

private static bool CanCopyAllFiles(string source, string dest)
{
    System.IO.FileInfo[] filesSource = new System.IO.DirectoryInfo(source).GetFiles();
    System.IO.FileInfo[] filesTarget = new System.IO.DirectoryInfo(dest).GetFiles();

    foreach (FileInfo fileInfo in filesSource)
    {
        FileInfo tmp = filesTarget.FirstOrDefault(f => f.Name == fileInfo.Name);
        if (tmp != null && tmp.LastWriteTime > fileInfo.LastWriteTime)
        {
            return false;
        }
    }
    return true;
}
于 2013-04-17T17:09:27.703 回答
1

So the first thing you need to do is to join the two collections of files. You need to match up each source file with it's destination file (if any). This is done with a GroupJoin (as opposed to a regular join as we want to return items with no matching destination item).

Once we've done the join then we can filter out the items that both have a destination file, and for which the destination file is more recent.

public static IEnumerable<FileInfo> FilesToCopy(DirectoryInfo source, DirectoryInfo target)
{
    return from sourceFile in source.GetFiles()
            join targetFile in target.GetFiles()
            on sourceFile.FullName equals targetFile.FullName
            into destinationFiles
            let targetFile = destinationFiles.FirstOrDefault()
            where !destinationFiles.Any() ||
                sourceFile.LastWriteTime > targetFile.LastWriteTime
            select sourceFile;
}
于 2013-04-17T17:20:50.643 回答
0

If you are looking to do it in linq you can join your lists on name and then compare LastWriteTime between Source and Target...

        var filesToCopy = filesSource
            .GroupJoin(filesTarget, source => source.Name, target => target.Name,
                  (source, target) => new { Source = source, Target = target })
            .Where(x => !x.Target.Any() || x.Target.First().LastWriteTime < x.Source.LastWriteTime);
        foreach (var file in filesToCopy)
        {
            // copy file
        }
于 2013-04-17T17:15:33.233 回答
-1

Your question is not very clear but this copies files that are newer in the source

foreach (var target in from target in filesTarget from source in filesSource where target.LastWriteTime < source.LastWriteTime select target)
{
    // Copy
}
于 2013-04-17T17:03:14.157 回答