How to Copy (and increment) Multiple Instances of a File Using Batch File

徘徊边缘 提交于 2019-11-26 10:03:49

问题


I need to create a batch file that copies a file and increments it upon placing it at the destination. Example.

copy C:\\TEMP\\MyDoc.txt E:\\MyData\\

Essentially, I need this copy command to copy every time I start it (which it does now just fine). I would like it to increment the file name instead of overwrite it though. If I ran this three times or 100 times (never a certain number) I would like to see on the \"MyData\" folder:

MyDoc.txt

MyDoc(1).txt

...

Or Copy (1) I\'m not really sure what the syntax is for a duplicated file nor do I necessarily care. I just want to ensure that I\'m not overwriting the pre-existing file on my jump drive.

The catch is I\'m doing this on an Allen Bradley PanelView Plus that is old and running Windows CE. Any help would be greatly appreciated.


回答1:


You can try like this :

@echo off
set Source=C:\TEMP\MyDoc.txt
set Destination=E:\MyData\
set Filename=MyDoc
set a=1

:loop
if exist %Destination%\%Filename%(%a%).txt set /a a+=1 && goto :loop
copy %Source% %Destination%\%Filename%(%a%).txt
pause


来源:https://stackoverflow.com/questions/28697436/how-to-copy-and-increment-multiple-instances-of-a-file-using-batch-file

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