2014-03-26 95 views
2

在我的批處理文件中,我有一個if語句,這是行爲不端。我錯過了哪些是造成這種情況的原因?批處理語句不起作用

if "%moveType%"=="file" (
echo Arcval File Move 1>>"%logfile%" 2>&1 

if not exist %arcval_folder% (
    mkdir %arcval_folder% 
    echo Directory: %arcval_folder% created. 1>>"%logfile%" 2>&1 
    ECHO. 1>>"%logfile%" 2>&1 
) 

echo xcopy "%src_dir%" "%tgt_dir%" 1>>"%logfile%" 2>&1 
echo F|xcopy "%src_dir%" "%tgt_dir%" 1>>"%logfile%" 2>&1 

if ERRORLEVEL 1 (
    echo Failure: File not found, Arcval File not promoted. 1>>"%logfile%" 2>&1 
    echo "\\ntmaster\code\reskit\blat.exe" "\\ntmaster\batch\Jobs\Test\far\Common\ArcvalFailure-emailTxt.txt" -t "%promoter_email%" -c "%initiator_email%" -s "Failure! File not found, Arcval File not promoted." -f "%initiator_email%" 1>>"%logfile%" 2>&1 
    "\\ntmaster\code\reskit\blat.exe" "\\ntmaster\batch\Jobs\Test\far\Common\ArcvalFailure-emailTxt.txt" -t "%promoter_email%" -c "%initiator_email%" -s "Failure! File not found, Arcval File not promoted." -f "%initiator_email%" 1>>"%logfile%" 2>&1 
    ECHO. 1>>"%logfile%" 2>&1 

    echo Failure email sent, exiting program. 

) else (
    echo Success: Arcval File was successfully promoted. 1>>"%logfile%" 2>&1 
    echo "\\ntmaster\code\reskit\blat.exe" "\\ntmaster\batch\Jobs\Test\far\Common\ArcvalSuccess-emailTxt.txt" -t "%promoter_email%" -c "%initiator_email%" -s "Success! Arcval File was successfully promoted." -f "%initiator_email%" 1>>"%logfile%" 2>&1 
    "\\ntmaster\code\reskit\blat.exe" "\\ntmaster\batch\Jobs\Test\far\Common\ArcvalSuccess-emailTxt.txt" -t "%promoter_email%" -c "%initiator_email%" -s "Success! Arcval File was successfully promoted." -f "%initiator_email%" 1>>"%logfile%" 2>&1 
    ECHO. 1>>"%logfile%" 2>&1 

    echo Success email sent, exiting program. 
) 

goto end 

) 

if "%moveType%"=="directory" (
echo Polysystems Directory Move 1>>"%logfile%" 2>&1 

if not exist %tgt_dir% (
    mkdir %tgt_dir% 
    echo Directory: %tgt_dir% created. 1>>"%logfile%" 2>&1 
    ECHO. 1>>"%logfile%" 2>&1 
) else (
    echo ERROR: Polysystems Target directory already exists, Source was not promoted. 1>>"%logfile%" 2>&1 
    echo "\\ntmaster\code\reskit\blat.exe" "\\ntmaster\batch\Jobs\Test\far\Common\polySystemsErr-emailTxt.txt" -t "%promoter_email%" -c "%initiator_email%" -s "ERROR! Polysystems Target directory already exists, Source was not promoted." -f "%initiator_email%" 1>>"%logfile%" 2>&1 
    ECHO. 1>>"%logfile%" 2>&1 

    "\\ntmaster\code\reskit\blat.exe" "\\ntmaster\batch\Jobs\Test\far\Common\polySystemsErr-emailTxt.txt" -t "%promoter_email%" -c "%initiator_email%" -s "ERROR! Polysystems Target directory already exists, Source was not promoted." -f "%initiator_email%" 1>>"%logfile%" 2>&1 
    ECHO. 1>>"%logfile%" 2>&1 
    echo Failure email sent, exiting program. 1>>"%logfile%" 2>&1 
    exit 
) 

echo xcopy %src_dir% %tgt_dir% /Y /Z /C /F /E 1>>"%logfile%" 2>&1 
echo F|xcopy %src_dir% %tgt_dir% /Y /Z /C /F /E 1>>"%logfile%" 2>&1 

echo Success: Polysystems Directory was successfully promoted. 1>>"%logfile%" 2>&1 
echo "\\ntmaster\code\reskit\blat.exe" "\\ntmaster\batch\Jobs\Test\far\Common\polySystemsSuccess-emailTxt.txt" -t "%promoter_email%" -c "%initiator_email%" -s "Success! Polysystems Directory was successfully promoted." -f "%initiator_email%" 1>>"%logfile%" 2>&1 
ECHO. 1>>"%logfile%" 2>&1 

"\\ntmaster\code\reskit\blat.exe" "\\ntmaster\batch\Jobs\Test\far\Common\polySystemsSuccess-emailTxt.txt" -t "%promoter_email%" -c "%initiator_email%" -s "Success! Polysystems Directory was successfully promoted." -f "%initiator_email%" 1>>"%logfile%" 2>&1 
ECHO. 1>>"%logfile%" 2>&1 
echo Success email sent, exiting program. 1>>"%logfile%" 2>&1 

goto end 
) 

這是它是如何表現:當移動類型==文件,它貫穿,如果聲明就好了,但是,當移動式==目錄,腳本死了,沒有做任何事情。

當if語句切換(目錄第一,第二的文件)目錄中的作品和文件不

+0

您發佈的代碼沒有任何問題。問題必須出現在您未顯示的代碼中。也許你有一些不平衡的括號。 – dbenham

+0

@dbenham - 我更新了兩條if語句中的所有代碼 – staples

+0

上面的代碼是否在另一個循環中? – foxidrive

回答

1

我只能想出一個可能會導致您描述的行爲的場景。我懷疑,如果moveType =目錄,則不能定義arcval_folder。相反,如果moveType = file,則不能定義tgt_dir。

你的代碼在這種情況下會失敗的原因是因爲複雜的IF語句被一次全部解析,包括所有相關的代碼塊。即使IF語句的計算結果爲FALSE,整個複雜語句也必須具有有效的語法。如果IF語句的某些部分沒有正確解析,那麼批處理腳本將以語法錯誤結束。

所以,如果moveType =目錄和arcval_folder是不確定的,那麼第一個IF塊如下所示後變量擴展:

if "file" == "file" (
    echo .... 
    if not exist (
    etc. 
) 
    etc. 
    goto end 
) 

內IF NOT EXIST聲明無效,而整個腳本失敗。

如果moveType = file且tgt_dir未定義,那麼第二個IF塊將是無效的,但第一個IF塊中的GOTO END繞過問題。如果您顛倒IF塊的順序,則在這種情況下只有問題。

最簡單的解決方案是將IF NOT EXIST文件夾放在引號中。 Foxidrive也是正確的,你可能應該包括一個尾隨\,以確保你只是在尋找文件夾。

if not exist "%arcval_folder%\" (... 

if not exist "%tgt_dir%\" (... 
+0

所以我應該把所有的%tgt_dir%和%arcval_folder%變量放在引號中? – staples

+0

@staples - 該表達式應在IF NOT EXIST語句中引用。引號不應該在變量值中。 – dbenham

2

使用雙引號字符,丟棄所有多餘的空白:

if "%moveType%"=="directory" (
+0

這不是一個壞主意來添加引號,但我懷疑這種情況在這種情況下會有所不同。 '=='前後的空格不會導致問題。 – dbenham

1

從這裏%tgt_dir%和其他變量不用雙引號,空格/ &字符會打破它。

if not exist %tgt_dir% (

,並在本次測試還需要一個尾部的反斜槓否則就太匹配文件名。

if not exist "%tgt_dir%\" (
+0

正確的解決方案,但我認爲是錯誤的解釋。如果空格或有毒字符是問題,則IF語句的順序無關緊要。請參閱[我的回答](http:// stackoverflow。com/a/22673285/1012053)爲我的理論爲什麼代碼失敗。 – dbenham

+0

@dbenham第一條if語句因爲引用而起作用。第二組失敗,因爲它們沒有被引用 - 假設條件中有空格。我們其實是在猜測,因爲我們沒有條款,所以不能用真實生活數據來測試代碼。 – foxidrive

+0

我想你錯過了我的觀點。在OP的代碼中,IF NOT EXIST語句都沒有引用。如果空間導致問題,那麼外部中頻塊的順序就不重要了 - 它總是會失敗。但執行委員會另有報告。我未定義的變量理論確實會產生依賴於IF順序的非對稱失效。是的,這是一個猜測,但它是一個高學歷的猜測。我沒有看到任何可能產生觀察行爲的其他可能的錯誤來源。 – dbenham