How do you avoid over-populating the PATH Environment Variable in Windows?

I would like to know what are the approaches that you use to manage the executables in your system. For example I have almost everything accessible through the command line, but now I come to the limit of the path string, so i can't add any more dir.

So what do you recommend? A long time ago, I tried to use softLinks of the executables in a Dir that belonged to the path, but that approach didn't work. Throw the "executable only" to a known Dir,has the problems that almost any application require a set of files, so this also is bad. Throw the executable and all his files to a known Dir, mmm this will work, but the possibility to get a conflict in the name of the files is very very high. Create a HardLink? i don't know. What do you think?

82004 次浏览

Creating a folder c:\bin adding to your path and hardlinking like you said could shorten the string. Maybe add a variable pf to system vars with value c:\Program Files then replace c:\Program Files with %pf% in path.

编辑:

创建一个虚拟驱动器。 Subst p: “ c: program files”

我能想到的一种方法是使用其他环境变量来存储部分路径; 例如,如果有

C:\this_is_a\long_path\that_appears\in_multiple_places\subdir1;
C:\this_is_a\long_path\that_appears\in_multiple_places\subdir2;

然后你可以创建一个新的环境变量,比如

SET P1=C:\this_is_a\long_path\that_appears\in_multiple_places

在那之后,你原来的路径变成

%P1%\subdir1;
%P1%\subdir2;

EDIT: Another option is to create a bin directory that holds .bat files that point to the appropriate .exe files.

编辑2: Ben Voigt 对另一个答案的评论提到,使用建议的其他环境变量可能不会减少 %PATH%的长度,因为它们会在存储之前被展开。这可能是真的,我还没有测试过。不过,另一种选择是对较长的目录名使用8dot3表单,例如 C:\Program Files通常等效于 C:\PROGRA~1。您可以使用 dir /x查看较短的名称。

编辑3: 这个简单的测试让我相信 Ben Voigt 是对的。

set test1=hello
set test2=%test1%hello
set test1=bye
echo %test2%

最后,您将看到输出 hellohello而不是 byehello

编辑4: 如果您决定使用批处理文件来消除 %PATH%中的某些路径,您可能会关心如何将参数从批处理文件传递到您的可执行文件,从而使过程是透明的(即,您不会注意到调用批处理文件和调用可执行文件之间的任何区别)。我没有很多编写批处理文件的经验,但这似乎工作得很好。

@echo off


rem This batch file points to an executable of the same name
rem that is located in another directory. Specify the directory
rem here:


set actualdir=c:\this_is\an_example_path


rem You do not need to change anything that follows.


set actualfile=%0
set args=%1
:beginloop
if "%1" == "" goto endloop
shift
set args=%args% %1
goto beginloop
:endloop
%actualdir%\%actualfile% %args%

一般来说,你应该小心从互联网上运行批处理文件,因为你可以用批处理文件做各种各样的事情,比如格式化你的硬盘驱动器。如果您不信任上面的代码(这是我写的) ,您可以通过替换这一行来测试它

%actualdir%\%actualfile% %args%

echo %actualdir%\%actualfile% %args%

理想情况下,您应该在运行每一行之前准确地知道它们的作用。

另一个想法是: 使用 DIR/X 确定为非8dot3文件生成的短名称 names. Then use these in your %PATH%.

例如,‘ C: Program Files’变成‘ C: PROGRA ~ 1’。

I generally don't have to worry about this (I haven't run into a path size limit - I don't even know what that is on modern Windows systems), but here's what I might do to avoid putting a program's directory in the path:

  • most command line utilities get thrown into a c:\util directory that's on the path
  • 否则,我将向 c:\util目录添加一个简单的 cmd/batch 文件,该文件类似于:

    @"c:\program files\whereever\foo.exe" %*
    

which essentially creates an alias for the command. It's not necessarily perfect. Some programs really insist on being in the path (that's pretty rare nowadays), and other programs that try to invoke it might not find it properly. But for most uses it works well.

