所以,我有这个批处理文件,据说将我的脚本附加到路径变量
@echo OFF
setx path "%path%;%cd%\script.py"
但是我遇到了一些问题。
警告:正在保存的数据被截断为1024个字符。
成功:保存了指定的值。
我的问题是,如何克服1024个字符的限制?
如何正确地将script.py附加到用户 PATH而不复制系统 PATH中的那些?
示例:
原始
USER PATH:C:\ dirA; ...; C:\ dirB;
系统路径:C:\ dirC; ...; C:\ dirD;
运行脚本后......
预期
USER PATH:C:\ dirA; ...; C:\ dirB; C:\ something \ script.py
系统路径:C:\ dirC; ...; C:\ dirD;
实际
USER PATH:C:\ dirC; ...; C:\ dirD; C:\ somet(截断为1024个字符)
系统路径:C:\ dirC; ...; C:\ dirD;
答案 0 :(得分:1)
下一个脚本显示了一种可能的方法。
@ECHO OFF >NUL
SETLOCAL enableextensions
rem enabledelayedexpansion
echo adding "%~1" to the user level HKCU\Environment /v Path
call :showReg old
call set "expanded=%~1"
if "%expanded%"=="" goto :usage
if not exist "%expanded%\" goto :usage
set "HKCU_type=REG_EXPAND_SZ"
set "HKCU_path="
for /F "tokens=1,2*" %%F in ('
reg query HKCU\Environment /v Path 2^>NUL ^|findstr /I "path"
') do (
set "HKCU_path=%%H"
REG ADD HKCU\Environment /v Path /t %HKCU_type% /d %%H;%~1 /f >NUL
)
if not defined HKCU_path (
REG ADD HKCU\Environment /v Path /t %HKCU_type% /d %~1 /f >NUL
)
:endlocal
call :showReg new
ENDLOCAL
goto :eof
:usage
echo directory "%~1" ^("%expanded%"^) not found
goto :endlocal
:showReg
<NUL set /P "=%~1: "
reg query HKCU\Environment /v Path 2>NUL|findstr /I "path"|findstr /V /R "^$"
if errorlevel 1 echo not defined
goto :eof
提供的示例显示了添加
的尝试d:\FooBar
(已拒绝); d:\bat
(硬编码参考); %SystemRoot%
(变量引用,在注册表中硬编码); ^%windir^%
(变量引用在注册表中保持可扩展)。<强>输出强>:
==>31602391.bat d:\FooBar
adding "d:\FooBar" to the user level HKCU\Environment /v Path
old: not defined
directory "d:\FooBar" ("d:\FooBar") not found
new: not defined
==>31602391.bat d:\test
adding "d:\test" to the user level HKCU\Environment /v Path
old: not defined
new: Path REG_EXPAND_SZ d:\test
==>31602391.bat %SystemRoot%
adding "C:\Windows" to the user level HKCU\Environment /v Path
old: Path REG_EXPAND_SZ d:\test
new: Path REG_EXPAND_SZ d:\test;C:\Windows
==>31602391.bat ^%windir^%
adding "%windir%" to the user level HKCU\Environment /v Path
old: Path REG_EXPAND_SZ d:\test;C:\Windows
new: Path REG_EXPAND_SZ d:\test;C:\Windows;%windir%
==>
答案 1 :(得分:0)
@echo OFF
for /f "skip=2 tokens=3*" %%a in ('reg query HKCU\Environment /v PATH') do (
if [%%b]==[] (
setx PATH "%%~a;%cd%"
) else (
setx PATH "%%~a %%~b;%cd%"
)
)
答案 2 :(得分:-3)
setx path c:\whatever
对于path
,setx会将指定的路径附加到现有路径。您不指定现有路径,只指定新路径。它也不会两次添加路径。
这是我使用的东西
%windir%\system32\setx path %~sdp0 > "%temp%\filter.tmp"
如果我尝试添加两次,重定向会隐藏错误消息。如果您不知道call /?
是什么
%~sdp0
命令