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

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

问题描述

我想知道您用来管理系统中的可执行文件的方法。例如,我几乎可以通过命令行访问所有内容,但现在我来到路径字符串的极限,所以我不能添加任何更多的目录。



所以你有什么建议?
很久以前,我试图在属于路径的目录中使用可执行文件的softLink,但是这种方法不起作用。
将可执行文件抛出到已知的目录中,有几乎任何应用程序需要一组文件的问题,所以这也是坏的。
将可执行文件及其所有文件抛出一个已知的目录,mmm将会起作用,但是以文件名称冲突的可能性非常高。
创建一个HardLink?我不知道。你觉得怎么样?

解决方案

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

  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;

编辑:另一个选项是创建一个 bin 目录,其中保存 .bat 指向相应的 .exe文件的文件。 / p>

编辑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此批处理文件指向位于另一目录中的同名的
rem的可执行文件。指定目录
rem here:

set actualdir = c:\this_is\an_example_path

rem您不需要更改以下任何内容。

set actualfile =%0
set args =%1
:beginloop
如果%1==goto endloop
shift
set args =%args%%1
goto beginloop
:endloop
%actualdir%\%actualfile%%args%

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

 %actualdir% \%actualfile%%args%

  echo%actualdir%\%actualfile%%args%

理想情况下,您应该知道每一行在运行之前做什么。


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:\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 bin directory that holds .bat files that point to the appropriate .exe files.

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.

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%

At the end of this, you see output hellohello rather than 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.

@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

%actualdir%\%actualfile% %args%

with

echo %actualdir%\%actualfile% %args%

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

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

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