But generally, I haven't had to worry about avoiding adding directories to the path.

如果有人感兴趣的话..。

我发现我从来没有真正需要所有这些路径一次,所以我创建了一堆“初始化”批处理文件,修改相应的路径。

例如,如果我想在 Eclipse 中进行一些 C + + 开发,我会这样做:

> initmingw
> initeclipse
> eclipse

这对于避免具有相同名称的可执行文件之间的冲突也很方便(例如 C + + 和 D 编译器,它们都具有 make.exe)。

我的批处理文件通常是这样的:

@echo off
set PATH=C:\Path\To\My\Stuff1;%PATH%
set PATH=C:\Path\To\My\Stuff2;%PATH%

我发现这种方法相对干净,并且还没有遇到任何问题。

使用 App Path 注册表项而不是特定于应用程序的路径的路径变量:

http://msdn.microsoft.com/en-us/library/windows/desktop/ee872121(v=vs.85).aspx

这将解析你的% PATH% 环境变量,并将每个目录转换为其等效的短名称,然后将它们拼接在一起:

@echo off


SET MyPath=%PATH%
echo %MyPath%
echo --


setlocal EnableDelayedExpansion


SET TempPath="%MyPath:;=";"%"
SET var=
FOR %%a IN (%TempPath%) DO (
IF exist %%~sa (
SET "var=!var!;%%~sa"
) ELSE (
echo %%a does not exist
)
)


echo --
echo !var:~1!

获取输出并更新环境变量中的 PATH 变量。

如果您使用的是 windows vista 或更高版本,您可以建立一个到该文件夹的符号链接。例如:

mklink /d C:\pf "C:\Program Files"

would make a link so c:\pf would be your program files folder. I shaved off 300 characters from my path by using this trick.

我按照以下步骤来管理这些条目:

  1. 为不同的软件包使用组合创建不同的用户。 示例: (a)创建一个用户网站,以提供所有网站开发软件; (b)创建一个用户数据库,以提供所有数据库和数据仓库软件包。记住,有些软件可能创建多个条目。或者有时我把它分成特定于 Oracle、特定于 MSSQL 和特定于 Oracle 的用户。我把 MySQL/PostgreSQL,tomcat,wamp,xamp 都输入到用户帐户 webr 中。

  2. 如果可能的话,安装一些常见的软件包,比如 office,photoshop。.作为系统特定的可用于所有用户和特殊包作为用户特定。当然,我必须登录到不同的用户并安装它们。并非所有软件都提供此选项。如果“仅为此用户安装”选项不可用,请为整个系统安装该选项。

  3. I avoid installing programs in to the folder Program File (x86) or in to Program File. I always install into the base directory. For example MySQL 64 bit goes into "C:\mysql64" and MySQL 32 bit goes into "C:\mysql" folder. I always assume adding a suffix 64 only for 64bit software. If no suffix, then it is a 32 bit. I follow the same thing to Java and others. This way my path will be shorter, not including "C:\Program File (x86)". For some software the configuration file may need to be edited to show where exactly the .exe file is. Only program that demands to be installed into "C:\Program File (x86)" will be installed into that folder. Always I remember to shorten the names. I avoid version number like tomcat/release/version-2.5.0.3 such details. If I need to the know version, I create a file by name version and put it into the tomcat folder. In general shorten the link as much as possible.

  4. 如果上述所有步骤都超过了 Windows 限制,则包含任何批处理以替换指向路径的缩写链接。

然后登录到特定用途(移动应用程序,或数据库/数据仓库或网络开发。...)用户和做相关的任务。

还可以在窗口中创建虚拟窗口。只要您有一个授权的操作系统副本,就可以创建具有相同键的多个虚拟窗口。您可以将特定于特定任务的包放入该机器中。每次都必须启动单独的 VM。一些内存密集型的软件包,如3D 动画电影制作者都应该放入主机,而不是 VM,因为 VM 将只有一部分内存可供其使用。不过,引导每个 VM 都是一个痛苦的过程。

