如何避免在 Windows 中过度填充 PATH 环境变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4405091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 07:51:34  来源:igfitidea点击:

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

windowspathenvironment-variablesexecutable

提问by mjsr

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?

那么你有什么推荐呢?很久以前,我尝试在属于路径的目录中使用可执行文件的软链接,但这种方法不起作用。将“仅可执行文件”扔到已知的目录,存在几乎所有应用程序都需要一组文件的问题,因此这也很糟糕。将可执行文件和他的所有文件扔到一个已知的目录中,嗯,这会起作用,但是文件名发生冲突的可能性非常高。创建硬链接?我不知道。你怎么认为?

采纳答案by Mitch Schwartz

One way I can think of is to use other environment variables to store partial paths; for example, if you have

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

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

then you can create a new environment variable such as

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

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

after which your original paths become

之后你的原始路径变成

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

EDIT:Another option is to create a bindirectory that holds .batfiles that point to the appropriate .exefiles.

编辑:另一种选择是创建一个bin目录,该目录包含.bat指向相应.exe文件的文件。

EDIT 2:Ben Voigt's comment to another answer mentions that using other environment variables as suggested might not reduce the length of %PATH%because they would be expanded prior to being stored. This may be true and I have not tested for it. Another option though is to use 8dot3 forms for longer directory names, for example C:\Program Filesis typically equivalent to C:\PROGRA~1. You can use dir /xto see the shorter names.

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

EDIT 3:This simple test leads me to believe Ben Voigt is right.

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

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

At the end of this, you see output hellohellorather than byehello.

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

