파일이있는 폴더와 .txt
한 폴더에서 다른 폴더로 복사해야하는 파일 이름 및 사본 수 목록이있는 파일이 있습니다.
스크립트는 파일을 복사하지만 .txt
파일에 동일한 이름 의 파일이 두 개 있으면 이전 파일을 덮어 씁니다.
목록에는 다음이 있습니다.
file1.txt 1 file2.txt 1 file1.txt 3 file2.txt 2
다음을 달성하고 싶습니다.
file1.txt file2.txt file1(1).txt file1(2).txt file1(3).txt file2(1).txt
이것은 내가 지금까지 가지고있는 코드입니다.
@echo off
set Source=C:\Users\siddique.gaffar\Desktop\Artworks
set Target=C:\Users\siddique.gaffar\Desktop\Artworks Copy
set FileList=C:\Users\siddique.gaffar\Desktop\Artwork TXT File\Book1.txt
echo.
if not exist "%Source%" echo Source folder "%Source%" not found & goto Exit
if not exist "%FileList%" echo File list "%FileList%" not found & goto Exit
if not exist "%Target%" md "%Target%"
for /F "delims=" %%a in ('type "%FileList%"') do copy "%Source%\%%a" "%Target%"
:Exit
echo.
echo press the Space Bar to close this window.
pause > nul
다음은 트릭을 수행해야합니다.
@echo off
set Source=C:\Users\siddique.gaffar\Desktop\Artworks
set Target=C:\Users\siddique.gaffar\Desktop\Artworks Copy
set FileList=C:\Users\siddique.gaffar\Desktop\Artwork TXT File\Book1.txt
echo.
if not exist "%Source%" echo Source folder "%Source%" not found & goto Exit
if not exist "%FileList%" echo File list "%FileList%" not found & goto Exit
if not exist "%Target%" md "%Target%"
for /F "usebackq tokens=1-2" %%a in ("%FileList%") do call :CopyFile "%%a" %%b
:Exit
echo.
echo press the Space Bar to close this window.
pause > nul
exit /b 0
:CopyFile
:: first argument = filename
:: second argument = number of copies
REM A little trick that will put limit on 0 if second argument is empty or not a number
set secondarg=%~2
set /a limit=secondarg
REM if limit is invalid (not strict positive), exit the function
IF %limit% LEQ 0 (
echo Invalid number of copies
exit /b 1
)
IF NOT EXIST "%Target%\%~1" (
copy "%Source%\%~1" "%Target%"
IF %limit% LEQ 1 exit /b 0
set /a limit-=1
)
REM File already exists: search correct index for filename
set index=0
set "targetfile=%target%\%~n1"
set file_ext=%~x1
:following
set /a index+=1
Rem if file with index %index% already exists, go back to get following index
IF exist "%targetfile%(%index%).%file_ext%" goto :following
Rem we have the correct index, now we can copy
set /a limit=index+limit-1
FOR /L %%g IN (%index%,1,%limit%) DO copy "%Source%\%~1" "%targetfile%(%%g).%file_ext%"
exit /b 0
긴 파일 이름이있는 경우 또 다른 옵션 은 명령 의 출력을 분석하는 대신 루프 usebackq
에서 큰 따옴표를 사용 하고 경로를 for f
묶는 것입니다 type
.
이 함수 :CopyFile
는로 파일의 존재를 확인하고 IF EXIST
카운터를 사용하여 새 파일의 파일 이름에 대한 다음 색인을 찾습니다. 경로 조작 을 사용 하여 색인으로 새 파일 이름을 생성합니다.
편집 : 텍스트 파일에서 필요한 사본 수를 읽고 그 숫자를 :CopyFile
함수의 두 번째 인수로 지정할 수있는 가능성을 추가했습니다 . 숫자가 주어지지 않거나 숫자가 완전 양수가 아니면 (0보다 큼) 복사본을 만들지 않습니다.
추신 : %limit%
두 번째 인수가 비어있는 경우 0으로 설정되는 "작은 트릭" set
은 /a
플래그로 빈 변수를 0으로 대체 하기 때문에 작동합니다. 인수 변수를 직접 사용하는 경우에는 작동하지 않습니다.
set /a limit=%~2
cmd 파서가 %~2
빈 문자열로 대체 되고 플래그를 set /a limit=
사용하여 잘못된 할당 이 실행 되므로 두 번째 인수가 비어 있으면 오류가 발생합니다 /a
. 그러나 추가 변수를 대중 교통으로 사용하는 경우 :
set var=%~2
set /a limit=var
set
cmd 인터프리터가 아닌 변수 확장을 처리 하게 할 것 입니다. 는 set
것을 볼 var
(경우에 변수가 비어 %2
비어)를 0으로 대체한다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다