批处理文件if语句错误

时间:2011-12-08 21:25:01

标签: if-statement batch-file

如何在.bat批处理文件中执行此操作:

 if ("c:\program files\visualsvn server\bin\svnlook.exe" log -r2 d:\repositories\myrepo | findstr "~~DEPLOY~~" )
    (
     #dosomething
    )
    else
    (
     #dosomethingelse
    )

现在我收到错误此时此日志意外。

3 个答案:

答案 0 :(得分:2)

使用&&和||根据前一个命令的成功或失败有条件地执行命令

"c:\program files\visualsvn server\bin\svnlook.exe" log -r2 d:\repositories\myrepo | findstr "~~DEPLOY~~" >nul && (
  #do_Something_If_Success
) || (
  #do_Something_Else_If_Failure
)

答案 1 :(得分:1)

请改为尝试:

setlocal enabledelayedexpansion
set found_deploy=0
for /f 'eol=; tokens=1 delims=' %%c in ('"c:\program files\visualsvnserver\bin\svnlook.exe" log -r2 d:\repositories\myrepo ^| findstr "~~DEPLOY~~"') do (
    set found_deploy=1
)

if "!found_deploy!"=="1" (
    @REM::do_something_based_on_finding_deploy
) else (
    @REM::do_something_based_on_not_finding_deploy
)

答案 2 :(得分:1)

dbenham的回答汇集了IF-THEN-ELSE,既高级又有折衷。 kikuchiyo的一个是不必要的复杂。

这是前两者之间的中间点:

"c:\program files\visualsvn server\bin\svnlook.exe" log -r2 d:\repositories\myrepo | findstr "~~DEPLOY~~" >nul
if errorlevel 1 (
    echo Deploy not found
) else (
    echo Deploy found
)