EDIT 4:In case you decide to use batch files to eliminate certain paths from %PATH%, you might be concerned about how to pass on arguments from your batch file to your executable such that the process is transparent (i.e., you won't notice any difference between calling the batch file and calling the executable). I don't have a whole lot of experience writing batch files, but this seems to work fine.

编辑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%

As a general rule, you should be careful about running batch files from the internet, since you can do all sorts of things with batch files such as formatting your hard drive. If you don't trust the code above (which I wrote), you can test it by replacing the line

作为一般规则,您应该小心从 Internet 运行批处理文件,因为您可以使用批处理文件执行各种操作,例如格式化硬盘驱动器。如果你不相信上面的代码(我写的),你可以通过替换该行来测试它

%actualdir%\%actualfile% %args%

with

echo %actualdir%\%actualfile% %args%

Ideally you should know exactly what every line does before you run it.

理想情况下,您应该在运行之前确切地知道每条线路的作用。

回答by Todd Smith

This will parse your %PATH% environment variable and convert each directory to its shortname equivalent and then piece it all back together:

这将解析您的 %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!

Take the output and update the PATH variable in environment variables.

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

回答by bmg002

if you are using windows vista or higher, you can make a symbolic link to the folder. for example:

如果您使用的是 windows vista 或更高版本,则可以创建指向该文件夹的符号链接。例如:

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

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

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

会做一个链接,所以c:\pf会是你的program files文件夹。通过使用这个技巧,我从我的路径中删除了 300 个字符。

回答by YellPika

In case anyone's interested...

如果有人有兴趣...

I find I never really need all those paths at once, so I create a bunch of "initialization" batch files which modify the path accordingly.

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

For example, if I wanted to do some C++ development in Eclipse, I would do:

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

> initmingw
> initeclipse
> eclipse

This is also handy for avoiding conflicts between executables with the same name (such as the C++ and D compilers, which both have a make.exe).

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

My batch files typically look like this:

我的批处理文件通常如下所示:

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

I find this approach relatively clean and have yet to run into any problems with it.

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

回答by Michael Burr

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:

我通常不必担心这个(我没有遇到路径大小限制 - 我什至不知道现代 Windows 系统上是什么),但这是我可能会做的事情来避免将程序目录放入路径:

  • most command line utilities get thrown into a c:\utildirectory that's on the path
  • otherwise, I'll add a simple cmd/batch file to the c:\utildirectory that looks something like:

    @"c:\program files\whereever\foo.exe" %*
    
  • 大多数命令行实用程序都被扔到c:\util路径上的目录中
  • 否则,我会将一个简单的 cmd/batch 文件添加到c:\util类似于以下内容的目录中:

    @"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.

但一般来说,我不必担心避免向路径添加目录。

回答by Mad Hatter

USe the App Path registry key instead of the path variable for application-specific paths:

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

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

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

回答by Android Eve

Another idea: Use DIR /X to determine the short names generated for non-8dot3 file names. Then use these in your %PATH%.

另一个想法:使用 DIR /X 确定为非 8dot3 文件名生成的短名称。然后在您的 %PATH% 中使用这些。

For example, 'C:\Program Files' becomes 'C:\PROGRA~1'.

例如,“C:\Program Files”变为“C:\PROGRA~1”。

回答by 131

I wrote and use on a every-time basis a standard stream (stdin/stderr/stdout) & exit code PROXY program (called dispatcher https://github.com/131/dispatcher)

我每次都编写并使用标准流(stdin/stderr/stdout)和退出代码代理程序(称为调度程序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".

我使用的所有 CLI 程序(node、php、python、git、svn、rsync、plink ...)实际上都是相同的 exe 文件(大约 10kb,我只是命名不同),我放在相同的目录。一个虚拟的静态明文文件执行“代理文件名到真实 exe 映射”。

Dispatcher use low level Process management win32 API to be absolutly transparent.

Dispatcher 使用低级进程管理 win32 API 是绝对透明的。

Using this software, i only do have ONE additionnal directory set in my PATH for all programs i might use.

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

回答by troynt

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.

创建一个文件夹 c:\bin 添加到您的路径并像您说的那样进行硬链接可以缩短字符串。也许将变量 pf 添加到系统变量中,值为 c:\Program Files 然后用路径中的 %pf% 替换 c:\Program Files。

Edit:

编辑:

Create a virtual drive. subst p: "c:\program files"

创建一个虚拟驱动器。subst p: "c:\program files"

回答by Dr. A. Anukanth

I follow these steps to make the entries manageable:

我按照以下步骤使条目易于管理:

  1. Created different users for different combination of software packages usage. Example: (a) Created a user web for making available all the web development software; (b) Created a user database for making available all the database and data warehousing software packages. Remember some software may create more than one entry. Or sometime I break this into oracle specific and MSSQL specific and oracle specific users. I put MySQL/PostgreSQL, tomcat, wamp, xamp all into the user account webr.

  2. If possible install common packages like office, photoshop, .. as system specific available for all users and special packages as user specific. Of course I had to log into different users and install them. Not all software may provide this option. If "install for this user only" option is not available, install it for the whole system.

  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. Include any batch to replace abbreviated link to the path, if all the above steps passed the Windows limit.

  1. 为不同的软件包使用组合创建了不同的用户。示例: (a) 创建一个用户网络,以提供所有网络开发软件;(b) 创建了一个用户数据库,以提供所有数据库和数据仓库软件包。请记住,某些软件可能会创建多个条目。或者有时我将其分解为特定于 oracle 的用户和特定于 MSSQL 的用户以及特定于 oracle 的用户。我把 MySQL/PostgreSQL、tomcat、wamp、xamp 都放到了用户账户 webr 中。

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

  3. 我避免将程序安装到文件夹 Program File (x86) 或 Program File 中。我总是安装到基本目录中。例如,MySQL 64 位进入“C:\mysql64”,MySQL 32 位进入“C:\mysql”文件夹。我总是假设只为 64 位软件添加后缀 64。如果没有后缀,则是 32 位。我对 Java 和其他人也遵循同样的做法。这样我的路径会更短,不包括“C:\Program File (x86)”。对于某些软件,可能需要编辑配置文件以显示 .exe 文件的确切位置。只有需要安装到“C:\Program File (x86)”的程序才会安装到该文件夹​​中。我总是记得缩短名称。我避免像 tomcat/release/version-2.5.0.3 这样的版本号这样的细节。如果我需要知道版本,我按名称版本创建一个文件并将其放入 tomcat 文件夹中。一般来说,尽可能缩短链接。

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

Then Log into usage specific (mobile application, or database/data warehousing or web-development.. ..) user and do the relevant tasks.

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

You can also create virtual windows within windows. As long as you have one licensed OS copy, creating multiple virtual windows with same key is possible. You can put packages specific for a particular task in that machine. You have to launch separate VM each time. Some memory intensive packages like 3D animation movie makers all should be put into the main machine, not into VM as VM will have only a part of the RAM available for its use. It is a pain to boot each VM though.

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