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 8953337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:04:23+00:00 2026-06-15T14:04:23+00:00

There are two (2) Windows batch scripts. The first one will CALL the second

  • 0

There are two (2) Windows batch scripts. The first one will CALL the second one. However, SETLOCAL must be used around the call to the second batch script because ENABLEDELAYEDEXPANSION is needed in a FOR loop.

=== batch1.bat

@echo off
echo in %0
set BAT1_VAR=5
SETLOCAL
    call  batch2.bat
    set|find "BAT"
    echo === before endlocal
ENDLOCAL
set|find "BAT"
echo === after endlocal
echo === begin exiting %0
set|find "BAT"
echo === end exiting %0
exit /B 0

=== batch2.bat

@echo off
echo in %0
SET BAT2_VAR=7
echo === begin exiting %0
SET|FIND "BAT"
echo === end exiting %0
EXIT /B 0

===
The problem is that the variable set by batch2.bat needs to still exist after the return to batch1.bat. This can be resolved by using:

ENDLOCAL & set BAT2_VAR=%BAT2_VAR%

HOWEVER, I do not want batch1.bat to need to know the names of variables created by batch2.bat. There might be many and the list may change.

Any ideas to overcome this problem? Thanks.

  • 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-15T14:04:24+00:00Added an answer on June 15, 2026 at 2:04 pm

    If you are in a loop, or any parenthesized block of code, then you cannot use the ENDLOCAL & set BAT2_VAR=%BAT2_VAR% method.

    Something like this would fail

    REM delayed expansion is disabled to start
    for ... %%A in (....) do (
       REM must SET before enabling delayed expansion to preserve ! in %%A value
       set var=%%A
       REM the remainder of the loop needs delayed expansion
       setlocal enableDelayedExpansion
       REM do something with !var!
       call batch2 !var!
       REM maybe the batch2 modifies var some more
       REM Must endlocal and preserve the value. The attempt below fails because %var% will
       REM be the value that existed before the loop was entered
       endlocal & set "var=%var%"
    )
    

    A simple fix is to transfer the value via a FOR variable

    REM delayed expansion is disabled to start
    for ... %%A in (....) do (
       REM must SET before enabling delayed expansion to preserve ! in %%A value
       set var=%%A
       REM the remainder of the loop needs delayed expansion
       setlocal enableDelayedExpansion
       REM do something with !var!
       call batch2 !var!
       REM maybe the batch2 modifies var some more
       REM the code below successfully transports the value across the endlocal barrier
       for /f "delims=" %%B in ("!var!") do endlocal & set "var=%%B"
    )
    

    You say your batch might set many values you need to preserve. Normally you pass the names of the return variables into your CALLed routine, and then your routine could do something like:

    set "%~2=return value1"
    set "%~3=return value2"
    ... etc.
    

    You would know the names of all the variables, but that might require a lot of code to transport all values accross the endlocal barrier.

    You could prefix all the variables with a common prefix, and then use a FOR /F loop to preserve the values. The entire result set of a FOR /F (‘command’) is cached before any of the iterations begin.

    Your CALLed batch could set variables prefix.var1, prefix.var2, … prefix.varN, and the following code would work

    REM delayed expansion is disabled to start
    for ... %%A in (....) do (
       REM must SET before enabling delayed expansion to preserve ! in %%A value
       set var=%%A
       REM the remainder of the loop needs delayed expansion
       setlocal enableDelayedExpansion
       REM do something with !var!
       call batch2 !var! prefix.
       REM the batch2 set variables prefixed by prefix.
       REM Must endlocal and preserve all values.
       for /f "delims=" %%B in ('set prefix.') do (
         if "!"=="" endlocal
         set "%%B"
       )
    )
    

    The if "!"=="" endlocal line is a nifty trick to only endlocal on the 1st inner loop iteration. It relies on the fact that delayed expansion was disabled before the outer loop, and enabled near the top of each outer loop iteration.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

is there any way to dock two windows(like Winamp does with panels), but one
There are two WinRT applications (C#/Xaml if it matters) on Windows 8. The first
Is there any way to bind two windows from seprate processes together using Python/Pygame?
When installing Windows services there are two options for automatically starting a Windows service
There are two objects. The Windows Form with a button and a progress bar,
In System.Windows.Forms.Application there are two properties called LocalUserAppDataPath and UserAppDataPath . On this computer
There are two table s : one is the master and one the detail
I am currently building two windows services: The first takes messages from an internet
I have two .exe file and 3 windows batch files and i need to
I have a two windows, one is opened from another, so, I have an

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.