拷贝整个目录

' Usage:
' Copy Recursive with Overwrite if exists.
' RecursiveDirectoryCopy("C:/Data", "D:/Data", True, True)
' Copy Recursive without Overwriting.
' RecursiveDirectoryCopy("C:/Data", "D:/Data", True, False)
' Copy this directory Only. Overwrite if exists.
' RecursiveDirectoryCopy("C:/Data", "D:/Data", False, True)
' Copy this directory only without overwriting.
' RecursiveDirectoryCopy("C:/Data", "D:/Data", False, False)

' Recursively copy all files and subdirectories from the specified source to the specified
' destination.
Private Sub RecursiveDirectoryCopy(ByVal sourceDir As String, ByVal destDir As String, ByVal fRecursive As Boolean, ByVal overWrite As Boolean)
    Dim sDir As String
    Dim dDirInfo As IO.DirectoryInfo
    Dim sDirInfo As IO.DirectoryInfo
    Dim sFile As String
    Dim sFileInfo As IO.FileInfo
    Dim dFileInfo As IO.FileInfo
    ' Add trailing separators to the supplied paths if they don't exist.
    If Not sourceDir.EndsWith(System.IO.Path.DirectorySeparatorChar.ToString()) Then
        sourceDir &= System.IO.Path.DirectorySeparatorChar
    End If
    If Not destDir.EndsWith(System.IO.Path.DirectorySeparatorChar.ToString()) Then
        destDir &= System.IO.Path.DirectorySeparatorChar
    End If
    'If destination directory does not exist, create it.
    dDirInfo = New System.IO.DirectoryInfo(destDir)
    If dDirInfo.Exists = False Then dDirInfo.Create()
    dDirInfo = Nothing
    ' Recursive switch to continue drilling down into directory structure.
    If fRecursive Then
        ' Get a list of directories from the current parent.
        For Each sDir In System.IO.Directory.GetDirectories(sourceDir)
            sDirInfo = New System.IO.DirectoryInfo(sDir)
            dDirInfo = New System.IO.DirectoryInfo(destDir & sDirInfo.Name)
            ' Create the directory if it does not exist.
            If dDirInfo.Exists = False Then dDirInfo.Create()
            ' Since we are in recursive mode, copy the children also
            RecursiveDirectoryCopy(sDirInfo.FullName, dDirInfo.FullName, fRecursive, overWrite)
            sDirInfo = Nothing
            dDirInfo = Nothing
        Next
    End If
    ' Get the files from the current parent.
    For Each sFile In System.IO.Directory.GetFiles(sourceDir)
        sFileInfo = New System.IO.FileInfo(sFile)
        dFileInfo = New System.IO.FileInfo(Replace(sFile, sourceDir, destDir))
        'If File does not exist. Copy.
        If dFileInfo.Exists = False Then
            sFileInfo.CopyTo(dFileInfo.FullName, overWrite)
        Else
            'If file exists and is the same length (size). Skip.
            
'If file exists and is of different Length (size) and overwrite = True. Copy
            If sFileInfo.Length <> dFileInfo.Length AndAlso overWrite Then
                sFileInfo.CopyTo(dFileInfo.FullName, overWrite)
            'If file exists and is of different Length (size) and overwrite = False. Skip
            ElseIf sFileInfo.Length <> dFileInfo.Length AndAlso Not overWrite Then
                Debug.WriteLine(sFileInfo.FullName & " Not copied.")
            End If
        End If
        sFileInfo = Nothing
        dFileInfo = Nothing
    Next
End Sub

'调用
RecursiveDirectoryCopy("C:/test1", "D:/test1", True, True)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Java中拷贝整个目录并改名,你可以使用递归的方式来实现。下面是一个示例代码,演示了如何拷贝整个目录并将其重命名: ```java import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardCopyOption; public class DirectoryCopyExample { public static void main(String[] args) { String sourcePath = "/path/to/source/directory"; String destinationPath = "/path/to/destination/directory"; String newDirectoryName = "new_directory_name"; try { copyDirectory(new File(sourcePath), new File(destinationPath, newDirectoryName)); System.out.println("Directory copied successfully."); } catch (IOException e) { System.out.println("Failed to copy directory: " + e.getMessage()); } } private static void copyDirectory(File source, File destination) throws IOException { if (!source.isDirectory()) { throw new IllegalArgumentException("Source is not a directory."); } if (!destination.exists()) { destination.mkdirs(); } File[] files = source.listFiles(); if (files != null) { for (File file : files) { Path sourcePath = file.toPath(); Path destinationPath = new File(destination, file.getName()).toPath(); Files.copy(sourcePath, destinationPath, StandardCopyOption.REPLACE_EXISTING); if (file.isDirectory()) { copyDirectory(file, new File(destination, file.getName())); } } } } } ``` 请替换 `sourcePath` 和 `destinationPath` 变量的值为你实际的源目录和目标目录路径,以及 `newDirectoryName` 为你想要的新目录名称。运行代码后,源目录将被拷贝到目标目录,并且以新的目录名称命名。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值