Is there an equivalent source command in Windows CMD as in bash or tcsh?

100,953

Solution 1

I am afraid not, but you can start using Powershell, which does support dot sourcing. Since powershell window is really based on cmd so all your dos command will continue to work, and you gain new power, much more power.

Solution 2

In the usual Windows command prompt (i.e. cmd.exe), just using call mybat.bat did what I wanted. I got all the environment variables it had set.

Solution 3

The dos shell will support .bat files containing just assignments to variables that, when executed, will create the variables in the current environment.

  c:> type EnvSetTest.bat
  set TESTXYZ=XYZ

  c:> .\EnvSetTest.bat

  c:> set | find "TESTX"
  TESTXYZ=XYZ
  c:>

IHTH.

Solution 4

Following example will help you to solve your problem.

env.bat This file is for setting variables. Its contents are given blow.

set name="test3"

test.bat Our main batch file.

call env.bat
call print.bat
pause

Now print.bat batch file to print variables. Its contents given below

echo %name%

Solution 5

The only way I have found this to work is to launch a new cmd window from my own config window. eg:

@echo off
echo Loading...
setlocal enabledelayedexpansion
call 1.cmd
call 2.bat
...
...
if "%LocalAppData%"=="" set LocalAppData=%UserProfile%\Local Settings\Application Data
SET BLAHNAME=FILE:%LocalAppData%\BLAH
call blah blah
cmd

The last cmd will launch a new cmd prompt with the desired settings exported to the command window.

Share:
100,953

Related videos on Youtube

mart2001
Author by

mart2001

Updated on July 09, 2022

Comments

  • mart2001
    mart2001 almost 2 years

    I know that in the unix world, if you edit your .profile or .cshrc file, you can do a source ~/.profile or source ~/.cshrc to get the effect on your current session. If I changed something in the system variable on Windows, how can I have it effect the current command prompt session without exiting the command prompt session and opening another command prompt session?

  • mart2001
    mart2001 almost 12 years
    Thanks John Shen. I have search the web and haven't found any equivalent. I have seen Powershell, perhaps I will start using that instead of the old command prompt.
  • BeniBela
    BeniBela about 11 years
    @thomasa88: you can click the down vote button again to remove the down vote
  • thomasa88
    thomasa88 about 11 years
    @BeniBela You can only undo the vote for a few minutes, then it gets locked. shellter: Vote corrected, thanks for the good answer :) Also found that setlocal can be used for local variables, and one can move data between local and global like this: wiki.answers.com/Q/…
  • Ajay Gautam
    Ajay Gautam about 11 years
    Couldn't get powershell to do this
  • Eryk Sun
    Eryk Sun about 6 years
    If mybat.bat runs other batch scripts, it needs to call them to allow execution to return to the current script.
  • Jebathon
    Jebathon over 5 years
    TLDR: Power! Unlimited power!
  • stelios
    stelios over 5 years
    call won't work for powershell scripts however. Suppose you want to import a function and invoke it, source would work in bash
  • Chinmay
    Chinmay almost 5 years
    This is a very old question, but I think this should be the correct answer. "call myfile.bat" --> reads the environment variables from the batch file and sets those in current environment from where "call" command is invoked. then all those variables are available for use in the current shell.
  • coolaj86
    coolaj86 almost 5 years
    Oh, and it was at a perfect 42... I almost wish I hadn't upvoted and just left it there. Thanks much! :)
  • Frank Buss
    Frank Buss over 3 years
    "call" worked for me, as the second answer with more than 10 upvotes proves, too.