How to Copy (and increment) Multiple Instances of

2020-01-25 03:27发布

问题:

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