在我的批处理文件中,我有一个行为不端的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语句运行得很好,但是,当移动类型==目录时,脚本会死掉并且什么都不做。
当if语句被切换(目录优先,文件第二)目录工作而文件没有
答案 0 :(得分:2)
使用双引号字符并删除所有无关的空格:
if "%moveType%"=="directory" (
答案 1 :(得分:2)
我只能想出一个可能导致你描述的行为的场景。我怀疑如果moveType = directory,则不能定义arcval_folder。相反,如果moveType = file,则不得定义tgt_dir。
在这种情况下代码失败的原因是因为复杂的IF语句被一次性解析,包括随后的所有相关代码块。即使IF语句的计算结果为FALSE,整个复杂语句也必须具有有效的语法。如果IF语句的某些部分未正确解析,则批处理脚本将终止,并出现语法错误。
因此,如果moveType = directory和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%\" ( ...
答案 2 :(得分:1)
从这里%tgt_dir%
和其他变量不是双引号和空格/&人物会打破它。
if not exist %tgt_dir% (
并且在此测试中还需要一个尾部反斜杠,否则它也可以匹配文件名。
if not exist "%tgt_dir%\" (