发生名称冲突并重命名文件时保留两个文件的批处理文件
我是创建.bat文件的新手.我正在尝试创建一个批处理文件,该文件将文件从一个目录移动或复制到另一个目录,如果目标目录中存在该文件,它将用(1)重命名该文件.下面是我要复制的.bat文件的内容,但它将覆盖目标目录中的文件.
I am new to creating .bat files. I am trying to create a batch file that will move or copy file(s) from one directory to another, and if the file(s) exists in the destination directory it will rename the file with a (1). Below is what I have for a .bat file to copy but it overwrites the file in the destination directory.
copy /-y "C:\Users\Private\Desktop\Move-Copy\1\1.txt" "C:\Users\Private\Desktop\Move-Copy\2"
pause
以下脚本提供了子例程:SUB_COPY
,该子例程可以执行您想要的操作,即在不覆盖现有文件的情况下复制文件.但会生成新的唯一名称,以防万一.该例程需要两个命令行参数:第一个是源项目的路径(支持通配符?
和 *
;也允许使用目录),第二个是目标(目标)目录的路径.所以这是脚本:
The following script provides a sub-routine :SUB_COPY
, which can do what you want, namely copying files without overwriting already existing ones but generating new unique names in case. The routine expects two command line arguments: the first one is the path to the source item(s) (wildcards ?
and *
are supported; a directory is allowed too), the second one is the path to the target (destination) directory. So here is the script:
@echo off
call :SUB_COPY "C:\Users\Private\Desktop\Move-Copy\1\1.txt" "TARGET=C:\Users\Private\Desktop\Move-Copy\2"
exit /B
:SUB_COPY source_item target_dir
if not "%~3"=="" (
>&2 echo ERROR: too many arguments!
)
set "SOURCE=%~f1"
if not defined SOURCE (
>&2 echo ERROR: missing argument!
exit /B 1
) else if not exist "%SOURCE%" (
>&2 echo ERROR: source not found!
exit /B 1
)
set "TARGET=%~f2"
if not defined TARGET set "TARGET=."
for /F "delims=" %%I in ('
2^> nul xcopy /L /Y /I "%SOURCE%" "%TEMP%\" ^| findstr /V /R "^[0-9]"
') do (
call :SUB_DO "%%~I" "%TARGET%"
)
exit /B
:SUB_DO source_file target_dir
set "SUFFIX="
:CHECK
if exist "%~f2\%~n1%SUFFIX%%~x1" (
set /A SUFFIX-=1
) else (
2> nul md "%~f2"
> nul 2>&1 copy /B "%~f1" "%~f2\%~n1%SUFFIX%%~x1" && (
echo INFO: copied "%~nx1" as "%~n1%SUFFIX%%~x1".
) || (
>&2 echo ERROR: copying of "%~nx1" failed!
exit /B 1
)
exit /B
)
goto :CHECK
首先,子例程:SUB_COPY
检查提供的参数的有效性.然后,有一个 for/F
循环,它枚举了源项,因此可以解析目录和通配符?
和 *
(以防万一)xcode/L 的输出,仅用于解析源并返回单个文件(非递归); /L
告诉您不要复制任何内容,而仅列出文件).在此循环内,调用了另一个子例程:SUB_DO
,该例程复制单个文件.此例程检查目标目录中是否存在相同名称的文件,如果存在,则在文件名后附加后缀 -1
;然后重复检查,如果仍然找到文件,则后缀变为 -2
,依此类推,直到找到新的文件名为止,该文件名在目标目录中不存在,依此类推.复制实际上是执行的.
Firstly, the sub-routine :SUB_COPY
checks the provided arguments for validity. Afterwards there is a for /F
loop which enumerates the source items, so to resolve directories and wildcards ?
and *
in case (this loop parses the output of xcopy /L
, just to resolve the source and return individual files (non-recursively); /L
tells to not copy anything but list the files only). Inside of this loop, another sub-routine :SUB_DO
is called, which does the copying of a single file; this routine checks whether a file with the same name exists in the target directory, and if so, a suffix -1
is appended to the file name; the check is repeated then, and if still a file is found, the suffix becomes -2
, and so on, until a new file name is found, which does not occur in the target directory, and so the copying is actually performed.