Page 1 of 1
How to avoid command termination
Posted: 08 Apr 2011 20:45
by susja
Hello,
I have a task: execute 2 commands: com1 and com2. I want that command com2 will be executed only if com1 succeeded. I use batch like this:
@echo off
set x=com1
%x%
IF %ERRORLEVEL% NEQ 0 (EXIT)
set y=com2
%y%
The problem is that execution never get to line 'if' ... and check for condition. It looks that after %x% batch always exit and doesn't give the option to check for condition.
Could someone give an idea how to prevent to terminate batch to exit after %x% and force to go to 'if' statement?
Thanks in advance
Re: How to avoid command termination
Posted: 09 Apr 2011 00:51
by aGerman
I don't know why because I don't know what com1 means. Is it a command line, another batch file or script, a third party tool ... Probably somewhere there you should search the reason if the batch processing interrupts.
Regards
aGerman
Re: How to avoid command termination
Posted: 09 Apr 2011 05:52
by susja
aGerman wrote:I don't know why because I don't know what com1 means. Is it a command line, another batch file or script, a third party tool ... Probably somewhere there you should search the reason if the batch processing interrupts.
Regards
aGerman
hi aGerman,
thanks for reply.
I have no doubt that the problem is caused by com1. If my com1 is like
set com1=copy x y
then it works fine.
But my actual com1 is a program and it looks like:
set com1=c:\myProgram\xyz
%com1%
It looks that after execution com1 immediately exit and doesn't go to the next line.
How to prevent it and force to go to the next line in my batch?
thanks
Re: How to avoid command termination
Posted: 09 Apr 2011 06:10
by aGerman
Try
or
But it depends on the program you've called whether an errorlevel is available and what it mean. It could be that the errorlevel is always 0 even if an error occurred and some command line tools (like robocopy) return errorlevel 1 even if everything was fine.
Regards
aGerman
Re: How to avoid command termination
Posted: 09 Apr 2011 09:27
by susja
aGerman wrote:Try
or
But it depends on the program you've called whether an errorlevel is available and what it mean. It could be that the errorlevel is always 0 even if an error occurred and some command line tools (like robocopy) return errorlevel 1 even if everything was fine.
Regards
aGerman
aGerman,
thanks again for you suggestion ... I tried both and realized that in my case I can't rely on %ERRORLEVEL%. I mean that when my %com1% passed or failed in both cases %ERRORLEVEL% returns 0.
Do you have any other idea ( other than %ERRORLEVEL%) how could I handle execution of second command only in case the first command succeeded?
Thanks
Re: How to avoid command termination
Posted: 10 Apr 2011 07:27
by aGerman
I can't give any further suggestions since I don't know what com1 has to do and whether it outputs something on the batch window or whether it changes something on the file systen.
Regards
aGerman
Re: How to avoid command termination
Posted: 10 Apr 2011 07:57
by susja
thanks for your help