1

我有两个目录(dirAdirB),我想将所有文件从dirA 包括子文件夹复制到dirB其中的文件dirA替换文件的位置,前提是dirB这两个文件具有相同的名称(以及相同的子路径),并且fileB.LastWriteTime < fileA.LastWriteTime,否则,如果 .in 中的文件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后将是:

<directory Name="dirB">

   <file Name="file1" LastWriteTime="10:00" />
   <file Name="file2" LastWriteTime="13:00" />
   <file Name="file7" LastWriteTime="12:00" />

   <directory Name="subDir">
      <file Name="file3" LastWriteTime="10:00" />
      <file Name="file4" LastWriteTime="12:00" />
   </directory>

   <directory Name="BsubDir2">
      <file Name="file5" LastWriteTime="09:00" />
   </directory>

   <directory Name="subDir2">
      <file Name="file5" LastWriteTime="10:00" />
      <file Name="file6" LastWriteTime="11:00" />
   </directory>

</directory>

谢谢。

4

2 回答 2

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

现在该睡觉了。博纳诺特。

于 2012-05-27T23:09:47.267 回答
1

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

这是一般算法。伪鳕鱼。这不会编译。

// 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。

于 2012-05-27T22:22:11.050 回答