How to avoid command termination

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
susja
Posts: 8
Joined: 26 Nov 2009 10:54

How to avoid command termination

#1 Post by susja » 08 Apr 2011 20:45

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

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to avoid command termination

#2 Post by aGerman » 09 Apr 2011 00:51

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

susja
Posts: 8
Joined: 26 Nov 2009 10:54

Re: How to avoid command termination

#3 Post by susja » 09 Apr 2011 05:52

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

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to avoid command termination

#4 Post by aGerman » 09 Apr 2011 06:10

Try

Code: Select all

call "%com1%"

or

Code: Select all

start "" /wait "%com1%"

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

susja
Posts: 8
Joined: 26 Nov 2009 10:54

Re: How to avoid command termination

#5 Post by susja » 09 Apr 2011 09:27

aGerman wrote:Try

Code: Select all

call "%com1%"

or

Code: Select all

start "" /wait "%com1%"

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

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to avoid command termination

#6 Post by aGerman » 10 Apr 2011 07:27

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

susja
Posts: 8
Joined: 26 Nov 2009 10:54

Re: How to avoid command termination

#7 Post by susja » 10 Apr 2011 07:57

thanks for your help

Post Reply