Batch file 如果语句不';行不通
在我的批处理文件中,我有一个if语句,它的行为不正常。我错过了什么导致了这一切Batch file 如果语句不';行不通,batch-file,Batch File,在我的批处理文件中,我有一个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&g
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
)
它的行为是这样的:当move type==file时,它会很好地运行if语句,但是当move type==directory时,脚本将终止,并且不会执行任何操作
切换if语句时(目录第一,文件第二),目录工作,文件不使用双引号字符并删除所有多余的空白:
if "%moveType%"=="directory" (
从这里开始,
%tgt\u dir%
和其他变量不会被双引号引用,空格/&字符会将其打断
if not exist %tgt_dir% (
在这个测试中,还需要一个尾随的反斜杠,否则它也可以匹配文件名
if not exist "%tgt_dir%\" (
我只能想出一个可能导致你描述的行为的场景。我怀疑,如果moveType=directory,则不能定义arcval_文件夹。相反,如果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结束绕过了该问题。在这种情况下,只有颠倒if块的顺序,才会出现问题
最简单的解决方案是将不存在的文件夹用引号括起来。Foxidrive也很正确,您可能应该包含一个尾随的\
,以确保您只查找文件夹
if not exist "%arcval_folder%\" ( ...
if not exist "%tgt_dir%\" ( ...
你发布的代码没有问题。问题一定是代码中没有显示的代码。也许你有一些不平衡的括号。@dbenham-我已经用这两个if语句中的所有内容更新了我的代码。这是另一个循环中的代码吗?@foxidrive不,不是。在这段代码上面,我解析了一个文件来填充变量,如果第二次添加引号不是一个坏主意,那么在我回显变量后,它就会消失,但我怀疑它在这种情况下会有什么不同。
==
前后的空格不会引起问题。正确的解决方案,但我认为错误的解释。如果空格或有毒字符是问题所在,那么If语句的顺序就无关紧要了。有关代码失败原因的理论,请参阅。@dbenham第一个if语句之所以有效,是因为它们被引用了。第二组失败,因为它们没有引号-假设术语中有空格/&的话。我们实际上是在猜测,因为我们没有术语,所以无法用实际数据测试代码。我想你没有领会我的意思。在OP的代码中,既不引用IF NOT EXIST语句,也不引用IF NOT EXIST语句。如果问题是由空格/&引起的,那么外部If块的顺序就无关紧要了——它总是会失败。但OP报告的情况并非如此。我的未定义变量理论确实产生了依赖于IF顺序的不对称失效。是的,这是一个猜测,但这是一个受过高等教育的猜测。我没有发现任何其他可能导致观察到的行为的错误源。因此,我是否应该将所有%tgt_dir%和%arcval_folder%变量都用引号引起来?@staples-表达式应该在IF NOT EXIST语句中引用。引号不应出现在变量值中。