如何避免在Windows中过度填充PATH环境变量?
我想知道您使用什么方法来管理系统中的可执行文件。例如,我几乎可以通过命令行访问所有内容,但现在我已经到了路径字符串的极限,因此我无法再添加任何dir 那你有什么建议? 很久以前,我尝试在属于路径的目录中使用可执行文件的软链接,但这种方法不起作用。 将“仅可执行文件”抛出到一个已知的目录,几乎任何应用程序都需要一组文件,因此这也是不好的。 将可执行文件和他的所有文件扔到一个已知的目录下,嗯,这会起作用,但是在文件名中发生冲突的可能性非常高。如何避免在Windows中过度填充PATH环境变量?,windows,path,environment-variables,executable,Windows,Path,Environment Variables,Executable,我想知道您使用什么方法来管理系统中的可执行文件。例如,我几乎可以通过命令行访问所有内容,但现在我已经到了路径字符串的极限,因此我无法再添加任何dir 那你有什么建议? 很久以前,我尝试在属于路径的目录中使用可执行文件的软链接,但这种方法不起作用。 将“仅可执行文件”抛出到一个已知的目录,几乎任何应用程序都需要一组文件,因此这也是不好的。 将可执行文件和他的所有文件扔到一个已知的目录下,嗯,这会起作用,但是在文件名中发生冲突的可能性非常高。 创建硬链接?我不知道。你觉得怎么样?创建一个文件夹c:\
创建硬链接?我不知道。你觉得怎么样?创建一个文件夹c:\bin,如你所说,添加到路径和硬链接,可以缩短字符串。可能会向系统变量添加一个值为c:\Program Files的变量pf,然后将路径中的c:\Program Files替换为%pf% 编辑: 创建一个虚拟驱动器。
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;
编辑:另一个选项是创建一个bin
目录,其中包含指向相应.exe
文件的.bat
文件
编辑2:Ben Voigt对另一个答案的评论提到,使用建议的其他环境变量可能不会减少%PATH%
的长度,因为它们在存储之前会被扩展。这可能是真的,我还没有测试过。另一种选择是使用8dot3表单来表示较长的目录名,例如C:\ProgramFiles
通常相当于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%
一般来说,从internet上运行批处理文件时应该小心,因为批处理文件可以执行各种操作,例如格式化硬盘驱动器。如果您不信任上面的代码(我编写的代码),可以通过替换行来测试它
%actualdir%\%actualfile% %args%
与
理想情况下,在运行每一行之前,您应该确切地知道它的作用。另一个想法:使用DIR/X来确定为非8dot3文件生成的短名称 名字。然后在%PATH%中使用这些
例如,“C:\Program Files”变为“C:\PROGRA~1”。我通常不必担心这一点(我没有遇到路径大小限制-我甚至不知道现代Windows系统上有什么限制),但我可以做以下几点来避免将程序目录放入路径中:
- 大多数命令行实用程序被抛出到路径上的
目录中c:\util
- 否则,我将向
目录添加一个简单的cmd/batch文件,该文件如下所示:c:\util
@"c:\program files\whereever\foo.exe" %*
但一般来说,我不必担心避免向路径中添加目录。以防有人感兴趣
@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
我发现我从来都不需要同时使用所有这些路径,所以我创建了一堆“初始化”批处理文件,这些文件会相应地修改路径
例如,如果我想在Eclipse做一些C++开发,我会做:
> initmingw
> initeclipse
> eclipse
<>这也是为了避免相同名称的可执行文件之间的冲突(例如C++和D编译器,它们都有一个.exe)。
我的批处理文件通常如下所示:
@echo off
set PATH=C:\Path\To\My\Stuff1;%PATH%
set PATH=C:\Path\To\My\Stuff2;%PATH%
我发现这种方法相对干净,还没有遇到任何问题。对于特定于应用程序的路径,请使用App Path注册表项而不是Path变量:
这将解析您的%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”
将创建一个链接,使
c:\pf
成为您的程序文件
文件夹。我使用此技巧从路径中删除了300个字符。我按照以下步骤使条目易于管理:
@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
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;
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
C:\Users\Yams\setUpPath.bat
_PATH1 = {LONGPATH1};{LONGPATH2};....{2048 char}
_PATH2 = {2049th char}...{LONGPATH_N-1};{LONGPATH_N}
rem // may be more parts
PATH = %_PATH1%;%_PATH2%