将文件复制到目标文件夹并保留重复项 [英] Copy file to destination folder and keep duplicates

查看:234
本文介绍了将文件复制到目标文件夹并保留重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将某个文件复制到已经具有相同名称的相同文件的目标文件夹中,请保留两个文件.

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

例如 如果目标文件夹中已经存在a.jpg(假设数量为一个),那么现在将有两个名称不同的文件(例如a.jpg和a(1).jpg

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

推荐答案

这是每个人都想要的解决方案,但只有少数人会得到(正是那些查找此页面的人). @Hackoo,我亲吻你的脚.

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.

创建一个名为easycopy.bat的批处理文件,然后将以下内容放入其中:

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

这篇关于将文件复制到目标文件夹并保留重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