3
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

4

2 回答 2

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
于 2013-03-20T08:31:28.413 回答
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|以检查XCOPY将首先生成哪些指令。

(>nul 抑制copied消息;ECHO F|强制XCOPY复制到目标文件,因为没有XCOPY允许这样做的开关)

于 2013-03-20T03:33:48.873 回答