batch file - Conditional execution (IF, ELSE) NOT WORKING -
i want write batch script statement where: findstr has check string , if found print out failed , end program if not found go check/look string , if found print out succeeded , close program, if not found print out error message again. ideas?
here did:
echo checking log file errors... findstr /c:"open failed" some_log.txt && (echo deployment failed. cscript //nologo success_mail.vbs pause) || findstr "rc (return code) = 0" && (echo deployment successful. cscript //nologo fail_mail.vbs pause)
i don't know why not working. appreciated.
here latest 1 looks like:
echo checking log file 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 successful. cscript //nologo success_mail.vbs goto offshore) )
and, not working. see errors? in advance.
i see couple of potential problems.
your
%workdir%
or%filenm%
contain spaces or special characters. safe should enclose them in quotes if haven't done in values.you must careful when using both
&&
,||
operators. if command(s) after&&
fail, can cause script fall||
section, though original command before&&
succeeded. i'm worried cscript command send mail. if succeeds today, perhaps fail in future , logic impacted.i'm guessing 2nd search string supposed phrase , not 5 different searches. remember search strings delimited space unless /c option used.
your 2nd findstr hanging because have neglected provide file input, waiting data on stdin.
i structure code so
echo checking log file 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 successful. cscript //nologo fail_mail.vbs "%filenm%_ddl_dev.sql" "%file%" pause )
Comments
Post a Comment