-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWhich.cmd
125 lines (96 loc) · 2.64 KB
/
Which.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
@echo off
rem set DEBUG=1
set self=%~n0
rem set program=%~f1
set program=%1
set envVarName=%2
if '%program%' == '' (goto Usage)
rem in case standalone mode iterate all variables
if '%2' == '' (
set iterateAll="True"
)
call :Debug Search for file in the current folder
call :CheckExistInFolder %program% "%CD%"
if %ERRORLEVEL% EQU 1 (
if '%iterateAll%' == '' (goto :eof)
)
call :Debug Search in EvalRun script dir
call :CheckExistInFolder %program% "%~dp0"
if %ERRORLEVEL% EQU 1 (
if '%iterateAll%' == '' (goto :eof)
)
call :Debug Search in the folders from the PATH env varibale
setlocal EnableDelayedExpansion
set LF=^
rem ** Two empty lines required
FOR /F "delims=" %%i in ("%path:;=!LF!%") do (
call :CheckExistInFolder %program% "%%i"
if %ERRORLEVEL% EQU 1 (
if '%iterateAll%' == '' (goto :eof)
)
)
endlocal EnableDelayedExpansion
goto :eof
:CheckExistInFolder
set "file=%1"
set "folder=%2"
set folder=%folder:"=%
if exist "%folder%\%file%.exe" (
call :Debug [%file%.exe]:Found in the dir "%folder%"
call :SetVar %envVarName% "%folder%\%file%.exe"
call :ShowResult "%folder%\%file%.exe" (
set ERRORLEVEL=1
if not '%iterateAll%' == '' (goto :eof)
)
if exist "%folder%\%file%" (
rem make sure it's not folder
If Not exist "%folder%\%file%\NUL" (
call :Debug [%file%]:Found in the dir "%folder%"
call :SetVar %envVarName% "%folder%\%file%"
call :ShowResult "%folder%\%file%"
set ERRORLEVEL=1
if not '%iterateAll%' == '' (goto :eof)
)
)
if exist "%folder%\%file%.cmd" (
call :Debug [%file%.cmd]:Found in the dir "%folder%"
call :SetVar %envVarName% "%folder%\%file%.cmd"
call :ShowResult "%folder%\%file%.cmd"
set ERRORLEVEL=1
if not '%iterateAll%' == '' (goto :eof)
)
if exist "%folder%\%file%.bat" (
call :Debug [%file%.bat]:Found in the dir "%folder%"
call :SetVar %envVarName% "%folder%\%file%.bat"
call :ShowResult "%folder%\%file%.bat"
set ERRORLEVEL=1
if not '%iterateAll%' == 'True' (goto :eof)
)
set ERRORLEVEL=0
goto :eof
:Usage
echo Usage: %self% App [Args]
echo Example: %self% Cmd dir c:\windows
goto :eof
:Debug
if '%DEBUG%' == '' (goto :eof)
echo ---===%*===--- >&2
goto :eof
:SetVar
rem set variable only when it exist( method has 2 parameters: 1-var name, 2- var value)
if '%2' == '' (goto :eof)
rem echo Setting var %1 %2
set value=%~2
set value=%value:"=%
set "%1=%value%"
echo "%value%"
goto :eof
rem show result only when envVarName variable is empty(standalone run mode)
:ShowResult
set "appPath=%1"
set "appPath=%appPath:"=%"
if '%1' == '' (echo '%program%':No application found >&2)
if '%envVarName%' == '' (
echo "%appPath%"
)
goto :eof