我每次都编写并使用标准流(stdin/stderr/stdout)和退出代码 PROXY 程序(称为调度程序 https://github.com/131/dispatcher)

All CLI program i use (node, php, python, git, svn, rsync, plink ...) i'm using are actually the same exe file (around 10kb, that i just name differently), that i put in the same directory. A dummy static clear text file do the "proxy file name to real exe mapping".

调度员使用低层过程管理 win32API 绝对透明。

使用这个软件,我只有一个额外的目录设置在我的 PATH 为所有程序我可能使用。

以上的解决方案只有在你能修剪你的路径时才有效。在我的例子中,这并不是一个真正的选项,而且每次我打开命令提示符时都必须运行一个脚本是一件很麻烦的事情。因此,我编写了一个简单的脚本,它在打开命令提示符时自动运行,并将文本文件的内容追加到路径中。

There are also some contexts where having this script run breaks things (say, in a github or cygwin shell), so I also added a file that contains a list of paths that, if the command prompt is started in them, the path variable isn't changed via the startup script that normally updates the path.

@echo off


:: Modify these to the actual paths of these two files
set dontSetupFile=C:\Users\Yams\Dontsetup.txt
set pathFile=C:\Users\Yams\Path.txt


:: Retrieve the current path (for determining whether or not we should append to our path)
set curDir=%cd%


:: Be done if the current path is listed in the dontSetupFile
SetLocal EnableDelayedExpansion
for /F "delims=" %%i in (%dontSetupFile%) do (
if "%%i"=="%curDir%" GOTO AllDone
)






:: Append the pathFile to our current PATH
set pathAppend=
for /F "delims=" %%i in (%pathFile%) do (set pathAppend=!pathAppend!%%i)


set PATH=%PATH%;%pathAppend%




:: The only way to actually modify a command prompt's path via a batch file is by starting
::   up another command prompt window. So we will do this, however, if this script is
::   automatically called on startup of any command prompt window, it will infinately
::   recurse and bad things will happen.


:: If we already ran, we are done
if "%yams%"=="onion" GOTO AllDone


:: Otherwise, flag that we just ran, and then start up a new command prompt window
::   with this flag set
set yams=onion


cmd \K set PATH=%PATH%;


:: When that command prompt exits, it will load back up this command prompt window, and
::   then the user will need to exit out of this as well. This causes this window to
::   automatically exit once the cmd it just spawned is closed.
exit()


:: Path is set up, we are done!
:AllDone
@echo on

Path.txt 看起来像

C:\Program Files (x86)\Google\google_appengine;
C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;
C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;
C:\Program Files\Microsoft SQL Server\110\Tools\Binn;
C:\Program Files\Microsoft DNX\Dnvm;
C:\Program Files (x86)\Windows Kits\8.0\Windows Performance Toolkit;

While Dontsetup.txt will look something like

C:\Program Files (x86)\Windows Kits\8.0\Windows Performance Toolkit
C:\Program Files (x86)\Git\cmd
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin

要在启动时自动运行该命令,打开 regedit,导航到 HKEY _ LOCAL _ MACHINE/SOFTWARE/Microsoft/Command Processor,然后右键单击右键并按 new-> Multi-String Value。命名为自动运行。设置它的值为

C:\Users\Yams\setUpPath.bat

或存储上述批处理文件的其他地方。

没有尝试,但是分割 PATH 的部分工作,并加入他们在最终变量的工作?

例如,首先假设您有一个类似于

PATH={LONGPATH1};{LONGPATH2};....{2048th char}....{LONGPATH_N-1};{LONGPATH_N}

相反,你要创造:

_PATH1 = {LONGPATH1};{LONGPATH2};....{2048 char}
_PATH2 = {2049th char}...{LONGPATH_N-1};{LONGPATH_N}
rem // may be more parts
PATH = %_PATH1%;%_PATH2%