2012-07-28 190 views
0

我已經在這裏批處理,我試圖讓父目錄堅持。覆蓋變量從例程

@echo off 

SET CWD= 

:process 
if [%1] == [] goto end 
SET MASTER_DIR="%~f1" 

rem Change to say, E:\DVD_LIBRARY\Rips 
cd /d %MASTER_DIR% 
for /R %%D IN (\) DO (

    rem Temporarily change to the subdir, such as E:\DVD_LIBRARY\Rips\SIN_CITY\ 
    pushd %%D 
    for /F "usebackq" %%Z in (`dir /b *.vob 2^>NUL`) DO (
     if exist %%~fZ (

      rem Get this parent directory to store the log file in (eventually) 
      CALL :resolve "%%D\.." CWD 

      rem Nada. 
      echo: %CWD% 
     ) 
    ) 
    popd 
) 
shift 
goto process 

:resolve 
SET %2=%~f1 
goto :EOF 

:resolve例程中,我得到了我想要的值。回到這個區塊:

if exist %%~fZ (
    CALL :resolve "%%D\.." CWD 
    echo: %CWD% 
) 

我什麼也沒得到。

任何想法,爲什麼這不是堅持,或更好的方式?我搜索谷歌,發現這種技術和其他一些喜歡它在這裏,但我不知道爲什麼價值是不翻譯後CALL

回答

2

問題不在於cwd變量未設置,但無法按照您嘗試的方式回顯它。 這是因爲在解析IF塊時擴展了它。

但是您需要CALL之後的擴展。 你可以用延遲擴展或CALL-擴大

if exist %%~fZ (
    CALL :resolve "%%D\.." CWD 
    call echo: %%CWD%% 
) 

setlocal EnableDelayedExpansion 
if exist %%~fZ (
    CALL :resolve "%%D\.." CWD 
    echo: !CWD! 
) 
+0

美麗的解決這個問題。這種奇怪的語言....謝謝! – Phix 2012-07-29 02:28:43