批量复制所有文件而不覆盖

时间:2013-03-20 02:20:41

标签: batch-file copy cmd

set dSource=C:\Games\Steam\steamapps
set dTarget=E:\Demos
set fType=*.dem

xcopy/i "%dSource%\%fType%" "%dTarget%"

这是我目前要复制的所有文件,但是如何让它复制所有文件并重命名任何具有相同名称的文件,以便将两个副本保存在目标文件夹中。

例如:
来源:demo.dem
    目的地:demo.dem

转到:
目的地:demo.dem,demo(1).dem

2 个答案:

答案 0 :(得分:2)

使用copy

尝试此解决方案
@echo off &setlocal
set "dSource=C:\Games\Steam\steamapps"
set "dTarget=E:\Demos"
set "fType=*.dem"

for %%i in ("%dSource%\%fType%") do if not exist "%dtarget%\%%~nxi" (copy /b "%%~i" "%dtarget%") else call :process "%%~i"
goto :eof

:process
set /a cnt=-1
:loop
set /a cnt+=1
set "fname=%dtarget%\%~n1(%cnt%)%~x1"
if exist "%fname%" goto :loop
copy /b "%~1" "%fname%"
goto :eof

endlocal

答案 1 :(得分:0)

@ECHO OFF
SETLOCAL
SET source=c:\sourcedir
SET dest=c:\destdir
SET mask=*.*
FOR /f "delims=" %%i IN (
  ' dir /b /a-d "%source%\%mask%" '
  ) DO IF EXIST "%dest%\%%i" (
  SET "destfn="
  SET "sourcefn=%source%\%%i"
  FOR /l %%g IN (1,1,9) DO IF NOT DEFINED destfn IF NOT EXIST "%dest%\%%~ni(%%g)%%~xi" SET destfn=%dest%\%%~ni(%%g^)%%~xi
  IF NOT DEFINED destfn FOR /l %%g IN (10,1,99) DO IF NOT DEFINED destfn IF NOT EXIST "%dest%\%%~ni(%%g)%%~xi" SET destfn=%dest%\%%~ni(%%g^)%%~xi
  IF NOT DEFINED destfn FOR /l %%g IN (100,1,999) DO IF NOT DEFINED destfn IF NOT EXIST "%dest%\%%~ni(%%g)%%~xi" SET destfn=%dest%\%%~ni(%%g^)%%~xi
  IF NOT DEFINED destfn FOR /l %%g IN (1000,1,9999) DO IF NOT DEFINED destfn IF NOT EXIST "%dest%\%%~ni(%%g)%%~xi" SET destfn=%dest%\%%~ni(%%g^)%%~xi
  CALL :copyg
  ) ELSE (XCOPY "%source%\%%i" "%dest%\" >nul)
)
GOTO :eof

:copyg
IF DEFINED destfn (ECHO F|XCOPY "%sourcefn%" "%destfn%" >nul
) ELSE (ECHO "%sourcefn%" NOT copied - out of generation numbers
)
GOTO :eof

警告:发布后,程序将为XCOPY。

我建议您将XCOPY语句更改为ECHO ...和 >nul / ECHO F| 以检查{{1}首先会生成指令。

(&n; nul会抑制XCOPY条消息; copied强制ECHO F|要复制到目标文件,因为没有XCOPY切换允许这样做