【问题标题】:File Copy - Keep both files if name conflicts Windows文件复制 - 如果名称冲突,保留两个文件 Windows
【发布时间】:2017-09-08 16:08:30
【问题描述】:

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

我需要一种方法从一个命令提示符会话中执行此操作

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

【问题讨论】:

    标签: windows command-line


    【解决方案1】:

    你可以从一个快速的谷歌上找到脚本,你可以放入一个 bat 文件并处理这个问题,但我建议你研究一下 powershell 来处理这个问题。

    $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
    

    如果你愿意,你可以从命令行调用 powershell,所以这个答案在技术上符合要求。如果您想对复制操作进行任何其他更改,Powershell 将使这些类型的任务更容易并为您提供更大的灵活性。

    How to Copy Individual Files and Rename Duplicates with Powershell

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-04
      • 1970-01-01
      • 1970-01-01
      • 2019-01-23
      • 2012-02-24
      • 1970-01-01
      • 1970-01-01
      • 2010-12-13
      相关资源
      最近更新 更多