0

I want to write a batch script statement where: FINDSTR has to check for a string AND IF found then just print out FAILED and end the program IF NOT FOUND then go check/look for another string AND IF FOUND then print out SUCCEEDED and close the program, IF NOT FOUND then print out the error message again. Any ideas?

Here is what I did:

ECHO Checking the log file for errors...
FINDSTR /C:"Open failed" some_Log.txt && (ECHO Deployment FAILED. 
cscript //nologo success_mail.vbs 
pause) || FINDSTR "RC (return code) = 0" && (ECHO Deployment was successful.
cscript //nologo fail_mail.vbs
pause)

I don't know why it is not working. Any help would be appreciated.

Here is what i my latest one looks like:

  ECHO Checking the log file for errors...
    FINDSTR /C:"Open failed" some_Log.txt some_Log.txt && (
     ECHO Deployment FAILED. 
     cscript //nologo fail_mail.vbs 
     GOTO offshore ) || (
      FINDSTR /C:"RC (return code) = 0" some_Log.txt && (
        ECHO Deployment was successful.
        cscript //nologo success_mail.vbs 
        GOTO offshore)
      )

And, it is not working. Do you see any errors? Thanks in advance.

4

1 回答 1

0

我看到了几个潜在的问题。

  • 您的%workDir%or%filenm%可能包含空格或特殊字符。为安全起见,如果您尚未在值中这样做,则应将它们括在引号中。

  • &&使用and运算符时必须小心||。如果&&失败之后的命令,那么它可能会导致脚本落入该||部分,即使&&成功之前的原始命令也是如此。我担心您发送邮件的 CSCRIPT 命令。即使它今天成功,也许它将来可能会失败,你的逻辑可能会受到影响。

  • 我猜第二个搜索字符串应该是一个短语而不是 5 个不同的搜索。请记住,除非使用 /C 选项,否则搜索字符串由空格分隔。

  • 您的第二个 FINDSTR 挂起,因为您忽略了提供文件作为输入,因此它正在等待标准输入上的数据。

我会像这样构造你的代码

ECHO Checking the log file for errors...
set "file=%workDir%\%filenm%_DEV_Log.txt"
set "search1=Error: Open failed because: No such file or directory"
set "search2=RC (return code) = 0"
set "err="
FINDSTR /C:"%search1%" "%file%" && (set err=1) || (
  FINDSTR /C:"%search2%" "%file%" || set err=1
)
if defined err (
  ECHO Deployment FAILED.
  cscript //nologo success_mail.vbs "%filenm%_ddl_DEV.sql" "%file%"
  pause
) else (
  ECHO Deployment was successful.
  cscript //nologo fail_mail.vbs "%filenm%_ddl_DEV.sql" "%file%"
  pause
)
于 2012-07-02T22:29:09.790 回答