制作一个在发生名称冲突时保留两个文件的批处理文件

制作一个在发生名称冲突时保留两个文件的批处理文件

问题描述:

有什么方法可以制作一个在名称冲突时不会覆盖现有文件,而是将文件的两个副本都放在同一路径中的批处理文件?

Is there any way to make a batch file that does not overwrite an existing file when there is a name conflict, but instead keeps both copies of the file in the same path?

下面的批处理文件就像COPY命令一样,只有一个文件.如果该文件已存在于目标文件夹中,则将括号中的数字添加到新文件中以保留两个文件.

The Batch file below works like COPY command with just one file. If the file already exist in target folder, a number in parentheses is added to new file in order to keep both files.

@echo off
Rem mycopy sourceFile targetDir
Set targetName=%~1
Set i=0
:nextName
   If not exist "%~2/%targetName%" goto copy
   Set /A i+=1
   Set targetName=%~1 (%i%)
   Goto nextName
:copy
Copy %1 "%~2/%targetName%"