|
| 1 | +@echo off |
| 2 | +REM set_user_path.bat |
| 3 | +REM English: This batch reads list.txt (if present) or uses the first argument |
| 4 | +REM (semicolon-separated paths) and prepends missing entries into the user's PATH (HKCU). |
| 5 | +REM Lines starting with '#' or empty lines in list.txt are ignored. |
| 6 | +REM 한국어: 이 배치파일은 동일 디렉터리의 list.txt(존재하면)를 읽거나, |
| 7 | +REM 명령행 인자로 전달된 세미콜론(;) 구분 경로들을 사용하여, |
| 8 | +REM 사용자 환경변수 PATH(HKCU)에 누락된 경로를 중복 없이 선행(prepend)으로 추가합니다. |
| 9 | +REM list.txt의 빈 줄과 '#'으로 시작하는 주석은 무시됩니다. |
| 10 | + |
| 11 | +setlocal EnableDelayedExpansion |
| 12 | + |
| 13 | +set "SCRIPT_DIR=%~dp0" |
| 14 | +set "LISTFILE=%SCRIPT_DIR%list.txt" |
| 15 | +set "ADDPATHS=" |
| 16 | +set "DRY_RUN=0" |
| 17 | +if "%~1"=="--dry-run" ( |
| 18 | + set "DRY_RUN=1" |
| 19 | + shift |
| 20 | +) |
| 21 | + |
| 22 | +if exist "%LISTFILE%" ( |
| 23 | + REM list.txt을 한 줄씩 읽어 빈 줄과 주석을 무시하고 세미콜론 구분 문자열을 구성 |
| 24 | + REM English: read list.txt line-by-line, ignore blank and comment lines, build semicolon list |
| 25 | + for /f "usebackq delims=" %%L in ("%LISTFILE%") do ( |
| 26 | + set "line=%%L" |
| 27 | + REM Trim leading spaces (tokens=* removes leading spaces) |
| 28 | + REM English: remove leading whitespace from the line |
| 29 | + for /f "tokens=*" %%A in ("!line!") do set "line=%%A" |
| 30 | + if not "!line!"=="" ( |
| 31 | + REM '#'로 시작하는 주석 라인 무시 |
| 32 | + REM English: ignore lines beginning with '#' |
| 33 | + echo !line! | findstr /b "#" >nul |
| 34 | + if errorlevel 1 ( |
| 35 | + REM 파일 시스템에 경로가 존재하지 않으면 경고를 stderr로 출력(그러나 목록에는 포함) |
| 36 | + REM English: warn if the listed path does not exist, but still include it |
| 37 | + if not exist "!line!\" ( |
| 38 | + echo 경고: 목록에 명시된 경로가 존재하지 않음: !line! >&2 |
| 39 | + ) |
| 40 | + if defined ADDPATHS ( |
| 41 | + set "ADDPATHS=!ADDPATHS!;!line!" |
| 42 | + ) else ( |
| 43 | + set "ADDPATHS=!line!" |
| 44 | + ) |
| 45 | + ) |
| 46 | + ) |
| 47 | + ) |
| 48 | +) else ( |
| 49 | + if "%~1"=="" ( |
| 50 | + echo 사용법: %~nx0 ^"C:\path1;C:\path2^" |
| 51 | + echo 또는 이 스크립트와 동일한 디렉터리에 한 줄에 하나씩 경로를 적은 list.txt 파일을 생성하세요. |
| 52 | + echo English: Usage: %~nx0 "C:\path1;C:\path2" |
| 53 | + echo Or create a list.txt file with one path per line in the same directory as this script. |
| 54 | + exit /b 1 |
| 55 | + ) |
| 56 | + set "ADDPATHS=%~1" |
| 57 | +) |
| 58 | + |
| 59 | +REM Prepare temporary PowerShell script to update user PATH safely |
| 60 | +REM Use a unique filename to avoid collisions when run in parallel or by multiple users. |
| 61 | +REM Construct a sanitized timestamp stamp and include a random component. |
| 62 | +set "STAMP=%DATE%_%TIME%" |
| 63 | +rem remove characters that are invalid/unfriendly in filenames |
| 64 | +set "STAMP=%STAMP::=%" |
| 65 | +set "STAMP=%STAMP:.=%" |
| 66 | +set "STAMP=%STAMP:/=%" |
| 67 | +set "STAMP=%STAMP: =_%" |
| 68 | +set "STAMP=%STAMP:,=%" |
| 69 | +set "TMP_PS=%TEMP%\_set_user_path_tmp_%STAMP%_%RANDOM%.ps1" |
| 70 | + |
| 71 | +rem Escape single quotes in ADDPATHS for embedding into PS script |
| 72 | +set "PS_ADD=%ADDPATHS:'='''%" |
| 73 | + |
| 74 | +if "%DRY_RUN%"=="1" ( |
| 75 | + echo DRY RUN: temporary PowerShell filename would be: |
| 76 | + echo %TMP_PS% |
| 77 | + endlocal |
| 78 | + exit /b 0 |
| 79 | +) |
| 80 | + |
| 81 | +>"%TMP_PS%" echo $add = '%PS_ADD%' -split ';' ^| Where-Object {$_ -ne ''} |
| 82 | +>>"%TMP_PS%" echo $current_raw = [Environment]::GetEnvironmentVariable('Path','User') |
| 83 | +>>"%TMP_PS%" echo if ([string]::IsNullOrEmpty($current_raw)) { $current = @() } else { $current = $current_raw -split ';' ^| Where-Object {$_ -ne ''} } |
| 84 | +>>"%TMP_PS%" echo foreach ($p in $add) { if (-not ($current -contains $p)) { $current = ,$p + $current } } |
| 85 | +>>"%TMP_PS%" echo $newPath = ($current -join ';') |
| 86 | +>>"%TMP_PS%" echo [Environment]::SetEnvironmentVariable('Path',$newPath,'User') |
| 87 | +>>"%TMP_PS%" echo Write-Host '사용자 PATH가 업데이트되었습니다 (상위 항목):' |
| 88 | +>>"%TMP_PS%" echo $current[0..([math]::Min(9,$current.Count-1))] ^| ForEach-Object { Write-Host " $_" } |
| 89 | +>>"%TMP_PS%" echo "" |
| 90 | +>>"%TMP_PS%" echo # Broadcast WM_SETTINGCHANGE so Explorer and other apps pick up the change |
| 91 | +>>"%TMP_PS%" echo $signature = @" |
| 92 | +>>"%TMP_PS%" echo using System; |
| 93 | +>>"%TMP_PS%" echo using System.Runtime.InteropServices; |
| 94 | +>>"%TMP_PS%" echo public class NativeMethods { |
| 95 | +>>"%TMP_PS%" echo [DllImport(^"user32.dll^", CharSet = CharSet.Auto)] |
| 96 | +>>"%TMP_PS%" echo public static extern IntPtr SendMessageTimeout(IntPtr hWnd, uint Msg, UIntPtr wParam, string lParam, uint fuFlags, uint uTimeout, out UIntPtr lpdwResult); |
| 97 | +>>"%TMP_PS%" echo } |
| 98 | +>>"%TMP_PS%" echo "@ |
| 99 | +>>"%TMP_PS%" echo Add-Type -TypeDefinition $signature -ErrorAction Stop |
| 100 | +>>"%TMP_PS%" echo $HWND_BROADCAST = [IntPtr]0xffff |
| 101 | +>>"%TMP_PS%" echo $WM_SETTINGCHANGE = 0x001A |
| 102 | +>>"%TMP_PS%" echo [UIntPtr]$result = [UIntPtr]::Zero |
| 103 | +>>"%TMP_PS%" echo [NativeMethods]::SendMessageTimeout($HWND_BROADCAST, $WM_SETTINGCHANGE, [UIntPtr]::Zero, 'Environment', 0, 5000, [ref]$result) ^| Out-Null |
| 104 | +>>"%TMP_PS%" echo Write-Host '환경변수 변경을 브로드캐스트했습니다 (WM_SETTINGCHANGE).' -ForegroundColor Cyan |
| 105 | + |
| 106 | +REM Run the temporary PowerShell script (bypass execution policy for this run) |
| 107 | +powershell -NoProfile -ExecutionPolicy Bypass -File "%TMP_PS%" |
| 108 | + |
| 109 | +REM Remove temporary script |
| 110 | +del /f /q "%TMP_PS%" >nul 2>&1 |
| 111 | + |
| 112 | +endlocal |
| 113 | +echo Done. |
| 114 | +exit /b 0 |
0 commit comments