文件复制-如果名称与Windows冲突,则保留两个文件 [英] File Copy - Keep both files if name conflicts Windows

查看:429
本文介绍了文件复制-如果名称与Windows冲突,则保留两个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将文件从一个目录复制到另一个目录,如果存在具有相同名称的文件,请保存该文件。基本上是复制但在Windows中保留两个文件选项。我该如何从Windows命令行完成此操作?

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

目录路径1: C:\Users\User 1
目录路径2: C:\Users\User 2

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

推荐答案

您可以从快速的google中找到可以放入bat文件并进行处理的脚本,但我想建议您使用powershell来解决此问题。

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

您可以打电话给po如果需要,请从命令行提供wershell,以便此答案在技术上符合要求。如果您要对复制操作进行任何其他更改,Powershell将使这些类型的任务更加轻松,并为您提供更大的灵活性。

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.

如何使用Powershell复制单个文件并重命名重复项

这篇关于文件复制-如果名称与Windows冲突,则保留两个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