Search code examples
batch-filecopybackupwindows-ceincrement

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


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.


Solution

  • 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