-
Jan 6th, 2017, 09:30 PM
#1
Thread Starter
Addicted Member
"If" and "GoTo" in batch file command?
Hi,
My batch file runs four commands, three of which are map compiling commands linked to the game engine, the fourth is just an alert for when all of the three compiling tasks have completed.
However if there is an error in the map compiling, in either of the three compile processes, the command console simply closes without executing any of the remaining commands that follow it.
How can I add an "If" case to "GoTo" the last command only if an error has occurred?
All searches have found are "If Exists" examples. That's not what I'm after.
Thank you
-
Jan 6th, 2017, 09:55 PM
#2
Re: "If" and "GoTo" in batch file command?
batch files really have nothing to do with classic vb
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Jan 6th, 2017, 09:58 PM
#3
Re: "If" and "GoTo" in batch file command?
20 years ago I could have probably answered that question but that is about how long it has been since I did any serious bat file programming.
Once upon a time I had some that were pretty complex but have long since forgotten most beyond the basics.
That said perhaps this would help
http://ss64.com/nt/goto.html
If not then you may want to try this
https://www.google.com/search?q=if+g...utf-8&oe=utf-8
-
Jan 6th, 2017, 10:26 PM
#4
Thread Starter
Addicted Member
Re: "If" and "GoTo" in batch file command?
Originally Posted by westconn1
batch files really have nothing to do with classic vb
Maybe not, westconn1, but they do have IF and GOTO commands and they are shelled from ...
Oops, okay. I see what you are saying. I shouldn't have posted a batch question here. Sorry, I didn't think of it as being unrelated to vb6..
DataMiser: Cheers mate, I have already found that first link and it was all way over my head.
I need something written for idiots where I can clearly see how to related to my command lines
like an example saying...
If commandline1 fails goto commandline 4
If commandline2 fails goto commandline 4
If commandline3 fails goto commandline 4
(optional)
If all else fails ask on vbForums. lol
-
Jan 6th, 2017, 10:52 PM
#5
Re: "If" and "GoTo" in batch file command?
if errorlevel 1, or maybe IF %ERRORLEVEL% NEQ 0
from http://www.robvanderwoude.com/errorlevel.php
in either case it would depend if the called program actually returns an errorlevel if it fails
using wscript you could return all the messages from each command to caller, though would have to parse the returned messages to find what message has been returned
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Jan 6th, 2017, 11:08 PM
#6
Thread Starter
Addicted Member
Re: "If" and "GoTo" in batch file command?
Thank you westconn1.
Wow, this does look promising. I will try to stumble through it.
My signature should read "I rely heavily on trial and error, mostly only succeeding in the latter."
-
Jan 7th, 2017, 12:04 AM
#7
Re: "If" and "GoTo" in batch file command?
Why not simply write this up in a *.vbs instead of a *.bat (reusing your existing skills)?
Olaf
-
Jan 7th, 2017, 03:51 AM
#8
Re: "If" and "GoTo" in batch file command?
(reusing your existing skills)?
lol!
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Jan 7th, 2017, 09:05 AM
#9
Thread Starter
Addicted Member
Re: "If" and "GoTo" in batch file command?
Originally Posted by Schmidt
Why not simply write this up in a *.vbs instead of a *.bat (reusing your existing skills)?
Agreed, lol
I rely heavily on trial and error, mostly only succeeding in the latter.
-
Jan 7th, 2017, 12:32 PM
#10
Re: "If" and "GoTo" in batch file command?
Thread moved from the 'VB6' forum to the 'General Developer' forum.
-
Jan 8th, 2017, 07:11 AM
#11
Member
Re: "If" and "GoTo" in batch file command?
Originally Posted by AccadaccA
Hi,
My batch file runs four commands, three of which are map compiling commands linked to the game engine, the fourth is just an alert for when all of the three compiling tasks have completed.
However if there is an error in the map compiling, in either of the three compile processes, the command console simply closes without executing any of the remaining commands that follow it.
How can I add an "If" case to "GoTo" the last command only if an error has occurred?
...
If commandline1 fails goto commandline 4
If commandline2 fails goto commandline 4
If commandline3 fails goto commandline 4
...
Several possibilities exist in a batch file.
Here're three of them that could be of interest:
Possibility 1:
Code:
@ECHO OFF
SETLOCAL ENABLEEXTENSIONS
cmdLine1
if errorlevel 1 GOTO :_Skip
cmdLine2
if errorlevel 1 GOTO :_Skip
cmdLine3
if errorlevel 1 GOTO :_Skip
ECHO commands executed successfully
' do something more here
GOTO:_End
:_Skip
ECHO execution for one of the three commands failed
:_End
ECHO.&ECHO This is end of batch
ENDLOCAL
PAUSE>NUL
Possibility 2 (better, but more advanced):
Code:
@ECHO OFF
SET "Err="
' this syntax is preferable if the commands return an errorlevel not equal 0
' execute command - if errorlevel equal or greater 1 set Err variable
cmdLine1||SET "Err=1"
cmdLine2||SET "Err=1"
cmdLine3||SET "Err=1"
IF NOT DEFINED Err (
Echo All commands executed successfully
) ELSE (
ECHO at least one of the three commands failed
)
ECHO.& ECHO This is end of batch
PAUSE>NUL&EXIT
Possibility 3 (advanced+more info):
Code:
@ECHO OFF
SETLOCAL ENABLEEXTENSIONS
SET "OK="
' this syntax is preferable if the commands return an errorlevel not equal 0
' execute command - if errorlevel less than 1 set OK variable - else call label _ErrH
cmdLine1 && SET OK=1||CALL:_ErrH 1
cmdLine2 && SET OK=1||CALL:_ErrH 2
cmdLine3 && SET OK=1||CALL:_ErrH 3
IF NOT DEFINED OK GOTO:_End
ECHO all command executed successfully
' do something more here
:_End
ECHO.& ECHO This is end of batch
ENDLOCAL
PAUSE>NUL&EXIT
:_ErrH
ECHO Command %~1 failed
' jump back to call position
GOTO:EOF
-
Jan 8th, 2017, 04:22 PM
#12
Re: "If" and "GoTo" in batch file command?
I don't know about batch files as I haven't programmed one in over 10 years now (actually I could have sworn Windows XP or Vista was the last MS Windows that could even run one) as I switched to using the new(er) Windows Command script files back in the XP days which does allow for things like error catching (not Try/Catch, sorry) and handling: Add Simple Error Handling to .cmd File
Some of the perks to using those is creating them are extremely similar to batch files and they're supported on Windows Server OS, if I remember correctly you can create a .cmd script for User Login script execution but it doesn't support batch files. If you're running Windows 2000 (server) or Windows XP or anything newer I'd just suggest switching to cmd scripts.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|