-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetPathOf.cmd
45 lines (37 loc) · 1.37 KB
/
getPathOf.cmd
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
@echo off
@REM -- See help text at bottom for overview.
if "%~1" equ "/?" goto :help
if /i "%~1" equ "/h" goto :help
if /i "%~1" equ "-h" goto :help
@REM -- Handle the case where a target variable name is supplied. In this case, set the given
@REM -- variable to the resulting path.
if "%~2" neq "" (
for /f "delims=" %%p in ('call getPathOf "%~1"') do (
set "%~2=%%p"
goto :eof
)
goto :eof
)
@REM -- Handle the case where the result is to be printed to standard output (no target variable).
for /f "delims=" %%f in ('where "%~1"') do (
@REM -- Use the base name of the first result from the 'where' command.
setlocal EnableDelayedExpansion
set basename=%%~dpf
@REM -- Trim the trailing slash from the path.
echo !basename:~0,-1!
endlocal
exit /b 0
)
exit /b 1
:help
echo.getPathOf: Print or set the base path of the given executable.
echo.Usage : getPathOf ^<executableName^> [^<targetVariableName^>]
echo.
echo.Given an executable name, this command finds the path of the first instance of
echo.that executable using the current path list.
echo.
echo.If no target variable name is supplied, the result is printed.
echo.
echo.If a target variable name is supplied, then the named variable is set to the
echo.resulting path.
goto :eof