如何避免在 Windows 中过度填充 PATH 环境变量? [英] How do you avoid over-populating the PATH Environment Variable in Windows?

查看:31
本文介绍了如何避免在 Windows 中过度填充 PATH 环境变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道您使用哪些方法来管理系统中的可执行文件.例如,我几乎可以通过命令行访问所有内容,但现在我遇到了路径字符串的限制,所以我无法再添加任何目录.

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?

推荐答案

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

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

C:	his_is_along_path	hat_appearsin_multiple_placessubdir1;
C:	his_is_along_path	hat_appearsin_multiple_placessubdir2;

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

then you can create a new environment variable such as

SET P1=C:	his_is_along_path	hat_appearsin_multiple_places

之后你的原始路径变成

%P1%subdir1;
%P1%subdir2;

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

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 查看较短的名称.

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 Files is typically equivalent to C:PROGRA~1. You can use dir /x to see the shorter names.

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

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

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

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

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

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

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.

@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:	his_isan_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%

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

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

%actualdir%\%actualfile% %args%

echo %actualdir%\%actualfile% %args%

理想情况下,您应该在运行之前确切地知道每一行的作用.

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

这篇关于如何避免在 Windows 中过度填充 PATH 环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