1

How do I copy a certain file to a destination folder that already has the same file with the same name, keep both files.

For eg. if a.jpg is already present in the destination folder (assume one in number), now there would two files with different names (eg. a.jpg and a(1).jpg

pnkjmndhl
  • 565
  • 3
  • 7
  • 21
  • Check [this page](https://stackoverflow.com/questions/5248393/windows-batch-file-to-copy-and-keep-duplicates) – JoSerra Sep 25 '17 at 04:24
  • You can get inspired with this batch file : [Incremental_Copy.bat](https://pastebin.com/31auQeFz) – Hackoo Sep 25 '17 at 08:11
  • Possible duplicate of [Windows batch file to copy and keep duplicates](https://stackoverflow.com/questions/5248393/windows-batch-file-to-copy-and-keep-duplicates) – Compo Sep 25 '17 at 11:55
  • @JoSerra that threat is different. I already have file in destination and I want to copy a new file with the same name to that destination. – pnkjmndhl Sep 25 '17 at 20:19
  • @Hackoo I am looking for a simple solution to this question. – pnkjmndhl Sep 25 '17 at 20:19
  • @Compo the topic looks the same but they are completely different. – pnkjmndhl Sep 25 '17 at 20:20
  • 1
    @pnkjmndhl here is a simple solution [How to Copy (and increment) Multiple Instances of a File Using Batch File](https://stackoverflow.com/questions/28697436/how-to-copy-and-increment-multiple-instances-of-a-file-using-batch-file?answertab=active#tab-top), just show us your work on it – Hackoo Sep 25 '17 at 20:35

2 Answers2

0

We can also use TIMESTAMP:

@echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%" & set "MS=%dt:~15,3%"
set "datestamp=%YYYY%%MM%%DD%" & set "timestamp=%HH%%Min%%Sec%" & set "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%-%MS%"
echo datestamp: "%datestamp%"
echo timestamp: "%timestamp%"
echo fullstamp: "%fullstamp%"
Xcopy /s "D:\folder1\test.xls" "D:\folder2\test_%fullstamp%.xls"
pause
Neuron
  • 5,141
  • 5
  • 38
  • 59
0

Here's the solution which every one wanted but only few will get (precisely those which looked up this page). @Hackoo I kiss your feet.

Create a batch file named easycopy.bat then put the following in it:

@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