Batch file and returning values from functions
I have a batch file that has multiple routines. examples
:checkFileExists
if not exist %1%2 (
echo %2 does not exist under %1
set returnValue=FAIL
exit /b 1
)
exit /b 0
:checkPortNumber
if %1 LSS 1024 (
port number should be greater than 1024 and less than 65535
set returnValue=FAIL
exit /b 1
)
... other checks for port number
exit /b 0
... main code..
set returnValue=OK
call :checkFileExists c:tmp
echo %returnValue%
call :checkFileExists c:tmp2
echo %returnValue%
call :checkPortNumber 89
echo %returnValue%
c:tmp is just an example and it exists. my return value correctly displays as OK. c:tmp2 does not exist and my returnValue correctly displays as FAIL.
I do expect checkPortNumber to fail the test but returnValue is set to OK always. I have tried with setlocal enableDelayedExpansion also and not. Is there something I am missing?
windows batch
add a comment |
I have a batch file that has multiple routines. examples
:checkFileExists
if not exist %1%2 (
echo %2 does not exist under %1
set returnValue=FAIL
exit /b 1
)
exit /b 0
:checkPortNumber
if %1 LSS 1024 (
port number should be greater than 1024 and less than 65535
set returnValue=FAIL
exit /b 1
)
... other checks for port number
exit /b 0
... main code..
set returnValue=OK
call :checkFileExists c:tmp
echo %returnValue%
call :checkFileExists c:tmp2
echo %returnValue%
call :checkPortNumber 89
echo %returnValue%
c:tmp is just an example and it exists. my return value correctly displays as OK. c:tmp2 does not exist and my returnValue correctly displays as FAIL.
I do expect checkPortNumber to fail the test but returnValue is set to OK always. I have tried with setlocal enableDelayedExpansion also and not. Is there something I am missing?
windows batch
add a comment |
I have a batch file that has multiple routines. examples
:checkFileExists
if not exist %1%2 (
echo %2 does not exist under %1
set returnValue=FAIL
exit /b 1
)
exit /b 0
:checkPortNumber
if %1 LSS 1024 (
port number should be greater than 1024 and less than 65535
set returnValue=FAIL
exit /b 1
)
... other checks for port number
exit /b 0
... main code..
set returnValue=OK
call :checkFileExists c:tmp
echo %returnValue%
call :checkFileExists c:tmp2
echo %returnValue%
call :checkPortNumber 89
echo %returnValue%
c:tmp is just an example and it exists. my return value correctly displays as OK. c:tmp2 does not exist and my returnValue correctly displays as FAIL.
I do expect checkPortNumber to fail the test but returnValue is set to OK always. I have tried with setlocal enableDelayedExpansion also and not. Is there something I am missing?
windows batch
I have a batch file that has multiple routines. examples
:checkFileExists
if not exist %1%2 (
echo %2 does not exist under %1
set returnValue=FAIL
exit /b 1
)
exit /b 0
:checkPortNumber
if %1 LSS 1024 (
port number should be greater than 1024 and less than 65535
set returnValue=FAIL
exit /b 1
)
... other checks for port number
exit /b 0
... main code..
set returnValue=OK
call :checkFileExists c:tmp
echo %returnValue%
call :checkFileExists c:tmp2
echo %returnValue%
call :checkPortNumber 89
echo %returnValue%
c:tmp is just an example and it exists. my return value correctly displays as OK. c:tmp2 does not exist and my returnValue correctly displays as FAIL.
I do expect checkPortNumber to fail the test but returnValue is set to OK always. I have tried with setlocal enableDelayedExpansion also and not. Is there something I am missing?
windows batch
windows batch
asked Dec 15 '18 at 7:26
SESHAGIRI SRIRAM
1
1
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
This reordered/reworked/reformatted code behaves as expected.
Main problem with your code is that you only set returnValue=OK once at the beginning, if a subroutine set returnValue=Fail this does persist even if another sub does exit /B 0.
:: Q:Test20181215SU_1383774.cmd
@echo off
:: ... main code..
call :checkFileExists c:tmp || Echo returned errorlevel %errorlevel%
echo %returnValue%
call :checkFileExists c:tmp2 || Echo returned errorlevel %errorlevel%
echo %returnValue%
call :checkPortNumber 89 || Echo returned errorlevel %errorlevel%
echo %returnValue%
Goto :Eof
:checkFileExists
if not exist "%~1%~2" (
echo %2 does not exist under %1
set returnValue=FAIL
exit /b 1
)
set returnValue=OK
exit /b 0
:checkPortNumber
if %1 LSS 1024 (
echo port number should be greater than 1024 and less than 65535
set returnValue=FAIL
exit /b 1
)
:: ... other checks for port number
set returnValue=OK
exit /b 0
1
It would be a better answer if you explained why this works when the original code doesn't.
– DavidPostill♦
Dec 15 '18 at 18:26
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1383774%2fbatch-file-and-returning-values-from-functions%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
This reordered/reworked/reformatted code behaves as expected.
Main problem with your code is that you only set returnValue=OK once at the beginning, if a subroutine set returnValue=Fail this does persist even if another sub does exit /B 0.
:: Q:Test20181215SU_1383774.cmd
@echo off
:: ... main code..
call :checkFileExists c:tmp || Echo returned errorlevel %errorlevel%
echo %returnValue%
call :checkFileExists c:tmp2 || Echo returned errorlevel %errorlevel%
echo %returnValue%
call :checkPortNumber 89 || Echo returned errorlevel %errorlevel%
echo %returnValue%
Goto :Eof
:checkFileExists
if not exist "%~1%~2" (
echo %2 does not exist under %1
set returnValue=FAIL
exit /b 1
)
set returnValue=OK
exit /b 0
:checkPortNumber
if %1 LSS 1024 (
echo port number should be greater than 1024 and less than 65535
set returnValue=FAIL
exit /b 1
)
:: ... other checks for port number
set returnValue=OK
exit /b 0
1
It would be a better answer if you explained why this works when the original code doesn't.
– DavidPostill♦
Dec 15 '18 at 18:26
add a comment |
This reordered/reworked/reformatted code behaves as expected.
Main problem with your code is that you only set returnValue=OK once at the beginning, if a subroutine set returnValue=Fail this does persist even if another sub does exit /B 0.
:: Q:Test20181215SU_1383774.cmd
@echo off
:: ... main code..
call :checkFileExists c:tmp || Echo returned errorlevel %errorlevel%
echo %returnValue%
call :checkFileExists c:tmp2 || Echo returned errorlevel %errorlevel%
echo %returnValue%
call :checkPortNumber 89 || Echo returned errorlevel %errorlevel%
echo %returnValue%
Goto :Eof
:checkFileExists
if not exist "%~1%~2" (
echo %2 does not exist under %1
set returnValue=FAIL
exit /b 1
)
set returnValue=OK
exit /b 0
:checkPortNumber
if %1 LSS 1024 (
echo port number should be greater than 1024 and less than 65535
set returnValue=FAIL
exit /b 1
)
:: ... other checks for port number
set returnValue=OK
exit /b 0
1
It would be a better answer if you explained why this works when the original code doesn't.
– DavidPostill♦
Dec 15 '18 at 18:26
add a comment |
This reordered/reworked/reformatted code behaves as expected.
Main problem with your code is that you only set returnValue=OK once at the beginning, if a subroutine set returnValue=Fail this does persist even if another sub does exit /B 0.
:: Q:Test20181215SU_1383774.cmd
@echo off
:: ... main code..
call :checkFileExists c:tmp || Echo returned errorlevel %errorlevel%
echo %returnValue%
call :checkFileExists c:tmp2 || Echo returned errorlevel %errorlevel%
echo %returnValue%
call :checkPortNumber 89 || Echo returned errorlevel %errorlevel%
echo %returnValue%
Goto :Eof
:checkFileExists
if not exist "%~1%~2" (
echo %2 does not exist under %1
set returnValue=FAIL
exit /b 1
)
set returnValue=OK
exit /b 0
:checkPortNumber
if %1 LSS 1024 (
echo port number should be greater than 1024 and less than 65535
set returnValue=FAIL
exit /b 1
)
:: ... other checks for port number
set returnValue=OK
exit /b 0
This reordered/reworked/reformatted code behaves as expected.
Main problem with your code is that you only set returnValue=OK once at the beginning, if a subroutine set returnValue=Fail this does persist even if another sub does exit /B 0.
:: Q:Test20181215SU_1383774.cmd
@echo off
:: ... main code..
call :checkFileExists c:tmp || Echo returned errorlevel %errorlevel%
echo %returnValue%
call :checkFileExists c:tmp2 || Echo returned errorlevel %errorlevel%
echo %returnValue%
call :checkPortNumber 89 || Echo returned errorlevel %errorlevel%
echo %returnValue%
Goto :Eof
:checkFileExists
if not exist "%~1%~2" (
echo %2 does not exist under %1
set returnValue=FAIL
exit /b 1
)
set returnValue=OK
exit /b 0
:checkPortNumber
if %1 LSS 1024 (
echo port number should be greater than 1024 and less than 65535
set returnValue=FAIL
exit /b 1
)
:: ... other checks for port number
set returnValue=OK
exit /b 0
edited Dec 15 '18 at 19:02
answered Dec 15 '18 at 9:08
LotPings
4,6761722
4,6761722
1
It would be a better answer if you explained why this works when the original code doesn't.
– DavidPostill♦
Dec 15 '18 at 18:26
add a comment |
1
It would be a better answer if you explained why this works when the original code doesn't.
– DavidPostill♦
Dec 15 '18 at 18:26
1
1
It would be a better answer if you explained why this works when the original code doesn't.
– DavidPostill♦
Dec 15 '18 at 18:26
It would be a better answer if you explained why this works when the original code doesn't.
– DavidPostill♦
Dec 15 '18 at 18:26
add a comment |
Thanks for contributing an answer to Super User!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1383774%2fbatch-file-and-returning-values-from-functions%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown