在文件夹之间更新文件的最佳方法

时间:2012-05-27 22:12:37

标签: .net filesystems directory copy updates

我有两个目录(dirAdirB),我想将dirA 的所有文件(包括子文件夹)复制到{{1} } dirB中的文件只有当两个文件具有相同的名称(以及相同的子路径)和dirA时才替换dirB中的文件,否则如果是文件fileB.LastWriteTime < fileA.LastWriteTime中没有显示dirA

所以,如果我有这些目录:

dirB

更新后<directory Name="dirA"> <file Name="file1" LastWriteTime="10:00" /> <file Name="file2" LastWriteTime="11:00" /> <file Name="file7" LastWriteTime="12:00" /> <directory Name="subDir"> <file Name="file3" LastWriteTime="10:00" /> <file Name="file4" LastWriteTime="11:00" /> </directory> <directory Name="subDir2"> <file Name="file5" LastWriteTime="10:00" /> <file Name="file6" LastWriteTime="11:00" /> </directory> </directory> <directory Name="dirB"> <file Name="file1" LastWriteTime="09:00" /> <file Name="file2" LastWriteTime="13:00" /> <directory Name="subDir"> <file Name="file3" LastWriteTime="09:00" /> <file Name="file4" LastWriteTime="12:00" /> </directory> <directory Name="BsubDir2"> <file Name="file5" LastWriteTime="09:00" /> </directory> </directory> 将是:

dirB

感谢。

2 个答案:

答案 0 :(得分:2)

这是您的解决方案。尝试用一些文件测试, 工作得很好,但我认为需要更多的测试

Private Sub UpdateRec(Src As String, Dest As String, IsTest As Boolean)

    ' Update files in dest dir 
    For Each fileSrc As String In Directory.GetFiles(Src)
        Dim srcInfo As New FileInfo(fileSrc)
        Dim destFile As String = Path.Combine(Dest, srcInfo.Name)
        Dim foundFile As Boolean = File.Exists(destFile)
        if foundFile = true then
            Dim destInfo As New FileInfo(destFile)
          ' Copy only if newer 
          If Not IsTest AndAlso destInfo.LastWriteTime < srcInfo.LastWriteTime Then
              File.Copy(srcInfo.FullName, destInfo.FullName, True)
          End If
        else
        'File not found in dest copy always
        If Not IsTest Then File.Copy(srcInfo.FullName, destFile)
        end if
    Next

    ' Update folder in dest
    For Each dirSrc As String In Directory.GetDirectories(Src)
        Dim srcInfo As New DirectoryInfo(dirSrc)
        Dim dstDir As String = Path.Combine(Dest, srcInfo.Name)
        Dim foundDir As Boolean = Directory.Exists(dstDir)
        ' Not found, create 
        if foundDir = false then
         Directory.CreateDirectory(dstDir)
        end if
        ' recurse to copy file and other subdirs
        Call Me.UpdateRec(dirSrc, dstDir, IsTest)
    Next
End Sub

现在是时候睡觉了。的Buonanotte。

答案 1 :(得分:1)

相对于两个位置的根路径的简单递归。

这是一般算法。 Psuedocodish。这不会编译。

// To copy left-to-right:
void CopyFrom(DirectoryInfo left, DirectoryInfo right) {
    foreach(FileInfo leftFile in left.Files) {
        if( !rightContainsFile( leftFile.Name ) ) {
            leftFile.CopyTo( Path.Combine( right.FullPath, leftFile.Name );
        }//if
    }//foreach

    foreach(DirectoryInfo leftDir in left.Directories) {
        DirectoryInfo rightChild;
        if( (rightChild = rightContainsDirectory( leftDir.Name ) ) == null ) {
            // right does not contain child of the same name
            // so create it
            rightChild = Directory.Create( Path.Combine( right.FullPath, leftChild.Name );
        }
        // recursive copy:
        CopyFrom( leftChild, rightChild );
    } //foreach

}//void

需要一些装配。 HTH。