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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T06:13:52+00:00 2026-05-17T06:13:52+00:00

I frequently find myself using setlocal within cmd.exe to avoid polluting the environment variable

  • 0

I frequently find myself using setlocal within cmd.exe to avoid polluting the environment variable space with temporary variables (and to ensure both command extensions and delayed expansion are active).

However, I’m at a loss on how to do this if I actually want one of those variables to be made available.

Consider the following code which gives you the final component of the current directory (so c:\pax\dir1 would give you dir1):

        @echo off
    :main
        setlocal enableextensions enabledelayedexpansion
        call :func
        echo.main_folder = %folder%
        endlocal
        goto :eof

    :func
        setlocal enableextensions enabledelayedexpansion
        set temp=%cd%
        set folder=
    :funcloop1
        if not "x%temp:~-1%"=="x\" (
            set folder=!temp:~-1!!folder!
            set temp=!temp:~1,-1!
            goto :funcloop1
        )
        echo.func_folder = %folder%
        endlocal
        goto :eof

When I run this, I get the output:

func_folder = dir1
main_folder =

and you can see that the %folder% variable does not survive the endlocal. However, if I leave off the setlocal/endlocal from the function, the temp varaible pollutes the namespace of main.

I know I can just use set temp= at the end of func and this will remove the environment variable but that doesn’t cover the case where I’m already using that variable in the outside scope (it gets totally destroyed in that case).

Is there a way in cmd.exe to allow a select group of environment variables to propogate to the outside scope while still preventing others from affecting it? In other words, can you have both local variables and return variables?


Aside, please don’t tell me there’s a better way of getting the final path component. That is an example case only.

Actually, you can tell me a better way since it’d be nice to know but it won’t be answering my specific question 🙂

  • 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-05-17T06:13:53+00:00Added an answer on May 17, 2026 at 6:13 am

    “The solution to this is to take advantage of the fact that the CMD shell evaluates variables on a line-by-line basis – so placing ENDLOCAL on the same line as the SET statement(s) gives the result we want:” source: ss64.com

    So changing the endlocal of func: to this…

    endlocal & set folder=%folder%
    

    …will get you the behaviour you want.

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

Sidebar

Related Questions

No related questions found

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.