4

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.

Dustin11h3
  • 57
  • 1
  • 1
  • 5
  • The answer is on this page: https://stackoverflow.com/questions/46397166/copy-file-to-destination-folder-and-keep-duplicates/61986897#61986897 – Toten Hosen May 24 '20 at 13:54

2 Answers2

5

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
Hackoo
  • 18,337
  • 3
  • 40
  • 70
  • I'm not having any luck using this code. I keep getting an error that says: "|: incorrect command syntax" – Dustin11h3 Feb 24 '15 at 20:23
  • 1
    Update: I ran this on my laptop and it worked just fine. For whatever reason, when I run this on the Windows CE machine, it does not like the code. Thanks Hackoo for the help. I have utilized a different method to get done what I needed. The code was simple enough (as I have a slight programming background) I just couldn't figure out how to do that in batch. Thanks again. – Dustin11h3 Feb 25 '15 at 15:25
0

Here's some spanking code:

Put into a file named easycopy.bat and use like,
'easycopy SourceDirectory DestinationDirectory'.

@rem easycopy
@rem Usage: easycopy SourcePath TargetPath (SourcePath can be the path to a directory or a single file)
@rem release 24/05/2020
@echo off

setlocal enableDelayedExpansion

rem Initialize and validate arguments
if "%~2" equ "" echo Error: Insufficient arguments>&2&exit /b 1
set "source=%1"
set "target=%2"
set /a counter=0
if not exist %target%\ echo Error: Target folder %target% does not exist>&2&exit /b 1

if not exist %source%\ call :newfile %source% %target% & set /a counter+=1 & goto :end

rem Do the work
for /r %source% %%F in (*) do if "%%~dpF" neq %target%\ (
  if exist %target%\"%%~nxF" (
    call :newfile "%%F" %target% & set /a counter+=1
  ) else copy "%%F" %target% >nul & set /a counter+=1
)

:end
echo.
if %errorlevel% EQU 0 echo %counter% file/s was/were copied.
if %errorlevel% GTR 0 echo Check if something went wrong.
goto :eof

:newfile <Source> <Destination>
set Source=%1
set Destination=%2
set Filename=%~n1
set Extention=%~x1
set a=1
:loop
if not exist %Destination%\"%Filename%%Extention%" copy %Source% %Destination%\"%Filename%%Extention%" >nul & goto :eof
if exist %Destination%\"%Filename%(%a%)%Extention%" set /a a+=1 && goto :loop
copy %Source% %Destination%\"%Filename%(%a%)%Extention%" >nul