-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalias2.bat
More file actions
61 lines (54 loc) · 1.78 KB
/
alias2.bat
File metadata and controls
61 lines (54 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
@echo off
setlocal EnableDelayedExpansion
REM Define alias directory (assumed to be C:\Aliases)
set "aliasDir=C:\Aliases"
REM ----- List all aliases if no arguments provided -----
if "%~1"=="" (
echo Current aliases:
for %%F in ("%aliasDir%\*.bat") do (
set "aliasName=%%~nF"
if /I not "!aliasName!"=="alias" (
REM Get the command by finding the line starting with "call"
for /f "tokens=1,* delims= " %%a in ('findstr /b /c:"call" "%%F"') do (
set "aliasCmd=%%b"
REM Remove trailing " %%*" (used for argument passing)
set "aliasCmd=!aliasCmd: %%*=!"
)
echo alias !aliasName!='!aliasCmd!'
)
)
endlocal
exit /b
)
REM ----- Show alias definition if no '=' present in the argument -----
echo %* | find "=" >nul
if errorlevel 1 (
if exist "%aliasDir%\%~1.bat" (
echo %~1 is aliased to:
for /f "tokens=1,* delims= " %%a in ('findstr /b /c:"call" "%aliasDir%\%~1.bat"') do (
set "aliasCmd=%%b"
set "aliasCmd=!aliasCmd: %%*=!"
)
echo alias %~1='!aliasCmd!'
) else (
echo No alias named '%~1' found.
)
endlocal
exit /b
)
REM ----- Create or update an alias (syntax: alias name=command) -----
set "line=%*"
for /f "tokens=1* delims==" %%a in ("%line%") do (
set "name=%%a"
set "cmd=%%b"
)
REM Remove surrounding quotes from the command if any
set "cmd=%cmd:"=%"
REM Write the alias file (overwrite if it exists)
(
echo @echo off
REM Using call so that %%* is expanded to %* at runtime (passing any extra arguments)
echo call %cmd% %%*
) > "%aliasDir%\%name%.bat"
echo Alias "%name%" created for command: %cmd%
endlocal