Make a Batch File That Keeps Both Files When a Name Conflict Occurs

纵然是瞬间 提交于 2020-01-13 20:30:25

问题


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?


回答1:


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%"


来源:https://stackoverflow.com/questions/14033582/make-a-batch-file-that-keeps-both-files-when-a-name-conflict-occurs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!