Copy only new or modified files to Directory (Folder) using C# and VB.Net

Last Reply 6 months ago By pandeyism

Posted 6 months ago

I know this has probably been done and discussed umpteen times, but I can’t find the answer I am looking for.

I am trying to create a simple “directory/file copy" console application in C#. What I need is to copy all folders and files (keeping the original hierarchy) from one drive to another, like from drive C:\Data to drive E:\Data.

However, I only want it to copy any NEW or MODIFIED files from the source to the destination.

If the file on the destination drive is newer than the one on the source drive, then it does not copy.

(the problem)

In the code I have, it's comparing file "abc.pdf" in the source with file "xyz.pdf" in the destination and thus is overwriting the destination file with whatever is in the source even though the destination file is newer. I am trying to figure out how to make it compare "abc.pdf" in the source to "abc.pdf" in the destination.

 This works if I drill the source and destination down to a specific file, but when I back out to the folder level, it overwrites the destination file with the source file, even though the destination file is newer.

(my solutions – that didn’t work)

I thought by putting the “if (file.LastWriteTime > destination.LastWriteTime)” after the “foreach” command, that it would compare the files in the two folders, File1 source to File1 destination, but it’s not.

It seems I’m missing something in either the “FileInfo[]”, “foreach” or “if” statements to make this a one-to-one comparison. I think maybe some reference to the “Path.Combine” statement or a “SearchOption.AllDirectories”, but I’m not sure.

Any suggestions?

As you can see from my basic code sample, I'm new to C# so please put your answer in simple terms.

Thank you.

Here is the code I have tried, but it’s not working.

class Copy  
{  
    public static void CopyDirectory(DirectoryInfo source, DirectoryInfo destination)  
    {  
        if (!destination.Exists)  
        {  
            destination.Create();  
        }  
        // Copy files.  
        FileInfo[] files = source.GetFiles();  
        FileInfo[] destFiles = destination.GetFiles();  
        foreach (FileInfo file in files)  
            foreach (FileInfo fileD in destFiles)  
                // Copy only modified files    
                    if (file.LastWriteTime > fileD.LastWriteTime)  
                    {  
                        file.CopyTo(Path.Combine(destination.FullName,  
                        file.Name), true);  
                    }  

                // Copy all new files  
                else  
                if (!fileD.Exists)  
                {  
                    file.CopyTo(Path.Combine(destination.FullName, file.Name), true);  
                }  
        // Process subdirectories.  
        DirectoryInfo[] dirs = source.GetDirectories();  
        foreach (DirectoryInfo dir in dirs)  
        {  
            // Get destination directory.  
            string destinationDir = Path.Combine(destination.FullName, dir.Name);  
            // Call CopyDirectory() recursively.  
            CopyDirectory(dir, new DirectoryInfo(destinationDir));  
        }  
    }  
}  

 

You are viewing reply posted by: pandeyism 6 months ago.
Posted 6 months ago

Hi pailwriter,

Refer below sample.

Namespaces

C#

using System.IO;

VB.Net

Imports System.IO

Code

C#

static void Main(string[] args)
{
    string sourcePath = @"C:\Users\anand\Desktop\Test";
    string destinationPath = @"C:\Users\anand\Desktop\Test1";
    CopyDirectory(sourcePath, destinationPath);
}

public static void CopyDirectory(string sourcePath, string destinationPath)
{
    DirectoryInfo sourceDir = new DirectoryInfo(sourcePath);
    FileInfo[] sourceFiles = sourceDir.GetFiles();
    for (int source = 0; source < sourceFiles.Length; source++)
    {
        DirectoryInfo destinationDir = new DirectoryInfo(destinationPath);
        FileInfo[] destFiles = destinationDir.GetFiles();
        for (int destination = 0; destination < destFiles.Length; destination++)
        {
            if (File.Exists(Path.Combine(destinationPath, sourceFiles[source].Name)))
            {
                if (sourceFiles[source].Name == destFiles[destination].Name)
                {
                    if (sourceFiles[source].LastWriteTime > destFiles[destination].LastWriteTime)
                    {
                        sourceFiles[source].CopyTo(Path.Combine(destinationDir.FullName, sourceFiles[source].Name), true);
                    }
                }
            }
            else
            {
                sourceFiles[source].CopyTo(Path.Combine(destinationDir.FullName, sourceFiles[source].Name), true);
            }
        }
    }
}

VB.Net

Sub Main()
    Dim sourcePath As String = "C:\Users\anand\Desktop\Test"
    Dim destinationPath As String = "C:\Users\anand\Desktop\Test1"
    CopyDirectory(sourcePath, destinationPath)
End Sub

Public Sub CopyDirectory(ByVal sourcePath As String, ByVal destinationPath As String)
    Dim sourceDir As DirectoryInfo = New DirectoryInfo(sourcePath)
    Dim sourceFiles As FileInfo() = sourceDir.GetFiles()
    For source As Integer = 0 To sourceFiles.Length - 1
        Dim destinationDir As DirectoryInfo = New DirectoryInfo(destinationPath)
        Dim destFiles As FileInfo() = destinationDir.GetFiles()
        For destination As Integer = 0 To destFiles.Length - 1
            If File.Exists(Path.Combine(destinationPath, sourceFiles(source).Name)) Then
                If sourceFiles(source).Name = destFiles(destination).Name Then
                    If sourceFiles(source).LastWriteTime > destFiles(destination).LastWriteTime Then
                        sourceFiles(source).CopyTo(Path.Combine(destinationDir.FullName, sourceFiles(source).Name), True)
                    End If
                End If
            Else
                sourceFiles(source).CopyTo(Path.Combine(destinationDir.FullName, sourceFiles(source).Name), True)
            End If
        Next
    Next
End Sub