Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8691873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:12:42+00:00 2026-06-13T00:12:42+00:00

In a batch script that I am working on, a variable value is not

  • 0

In a batch script that I am working on, a variable value is not being retained after calling 2 subroutines, one from another from a batch script’s FOR loop.
Here is a code snippet demo’ing the scenario:

set ERRORCODE=0
FOR ... do (
    call :myRoutine

    @ECHO %ERRORCODE%        // Here I am expecting the ERRORCODE to be a 1 (non-zero), but I am seeing that it is getting reset to 0
)

myRoutine:
call :another
IF %ERRORCODE% NEQ 0 GOTO :EOF  // Here I am getting the ERRORCODE as 1 as expected
...
GOTO :EOF


another:
something went wrong here..
IF %ERRORLEVEL% NEQ 0 (
    set ERRORCODE=1
    GOTO :EOF
)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T00:12:43+00:00Added an answer on June 13, 2026 at 12:12 am

    The ERRORCODE is retained just fine – you are just not accessing it properly.

    Your problem is that %ERRORCODE% is expanded when the line is parsed, and the entire parenthesized block of code is parsed all at once, before the FOR loop is executed. So you are seeing the value that existed before the loop is executed.

    The solution is to use delayed expansion, !ERRORCODE!, which requires setlocal enableDelayedExpansion. Delayed expansion occurs when the line is executed. Type HELP SET or SET /? from the command line for more information about delayed expansion.

    set ERRORCODE=0
    setlocal enableDelayedExpansion
    FOR ... do (
      call :myRoutine
      echo !ERRORCODE!
    )
    
    ... etc.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a batch script that will let me delete files older then
I've written a batch script that checks if Python is installed, if it's not
My batch file terminates prematurely after I assign the first environmental variable (script output
I have a batch script that outputs a file, and I'm trying to ensure
I have a batch script that looks like this: Test.bat @echo off :: Starts
I have a batch script that calls a process and currently it waits for
I'm writing a batch script that switches between ip addresses depending on the users
I have a nightly build DOS batch script that invokes devenv.exe to build a
Is there any way to link batch script (.bat files) so that they run
I am trying to modify a batch script that installs a simple script file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.