Last active
September 28, 2024 16:23
-
-
Save lboulard/df4a7eec45882dfeb6da3511d0c6dd26 to your computer and use it in GitHub Desktop.
PATH manipulations for Windows in CMD.EXE and PowerShell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@IF NOT DEFINED ANDROID_HOME SET "ANDROID_HOME=%LOCALAPPDATA%\Android\sdk" | |
@IF NOT DEFINED ANDROID_NDK_HOME SET "ANDROID_NDK_HOME=%ANDROID_HOME%\ndk-bundle" | |
@SET ANDROID_HOME | |
@SET ANDROID_NDK_HOME | |
@FOR %%x in (cecho.exe) DO @( | |
SET "ECHO=%%~$PATH:x" | |
IF DEFINED ECHO ( | |
SET "GREEN={0A}" | |
SET "YELLOW={06}" | |
) | |
) | |
@IF NOT DEFINED ECHO ( | |
SET ECHO=ECHO | |
SET GREEN= | |
SET YELLOW= | |
) | |
@FOR %%x IN (adb.exe) DO @( | |
IF NOT EXIST "%%~$PATH:x" ( | |
%ECHO% %GREEN%%%x -^> %ANDROID_HOME%\platform-tools\%%x | |
PATH=%ANDROID_HOME%\platform-tools;%PATH:)=^)% | |
) ELSE ( | |
%ECHO% %YELLOW%%%x already found at %%~$PATH:x | |
) | |
) | |
@FOR %%x IN (sdkmanager.bat) DO @( | |
IF NOT EXIST "%%~$PATH:x" ( | |
%ECHO% %GREEN%%%x -^> %ANDROID_HOME%\tools\bin\%%x | |
PATH=%ANDROID_HOME%\tools\bin;%PATH:)=^)% | |
) ELSE ( | |
%ECHO% %YELLOW%%%x already found at %%~$PATH:x | |
) | |
) | |
@FOR %%x IN (ndk-build.cmd) DO @( | |
IF NOT EXIST "%%~$PATH:x" ( | |
%ECHO% %GREEN%%%x -^> %ANDROID_NDK_HOME%\%%x | |
PATH=%ANDROID_NDK_HOME%;%PATH:)=^)% | |
) ELSE ( | |
%ECHO% %YELLOW%%%x already found at %%~$PATH:x | |
) | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@"%~dp0path-prepend.bat" "%ProgramFiles%\LLVM\bin" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
& { | |
$p = [IO.Path]::Combine($Env:ProgramFiles, "LLVM", "bin") | |
$p = [IO.Path]::GetFullPath($p) | |
$paths = ($env:PATH).Split(';') | ForEach { [IO.Path]::GetFullPath($_) } | |
$paths = ($paths | Where-Object { $_ -ne $p }) | |
$paths = @($p) + $paths | |
$env:PATH = ($paths -join ';') | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@"%~dp0path-prepend.bat"^ | |
"%ProgramFiles%\Elixir\bin"^ | |
"%ProgramFiles%\Erlang OTP\bin"^ | |
"%USERPROFILE%\.mix\escripts" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Configure exilir and erlang | |
& { | |
$paths = ($env:PATH).Split(';') | foreach { [IO.Path]::GetFullPath($_) } | |
@( | |
"${Env:ProgramFiles}\Elixir\bin" | |
"${Env:ProgramFiles}\Erlang OTP\bin" | |
"${Env:USERPROFILE}\.mix\escripts" | |
)[2..0].ForEach({ | |
$p = $_ | |
$p = [IO.Path]::GetFullPath($p) | |
$paths = ($paths | Where-Object { $_ -ne $p }) | |
$paths = @($p) + $paths | |
}) | |
$env:PATH = ($paths -join ';') | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@REM Configure lua (luajit) and luarocks | |
@SET LUA_PATH=%LBPROGRAMS%\Apps\luarocks\lua\?.lua | |
@SET LUA_PATH=%LUA_PATH%;%LBPROGRAMS%\Apps\luarocks\lua\?\init.lua | |
@SET LUA_PATH=%LUA_PATH%;%APPDATA%\LuaRocks\share\lua\5.1\?.lua | |
@SET LUA_PATH=%LUA_PATH%;%APPDATA%\LuaRocks\share\lua\5.1\?\init.lua | |
@SET LUA_PATH=%LUA_PATH%;%LBPROGRAMS%\Apps\luarocks\systree\share\lua\5.1\?.lua | |
@SET LUA_PATH=%LUA_PATH%;%LBPROGRAMS%\Apps\luarocks\systree\share\lua\5.1\?\init.lua | |
@SET LUA_CPATH=%APPDATA%\LuaRocks\lib\lua\5.1\?.dll | |
@SET LUA_CPATH=%LUA_CPATH%;%LBPROGRAMS%\Apps\luarocks\systree\lib\lua\5.1\?.dll | |
@::SET PATHEXT=%PATHEXT%;.LUA | |
@"%~dp0path-prepend.bat"^ | |
"%LBPROGRAMS%\Apps\luajit\bin"^ | |
"%LBPROGRAMS%\Apps\luarocks"^ | |
"%LBPROGRAMS%\Apps\luarocks\systree\bin" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Configure lua (luajit) and luarocks | |
& { | |
$paths = ($env:PATH).Split(';') | foreach { [IO.Path]::GetFullPath($_) } | |
@( | |
"${Env:LBPROGRAMS}\luajit\bin" | |
"${Env:LBPROGRAMS}\luarocks\systree\bin" | |
"${Env:LBPROGRAMS}\luarocks" | |
)[2..0].ForEach({ | |
$p = $_ | |
$p = [IO.Path]::GetFullPath($p) | |
$paths = ($paths | Where-Object { $_ -ne $p }) | |
$paths = @($p) + $paths | |
}) | |
$luarocks = [IO.Path]::Combine($Env:LBPROGRAMS, "Apps", "luarocks") | |
$appData = $Env:APPDATA | |
$env:LUA_PATH = @( | |
"${luarocks}\luarocks\lua\?.lua" | |
"${luarocks}\lua\?\init.lua" | |
"${appData}\LuaRocks\share\lua\5.1\?.lua" | |
"${appData}\LuaRocks\share\lua\5.1\?\init.lua" | |
"${luarocks}\systree\share\lua\5.1\?.lua" | |
"${luarocks}\systree\share\lua\5.1\?\init.lua" | |
) -join ';' | |
$env:LUA_CPATH = @( | |
"${appData}\LuaRocks\lib\lua\5.1\?.dll" | |
"${luarocks}\systree\lib\lua\5.1\?.dll" | |
) -join ';' | |
$env:PATH = ($paths -join ';') | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION | |
@ECHO OFF | |
REM Usage in script | |
REM @"%~dp0path-prepend.bat" ... | |
SET "_old_path=%PATH%" | |
SET "newpath=%PATH%" | |
SET "prepend=" | |
REM remove exiting entries in PATH, then prepend all arguments | |
FOR %%p IN (%*) DO ( | |
CALL :remove "%%~p" | |
IF NOT ERRORLEVEL 1 CALL :prepend "%%~p" | |
IF ERRORLEVEL 1 GOTO :exit | |
) | |
IF NOT ERRORLEVEL 1 PATH %prepend%%newpath% | |
:exit | |
IF ERRORLEVEL 1 PATH %_old_path% | |
IF ERRORLEVEL 1 ECHO.** An error occured (ERRORLEVEL=%ERRORLEVEL%) | |
@ECHO ON | |
@ENDLOCAL& PATH %PATH%& EXIT /B %ERRORLEVEL% | |
:prepend | |
SET "prepend=%prepend%%~f1;" | |
GOTO :EOF | |
:remove | |
SET "entry=%~f1" | |
IF "%entry%"=="" ( | |
ECHO.** Path "%~1": not found | |
EXIT /B 2 | |
) | |
SET "pathvar=!newpath!" | |
SET newpath= | |
:loop | |
FOR /F "delims=; tokens=1*" %%a IN ("!pathvar!") DO ( | |
SET "pathvar=%%~b" | |
CALL :cmpNames "%entry%" "%%~a" | |
IF "%%~a" NEQ "" CALL :updatePath "%%~a" | |
) | |
IF DEFINED pathvar GOTO :loop | |
GOTO :EOF | |
:updatePath | |
IF ERRORLEVEL 1 SET "newpath=%newpath%%~1;" | |
TYPE NUL>NUL | |
GOTO :EOF | |
:cmpNames | |
REM ERRORLEVEL is 0 when the same | |
TYPE NUL>NUL | |
SET "path1=%~f1" | |
SET "path2=%~f2" | |
IF "%path1%"=="" GOTO :EOF | |
IF "%path2%"=="" GOTO :EOF | |
IF "%path1:~-1%"=="\" SET "path1=%path1:~0,-1%" | |
IF "%path2:~-1%"=="\" SET "path2=%path2:~0,-1%" | |
IF /I "%path1%" NEQ "%path2%" ( | |
CALL :errorlevel 1 | |
) | |
GOTO :EOF | |
:errorlevel | |
EXIT /B %~1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment