2014-03-26 94 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