我有一个提示文件路径的记事本文件,从这些文件路径我要将文件复制到文件夹但是同名文件被覆盖如何使用批处理代码重命名这些相同的文件名,以便所有文件都被复制到文件夹? ?
我编写的代码
@echo off
SET src=c:\link.txt
set dest=c:\files
FOR /F "delims=" %%a IN (%link%) DO COPY "%%a" "%dest%\%%~nxa" /-Y
pause
任何帮助改善这个???
答案 0 :(得分:0)
这样的事情应该有效。它会检查每个单独的文件并保持附加数字,直到确定唯一名称。
如果原始名称在dest
中不存在,则以下脚本将保留原始名称,但在将来的调用中,它将附加序列号。例如:
MyFile.txt
MyFile-2.txt
MyFile-3.txt
这并不考虑任何时间戳,而只是在确定第一个可用(未使用)文件名之前保持递增数字。
@echo off
SETLOCAL EnableExtensions
REM Source file which contains a list of full file names.
REM This filename should _not_ be in quotes.
SET src=c:\link.txt
REM Directory to copy the files to.
REM This should _not_ be in quotes.
REM Entire declaration wrapped in quotes to prevent accidental spaces on the end.
set "dest=c:\files"
FOR /F "usebackq tokens=* delims=" %%a IN ("%src%") DO (
ECHO Processing: %%a
CALL :DoCopy "%%a" 0
)
pause
ENDLOCAL
GOTO :EOF
:DoCopy
REM First parameter is the file name.
REM Second parameter is the number to append on matches.
SET /A Append=%~2+1
SET FileToCheck="%dest%\%~nx1"
IF NOT "%Append%"=="1" (
REM If we get here a duplicate name exists.
REM Append the number to the end.
SET FileToCheck="%dest%\%~n1-%Append%%~x1"
)
IF NOT EXIST %FileToCheck% (
REM Does not exist, copy it.
COPY "%~1" %FileToCheck% /-Y
GOTO :EOF
) ELSE (
REM File does exist.
REM Keep going until we have a unique value.
CALL :DoCopy "%~1" %Append%
GOTO :EOF
)