File Copy - Keep both files if name conflicts Wind

2019-08-27 06:25发布

I am trying to copy files from one directory to another and if there is a file with that duplicate name save the file. Basically the copy but keep both files option in windows. How can i accomplish this from the windows command line?

I need a way to do this from one command prompt session

Directory path 1: "C:\Users\User 1" Directory path 2: "C:\Users\User 2"

1条回答
爷、活的狠高调
2楼-- · 2019-08-27 06:59

You can find scripts from a quick google that you could drop into a bat file and handle this but I would like to propose you look into powershell to handle this problem.

$SourceFile = "C:\Temp\File.txt"
$DestinationFile = "C:\Temp\NonexistentDirectory\File.txt"

If (Test-Path $DestinationFile) {
    $i = 0
    While (Test-Path $DestinationFile) {
        $i += 1
        $DestinationFile = "C:\Temp\NonexistentDirectory\File$i.txt"
    }
} Else {
    New-Item -ItemType File -Path $DestinationFile -Force
}

Copy-Item -Path $SourceFile -Destination $DestinationFile -Force

You could call powershell from the command line if you wanted to so this answer technically fits the requirement. Powershell will make these kinds of tasks easier and give you more flexibility if you want to make any other changes to the copy operation.

How to Copy Individual Files and Rename Duplicates with Powershell

查看更多
登录 后发表回答