Results 1 to 12 of 12

Thread: "If" and "GoTo" in batch file command?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2008
    Posts
    171

    "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

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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

  3. #3
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    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

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Feb 2008
    Posts
    171

    Re: "If" and "GoTo" in batch file command?

    Quote Originally Posted by westconn1 View Post
    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

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Feb 2008
    Posts
    171

    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."

  7. #7
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,219

    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

  8. #8
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Feb 2008
    Posts
    171

    Re: "If" and "GoTo" in batch file command?

    Quote Originally Posted by Schmidt View Post
    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.

  10. #10
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: "If" and "GoTo" in batch file command?

    Thread moved from the 'VB6' forum to the 'General Developer' forum.

  11. #11
    Member
    Join Date
    Dec 2009
    Location
    Germany
    Posts
    47

    Re: "If" and "GoTo" in batch file command?

    Quote Originally Posted by AccadaccA View Post
    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

  12. #12
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    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.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width