diff --git a/README.md b/README.md index 6b66d3d..544eee5 100644 --- a/README.md +++ b/README.md @@ -346,8 +346,10 @@ $Env:PATH = "$HOME\bin;$Env:PATH" ``` ```batch -REM cmd.exe - persistent for current user -setx PATH "%USERPROFILE%\bin;%PATH%" +REM cmd.exe - persistent for current user (use PowerShell method above instead) +REM WARNING: This expands %PATH% which includes system PATH, causing duplication +REM Note: Using reg add instead of setx to avoid 1024-character truncation limit +reg add "HKCU\Environment" /v Path /t REG_EXPAND_SZ /d "%USERPROFILE%\bin;%PATH%" /f ``` --- diff --git a/README_CN.md b/README_CN.md index 8d9dc81..9c05083 100644 --- a/README_CN.md +++ b/README_CN.md @@ -282,8 +282,10 @@ $Env:PATH = "$HOME\bin;$Env:PATH" ``` ```batch -REM cmd.exe - 永久添加(当前用户) -setx PATH "%USERPROFILE%\bin;%PATH%" +REM cmd.exe - 永久添加(当前用户)(建议使用上面的 PowerShell 方法) +REM 警告:此命令会展开 %PATH% 包含系统 PATH,导致重复 +REM 注意:使用 reg add 而非 setx 以避免 1024 字符截断限制 +reg add "HKCU\Environment" /v Path /t REG_EXPAND_SZ /d "%USERPROFILE%\bin;%PATH%" /f ``` --- diff --git a/install.bat b/install.bat index 298a940..534e972 100644 --- a/install.bat +++ b/install.bat @@ -117,11 +117,18 @@ if "!ALREADY_IN_USERPATH!"=="1" ( set "USER_PATH_NEW=!PCT!USERPROFILE!PCT!\bin" ) rem Persist update to HKCU\Environment\Path (user scope) - setx Path "!USER_PATH_NEW!" >nul - if errorlevel 1 ( - echo WARNING: Failed to append %%USERPROFILE%%\bin to your user PATH. + rem Use reg add instead of setx to avoid 1024-character limit + echo(!USER_PATH_NEW! | findstr /C:"\"" /C:"!" >nul + if not errorlevel 1 ( + echo WARNING: Your PATH contains quotes or exclamation marks that may cause issues. + echo Skipping automatic PATH update. Please add %%USERPROFILE%%\bin to your PATH manually. ) else ( - echo Added %%USERPROFILE%%\bin to your user PATH. + reg add "HKCU\Environment" /v Path /t REG_EXPAND_SZ /d "!USER_PATH_NEW!" /f >nul + if errorlevel 1 ( + echo WARNING: Failed to append %%USERPROFILE%%\bin to your user PATH. + ) else ( + echo Added %%USERPROFILE%%\bin to your user PATH. + ) ) ) ) diff --git a/test_install_path.bat b/test_install_path.bat new file mode 100644 index 0000000..cf78417 --- /dev/null +++ b/test_install_path.bat @@ -0,0 +1,67 @@ +@echo off +setlocal enabledelayedexpansion + +echo Testing PATH update with long strings... +echo. + +rem Create a very long PATH string (over 1024 characters) +set "LONG_PATH=" +for /L %%i in (1,1,30) do ( + set "LONG_PATH=!LONG_PATH!C:\VeryLongDirectoryName%%i\SubDirectory\AnotherSubDirectory;" +) + +echo Generated PATH length: +echo !LONG_PATH! > temp_path.txt +for %%A in (temp_path.txt) do set "PATH_LENGTH=%%~zA" +del temp_path.txt +echo !PATH_LENGTH! bytes + +rem Test 1: Verify reg add can handle long strings +echo. +echo Test 1: Testing reg add with long PATH... +set "TEST_PATH=!LONG_PATH!%%USERPROFILE%%\bin" +reg add "HKCU\Environment" /v TestPath /t REG_EXPAND_SZ /d "!TEST_PATH!" /f >nul 2>nul +if errorlevel 1 ( + echo FAIL: reg add failed with long PATH + goto :cleanup +) else ( + echo PASS: reg add succeeded with long PATH +) + +rem Test 2: Verify the value was stored correctly +echo. +echo Test 2: Verifying stored value length... +for /f "tokens=2*" %%A in ('reg query "HKCU\Environment" /v TestPath 2^>nul ^| findstr /I "TestPath"') do set "STORED_PATH=%%B" +echo !STORED_PATH! > temp_stored.txt +for %%A in (temp_stored.txt) do set "STORED_LENGTH=%%~zA" +del temp_stored.txt +echo Stored PATH length: !STORED_LENGTH! bytes + +if !STORED_LENGTH! LSS 1024 ( + echo FAIL: Stored PATH was truncated + goto :cleanup +) else ( + echo PASS: Stored PATH was not truncated +) + +rem Test 3: Verify %%USERPROFILE%%\bin is present +echo. +echo Test 3: Verifying %%USERPROFILE%%\bin is in stored PATH... +echo !STORED_PATH! | findstr /I "USERPROFILE" >nul +if errorlevel 1 ( + echo FAIL: %%USERPROFILE%%\bin not found in stored PATH + goto :cleanup +) else ( + echo PASS: %%USERPROFILE%%\bin found in stored PATH +) + +echo. +echo ======================================== +echo All tests PASSED +echo ======================================== + +:cleanup +echo. +echo Cleaning up test registry key... +reg delete "HKCU\Environment" /v TestPath /f >nul 2>nul +endlocal