0

I need to add the following code (or similar code that accomplishes the same thing)

Set "EightParam=%8"
Set "MyStringVariable=MyString"

if "%EightParam%"=="%MyStringVariable%" (
    call myBatFile.bat
    IF %ERRORLEVEL% NEQ 0 (
        exit /b %ERRORLEVEL%
    )
    myExeFile.exe
    IF %ERRORLEVEL% NEQ 0 (
        exit /b %ERRORLEVEL%
    )
)

REM a lot more code follows below, that I don't dare to change...

in the beginning of an existing big bat-file that I don't dare to fiddle with. Hence, I don't want to add "setlocal enabledelayedexpansion" in order to solve this problem. Does anybody know of a workaround that does not use enabledelayedexpansion?

arnold_w
  • 59
  • 9
  • move the code in the if-codeblock to a subroutine and call that subroutine in the if-statement. This way you do not need delayed expansion. – OJBakker May 15 '23 at 19:28
  • Another method is the `if not errorlevel 1` syntax (see `if /?`) – Stephan May 15 '23 at 20:25
  • It's very important that you understand that in your code, `%errorlevel%` would be replaced by the value of `errorlevel` **at the time the IF is encountered**, not as it may have been changed by `myBatFile.bat` or `myExeFile.exe`. [Stephan's DELAYEDEXPANSION link](https://stackoverflow.com/a/30284028/2128947) – Magoo May 15 '23 at 21:42

1 Answers1

2

Example, removing the need for parenthesized command blocks:

Set "MyStringVariable=MyString"

If /I Not "%~8" == "%MyStringVariable%" GoTo Next
Call "myBatFile.bat"
If ErrorLevel 1 Exit /B %ErrorLevel%
"myExeFile.exe"
If ErrorLevel 1 Exit /B %ErrorLevel%

:Next
Rem A lot more code follows below...
Compo
  • 36,585
  • 5
  • 27
  • 39
  • But what if myBatFile.bat and myExeFile.exe return errorlevel 2, 3, 4,... will this work then? – arnold_w May 15 '23 at 20:16
  • 1
    @arnold_w: yes; `if errorlevel 1` tests if the exit code (also in %ERRORLEVEL%) is _equal to OR greater than_ 1. See `if /?` for documentation. – dave_thompson_085 May 15 '23 at 21:03
  • I have no idea what `myExeFile.exe` is really, or how it works, @arnold_w, so cannot help you with how it reports its error or exit codes. Either way, the answer I've offered, even if you wanted to replace ```If ErrorLevel 1``` with ```If "%ErrorLevel%" == "1"```, ```If %ErrorLevel% Equ 1```, ```If Not "%ErrorLevel%" == "0"```, ```If %ErrorLevel% NEq 0```, or some other combination, does exactly what your question asked, so your new question is irrelevant to it. – Compo May 15 '23 at 22:15
  • @Compo myExeFile.exe is a self-written C# console application that returns an int (0 for success, non-zero for failure). But it seems to work fine if I follow OJBakker's advice above and put the code in its own subroutine. – arnold_w May 15 '23 at 22:22
  • The 'subroutine' comment will not change anything @arnold_w. If your ```If %ErrorLevel% NEq 0``` method works for you in a `call`ed label, it, or one of the other options I've used or commented, will work equally well as shown above. – Compo May 15 '23 at 22:30
  • @arnold_w, why did you delete your previous comment, admitting that my code works as intended? The only reason I can think of for doing so, would be a deliberate attempt to not help future readers. If the answer worked, and it was your error, as you had stated, you could have at least marked the answer as accepted. – Compo May 16 '23 at 13:07
  • @Compo, thanks for your efforts. When I thought it was working for me, I had made a mistake in my code and it really wasn't working. I still can't get any code to work so I've given up. My workaround is to put the code in a subroutine that resides in the very end of the file and since there's no code beyond it, I think setting setlocal disableDelayedExpansion will be harmless (it won't impact any of the big, existing code). – arnold_w May 16 '23 at 17:58
  • Post the code you say works when mine doesn't @arnold_w. This is important because there is absoluteay no reason for my code not to work when the 'inferior' and unnecessary 'call' to a label does. Please also note, if you have modified absolutely anything I submitted as code, (other than the commeted offerings), then you have not tested my code at all. You should also show those changes. In fact, if you now have working code, you should submit it as an answer. Any code must be a [mcve] we can run and test ourselves. – Compo May 16 '23 at 18:16
  • @Compo I wanted to find a solution without using setlocal disableDelayedExpansion, but I couldn't get any to work. It was like the 2nd if-statement on the errorlevel always was ignored. So, I felt forced to use setlocal disableDelayedExpansion. If I had placed it in the beginning of the code, I'd be afraid I'd mess something up in big part of the code that I didn't write myself. Therefore, in order to be able to place it in the end I needed a subroutine. It's not a solution, it's a workaround. – arnold_w May 16 '23 at 18:35
  • Your commet above is the exact opposite of your question @arnold_w. Please note the distinct difference between ```SetLocal DisableDelayedExpansion``` and ```SetLocal EnableDelayedExpansion```. Not that either affects my answer, which requires no changes as written under either, _(unless everything you posted is contained within a parenthesized block of code you have not shown, or even implied)_. – Compo May 16 '23 at 19:16
  • @Compo, you are absolutely correct that I have failed to notice that there's both an enable and disable function. So I guess the entire problem description is wrong because it's setlocal disableDelayedExpansion that I want to get rid of. This is the subroutine that works fine for me: :CallBatAndExeFilesSubroutine setlocal disableDelayedExpansion call myBatFile.bat IF %ERRORLEVEL% NEQ 0 ( exit /b %ERRORLEVEL% ) myExeFile.exe IF %ERRORLEVEL% NEQ 0 ( exit /b %ERRORLEVEL% ) goto :subroutinesEnd REM Subroutines' end label :subroutinesEnd – arnold_w May 17 '23 at 09:10
  • None of that makes any sense in the comment section, and without seeing the entire content of `myBatFile.bat`, and preferably all of your currently submitted batch file, not just a limited snippet of it, I cannot assist you further. I answered your question correctly based upon all of the information you offered. – Compo May 17 '23 at 09:54