As foxidrive pointed out, we're better off seeing your full code. I'm going to take stab at it anyway, based on what you've given so far. There may be other issues, but these stand out.
Avanatus wrote:Code: Select all
SET DecimalNumberEND=!DecimalNumber!
ECHO ##### ##### DecimalNumberEND: !DecimalNumberEND! ##### #####
ENDLOCAL & SET RETURNVALUE=%DecimalNumberEND%
EXIT /b %DecimalNumberEND%
3. test.cmd tries to read the returnvalue via:
Code: Select all
SET DecimalNumber=DecimalNumberEND
In step 3, you're trying to retrieve the value from
DecimalNumberEND when you stored it in
RETURNVALUE. Also, you're missing the percent signs on the right side of the assignment, so you're just storing the variable name instead of its value.
Code: Select all
:: This doesn't work because you're just storing the string "DecimalNumberEND".
SET DecimalNumber=DecimalNumberEND
:: This doesn't work either, because DecimalNumberEND doesn't exist anymore.
SET DecimalNumber=%DecimalNumberEND%
:: This should work, because you set RETURNVALUE after ENDLOCAL.
SET DecimalNumber=%RETURNVALUE%
:: You don't actually need another SET command to use the variable.
ECHO %RETURNVALUE%
In RomanInDecimal.cmd, you're trying to pass the DecimalNumberEND value back as an error code, which is fine, but again, you're using the wrong variable. Change
EXIT /b %DecimalNumberEND% to
EXIT /b %RETURNVALUE%, and you can do this in test.cmd:
SET DecimalNumber=%errorlevel%. You'll need to do that shortly after the CALL, before running any commands that set ERRORLEVEL. The ECHO command doesn't set ERRORLEVEL, so it's okay to use.
The DecimalNumberEND variable isn't needed, based on what you've shown. Using
ENDLOCAL & SET RETURNVALUE=%DecimalNumber% will work just as well.
The
endlocal & set "return=%whatever%" trick works because
%whatever% gets expanded to its value before the ENDLOCAL command is run, but the SET command runs afterward, so that the variable (
return, in this case) gets set in the environment that existed before the SETLOCAL command. We can use it in the rest of the current batch (e.g., in the EXIT command) and in the calling batch.
If you know what variable name you'll want to use in the calling batch, you can just go ahead and use that in the called batch instead of the intermediate RETURNVALUE. That's a matter of preference, not right or wrong. If you want to do that, then the last couple lines in RomanInDecimal.cmd would be
ENDLOCAL & SET DecimalNumber=%DecimalNumber% and
EXIT /B %DecimalNumber%Put it all together, and we get this:
Code: Select all
:: test.cmd
CALL RomanInDecimal.cmd SETCodeStart !Filename!
ECHO DecimalNumber: %DecimalNumber%
ECHO errorlevel: %errorlevel%
SET DecNumFromErr=%errorlevel%
Code: Select all
:: RomanInDecimal.cmd
REM Don't need this: SET DecimalNumberEND=!DecimalNumber!
ECHO ##### ##### DecimalNumber: %DecimalNumber% ##### #####
ENDLOCAL & SET DecimalNumber=%DecimalNumber%
EXIT /B %DecimalNumber%