2016-07-31 3 views
1

robocopy를 사용하여 파일을 백업하는 Windows 배치 스크립트를 작성하고 있습니다. 나는 기본적으로 대상 경로에 최상위 소스 디렉토리를 추가하는 linux rsync의 동작을 모방하려고합니다. 나는 그것이 소스의 서브 디렉토리만을 목적지에 복사하기 때문에 robocopy가 이것을 할 수 있다는 것을 알지 못한다. 따라서 다음 스크립트를 사용하여 경로에서 최상위 디렉토리를 추출하여 for 중첩 된 for 루프를 사용하여 대상 경로에 추가합니다.windows 배치 스크립트 : for 루프가 작동하지 않습니다.

아래 스크립트를 사용하여 작동하지 않는 부분에 초점을 맞추어 단순화했습니다.

"C:\Users\Sample User\Documents\keys" "C:\Users\Sample User\Documents\test\" 
"C:\Users\Sample User\Documents\Custom Office Templates" "C:\Users\Sample User\Documents\test\" 

을 % ddir % 변수가 비어 :

@echo off 
setlocal enabledelayedexpansion 

set source[0]=C:\Users\Sample User\Documents\keys 
set source[1]=C:\Users\Sample User\Documents\Custom Office Templates 

set dest=C:\Users\Sample User\Documents\test 

:: range in for loop specified as (start,step,end) 
for /l %%x in (0,1,1) do (
    for %%a in ("!source[%%x]!") do (
    set ddir=%%~nxa 
) 
    echo "!source[%%x]!" "%dest%\%ddir%" 
) 

스크립트는 다음과 같은 출력을 생성합니다. 따라서 소스 경로의 최상위 디렉토리가 의도 한대로 대상에 추가되지 않습니다. 나는 다음과 같은 출력을 달성하기 위해 싶습니다

"C:\Users\Sample User\Documents\keys" "C:\Users\Sample User\Documents\test\keys" 
"C:\Users\Sample User\Documents\Custom Office Templates" "C:\Users\Sample User\Documents\test\Custom Office Templates" 

나는 별도로 외부 및 루프에 대한 내부 테스트를 거친 후 일부 스크립트가 예상대로 작동합니다. 물론 inner for 루프는이 경우 전체 소스 배열에 액세스 할 수 없습니다. 루프

외부 :

@echo off 
setlocal enabledelayedexpansion 

set source[0]=C:\Users\Sample User\Documents\keys 
set source[1]=C:\Users\Sample User\Documents\Custom Office Templates 

:: range in for loop specified as (start,step,end) 
for /l %%x in (0,1,1) do (
    echo "!source[%%x]!" 
) 

출력 : 루프

"C:\Users\Sample User\Documents\keys" 
"C:\Users\Sample User\Documents\Custom Office Templates" 

내부 :

@echo off 
setlocal enabledelayedexpansion 

set source[0]=C:\Users\Sample User\Documents\keys 
set dest=C:\Users\Sample User\Documents\test 

for %%a in ("!source[0]!") do (
    set ddir=%%~nxa 
) 

출력 :

"C:\Users\Sample User\Documents\keys" "C:\Users\Sample User\Documents\test\keys" 

중첩 된 for 루프에 어떤 문제가 있습니까?

+0

이 ... – aschipfl

+0

'는 % ddir % 변수는 있기 때문에 empty.'됩니다 확장이 가능한'! ddir! '을 사용할 필요가있다. – TessellatingHeckler

+0

감사합니다. 이제 작동합니다. 나는 정말로 "delayedexpansion"개념을 윈도우 스크립팅의 개념으로 이해하지 못했다고 생각한다. – Pohl7534

답변

0

변수 및 경로 인수 확장의 지연된 확장을 올바르게 사용해야합니다. 이 시도하고 나에게 어떤 문제를 알려 :

당신은`echo` 명령 행에 변수`ddir`에 대해서도 지연 확장을 사용할 필요가
@echo off 
setlocal enabledelayedexpansion 

set "source[0]=C:\Users\Sample User\Documents\keys" 
set "source[1]=C:\Users\Sample User\Documents\Custom Office Templates" 
set "dest=C:\Users\Sample User\Documents\test" 

:: range in for loop specified as (start,step,end) 
for /l %%x in (0,1,1) do (
    for /f %%a in ("!source[%%x]!") do (
    set "ddir=%%~na" & set "dst=%dest%\!ddir!" 
    echo "!source[%%x]!" "!dst!")) 
exit /b 
+0

@ Pohl7534 스크립트가 효과가 있다면, 대답의 왼쪽에있는 "화살표 위로"및 "선택"기호를 클릭하여이 스레드의 모든 응답을 upvoting 및 수락하는 것을 고려하십시오. :) – sambul35

+0

내부 루프에서 '/ f' 스위치를 제거하면 제안 사항이 적용됩니다. @TessellatingHeckle은 이미 지적했듯이 원래 스크립트에서 변경해야하는 모든 것은'! ddir! '변수에 delayedexpansion을 사용했습니다. – Pohl7534

+0

댓글은 Google 검색을 올바르게 허용하지 않습니다. 답변을 찾으려면 답변이 아닌 댓글로 게시되어야합니다. – sambul35

관련 문제