Is there an equivalent source command in Windows C

2019-01-11 06:24发布

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?

8条回答
Deceive 欺骗
2楼-- · 2019-01-11 06:51

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.

查看更多
狗以群分
3楼-- · 2019-01-11 06:53

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.

查看更多
Rolldiameter
4楼-- · 2019-01-11 07:00

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.

查看更多
我命由我不由天
5楼-- · 2019-01-11 07:03

Use git bash for windows, it works totally fine!

查看更多
戒情不戒烟
6楼-- · 2019-01-11 07:07

For example to set VC# vars

C:\Windows\System32\cmd.exe /k "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat"
查看更多
Fickle 薄情
7楼-- · 2019-01-11 07:08

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.

查看更多
登录 后发表回答