简体   繁体   中英

copy files and directories recursively if files are newer (update routine)

My code is working so far very well. But unfortunately it came out that my Source-Directory could change its structure kind of dynamically. Is there any smaller and smarter code to get files and dirs recursive and copy them to the new dir? Do i have to separate files and directories?

Any advice would be really kind.

Thanks.

public void CopyDirFilesRecurse()
{
    // copy alle files from source (copy if newer or not existing)
    var files = Directory.GetFiles(@"\\cifsShare\source");
    var destDir = @"C:\newdir";
    foreach (var file in files)
    {
        FileInfo fileName = new FileInfo(file);
        FileInfo destFile = new FileInfo(Path.Combine(destDir, fileName.Name));
        if (destFile.Exists)
        {
            if (fileName.LastWriteTime > destFile.LastWriteTime)
            {
                fileName.CopyTo(destFile.FullName, true);
            }
        }
        else
        {
            fileName.CopyTo(destFile.FullName);
        }
    }

    // get all dirs (create if not existing
    //      copy all files from source)
    // if dir exist copy alle files from source (copy if newer or not existing)
    var directories = Directory.GetDirectories(@"\\cifsShare\source");
    foreach (var dir in directories)
    {
        DirectoryInfo dirName = new DirectoryInfo(dir);
        DirectoryInfo destDirName = new DirectoryInfo(Path.Combine(destDir, dirName.Name));
        if (!destDirName.Exists)
        {
            System.IO.Directory.CreateDirectory(destDirName.FullName);
            var filesInDir = Directory.GetFiles(dirName.FullName);
            var destDirPath = destDirName.FullName;
            foreach (var file in filesInDir)
            {
                FileInfo fileName = new FileInfo(file);
                FileInfo destFile = new FileInfo(Path.Combine(destDirPath, fileName.Name));
                if (!destFile.Exists)
                {
                    fileName.CopyTo(destFile.FullName);
                }
            }
        }
        else
        {
            var filesInDir = Directory.GetFiles(dirName.FullName);
            var destDirPath = destDirName.FullName;
            foreach (var file in filesInDir)
            {
                FileInfo fileName = new FileInfo(file);
                FileInfo destFile = new FileInfo(Path.Combine(destDirPath, fileName.Name));
                if (destFile.Exists)
                {
                    if (fileName.LastWriteTime > destFile.LastWriteTime)
                    {
                        fileName.CopyTo(destFile.FullName, true);
                    }
                }
                else
                {
                    fileName.CopyTo(destFile.FullName);
                }
            }
        }
    }
}

EDIT:

I think I am almost there. I found a way to get all files recursively from the source. My problem now is that my code is copying from the source-subdirs to my destination root-dir and not to the new destination-subdir.

It shall go like this:

C:\source\a.txt
C:\source\subidr1\b.txt
C:\source\subidr2\c.txt

to

C:\dest\a.txt
C:\dest\subidr1\b.txt
C:\dest\subidr2\c.txt

but it goes to

C:\dest\a.txt
C:\dest\b.txt
C:\dest\c.txt

here is my code now:

void Test()
        {                
            var sourcePath = @"C:\sourcedir";
            var destinationPath = @"C:\newdir";
            string[] allFilesSoure = Directory.GetFiles(sourcePath, "*.*", SearchOption.AllDirectories);
            foreach (var sourceFile in allFilesSoure)
            {
                FileInfo InfoSourceFile = new FileInfo(sourceFile);
                Debug.WriteLine(InfoSourceFile.FullName);
                var newDestinationPath = Path.Combine(destinationPath, InfoSourceFile.Name);
                string[] destinationFile = Directory.GetFiles(destinationPath, $"{InfoSourceFile.Name}", SearchOption.AllDirectories);
                if (destinationFile.Length == 1)
                {
                    FileInfo InfoDestinationFile = new FileInfo(destinationFile[0]);
                    if (InfoSourceFile.LastWriteTime > InfoDestinationFile.LastWriteTime)
                    {
                        InfoSourceFile.CopyTo(InfoDestinationFile.FullName, true);
                    }
                }
                else
                {
                    InfoSourceFile.CopyTo(newDestinationPath);
                }
            }
        }

To resolve copying source sub dirs to destination root dirs you can use something like this:

    public static void Copy(string sourceDir, string targetDir)
    {
        foreach (var file in Directory.GetFiles(sourceDir))
        {
            File.Copy(file, Path.Combine(targetDir, Path.GetFileName(file)));
        }
        foreach (var _sourceDir in Directory.GetDirectories(sourceDir))
        {
            var _targetPath = Path.Combine(
                        targetDir,
                        Path.GetFileName(_sourceDir)
                        );

            if (!Directory.Exists(_targetPath))
            {
                Directory.CreateDirectory(_targetPath);
            }
            FileOps.Copy(_sourceDir, _targetPath);
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM