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

  • Home
  • SEARCH
  • 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 8426841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:38:17+00:00 2026-06-10T04:38:17+00:00

Context : I need to call a Windows batch script which would update my

  • 0

Context: I need to call a Windows batch script which would update my PATH by adding another path ‘xxx‘ at the end of it, but:

  • without any duplicate
    (if I add ‘xxx‘ to a PATH like ‘aaa;xxx;bbb‘, I need an updated PATH like ‘aaa;bbb;xxx‘)
  • without any aggregation
    (I can call the script repeatedly without ending up with ‘aaa;bbb;xxx;xxx;xxx;...‘)

What I have tried:

The following function takes care of any duplicate and does the job

:cleanAddPath -- remove %~1 from PATH, add it at the end of PATH
SETLOCAL ENABLEDELAYEDEXPANSION
set PATH=!PATH:%~2=!
set PATH=!PATH:;;=;!
set PATH=%PATH%;%~2
set P=!P:;;=;!
echo %PATH%
echo -------------
ENDLOCAL  
exit /b

But, it needs delayed expansion local mode, which means: at the end of the script (or here, at the end of the function cleanAddPath), whatever has been set for %PATH% is thrown away.

I could ask the users (for which I write the script) to launch their cmd with a cmd /V:ON option (activating the delayed expansion, otherwise off by default), but that is not practical.

How can I modify the PATH variable the way I described above, and still have it updated in my current DOS session after calling said script?

  • 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-10T04:38:18+00:00Added an answer on June 10, 2026 at 4:38 am

    The page “DOS – Function Collection” gives great example on how a function can return a value in DOS, even when using delayed expansion mode:

    The following function will update any variable you want with an addition PATH:

    :cleanAddPath -- remove %~2 from %~1, add it at the end of %~1
    SETLOCAL ENABLEDELAYEDEXPANSION
    set P=!%~1!
    set P=!P:%~2=!
    set P=!P:;;=;!
    set P=!P!;%~2
    set P=!P:;;=;!
    (ENDLOCAL & REM.-- RETURN VALUES
      SET "%~1=%P%"
    )
    exit /b
    

    Note the concatenation of paths using. As jeb comments:

    The line set P=%P%;%~2 is critical if your path contains ampersands like in C:\Documents&Settings.
    Better change to set “P=!P!;%~2“.

    The SET "%~1=%P%" is the part which allows to memorize (in the variable represented by %~1) the value you have set using delayed expansion features.
    I initially used SET "%~1=%P%" !, but jeb comments:

    The command SET "%~1=%P%" ! could be simplified to SET "%~1=%P%" as the trailing exclamation mark has only a (good) effect in delayed expansion mode and if you prepared %P% before.

    To update your PATH variable, you would call your function with:

    call :cleanAddPath PATH "C:\my\path\to\add"
    

    And it will persists after leaving that script, for your current DOS session.

    dbenham‘s answer points to a more robust answer (upvoted), but in my case this script is enough.

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

Sidebar

Related Questions

I need the context to ApplicationContext.xml ,which I provided in web.xml as <listener> <listener-class>
I have a problem to test my non activity-class which need the context of
I am using a C# Application to call a Batch file which compiles and
I call ant command in my ruby script (on Windows XP) using %x[]. I
I'm refining some code which simulated a context-switching scheduler on x86 Windows systems. The
Context: I need to develop a monitoring server that monitors some of our applications
On an app that I'm working on, I need a context menu to show
I need to save the context of the program before exiting ... I've put
I need to get the current context given an entity I found this old
I need to know, in a context of a unit test, if Jetbrains IntelliJ

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.