-
Notifications
You must be signed in to change notification settings - Fork 0
/
PathFind.dpr
166 lines (138 loc) · 3.44 KB
/
PathFind.dpr
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
program PathFind;
{$APPTYPE CONSOLE}
uses
SysUtils, Windows;
const
TotalFilesFound: Integer = 0;
function UpCaseStr (s: string): string;
var
i: Integer;
begin
for i := 1 to Length (s) do
s[i] := UpCase (s[i]);
Result := s;
end;
function GetEnv (const Name: string): string;
var
BufSize: Integer;
begin
BufSize := GetEnvironmentVariable (PChar (Name), nil, 0);
if BufSize > 0 then
begin
SetLength (Result, BufSize - 1);
GetEnvironmentVariable (PChar (Name), PChar (Result), BufSize);
end
else
Result := '';
end;
function GetNthVar (const Path: string; N: Integer): string;
{ returns the Nth string in a ';'-separated string (zero based) }
var
i: Integer;
c: Char;
begin
Result := '';
i := 1;
while (i <= Length (Path)) do
begin
c := Path[i];
if (c = ';') then
Dec (N)
else
if (N = 0) then
Result := Result + c;
Inc (i);
end;
end;
procedure Check (CurPath: string; Name: string; Ext: string);
var
FullName: string;
SR: TSearchRec;
begin
FullName := CurPath + '\' + Name + Ext;
if (FileExists (FullName)) then
begin
if (SysUtils.FindFirst (FullName, faAnyFile, SR) = 0) then
begin
repeat
WriteLn (CurPath + '\' + SR.Name);
Inc (TotalFilesFound);
until SysUtils.FindNext (SR) <> 0;
SysUtils.FindClose (SR);
end;
end;
end;
var
Query: string;
HasExt: Boolean;
PathExt,
Path: string;
i,
j: Integer;
CurPath,
CurExt: string;
ch: Char;
begin
if (ParamCount () < 1) then
WriteLn ('usage: PathFind [executable]')
else
begin
Path := GetEnv ('PATH');
Query := ParamStr (1);
// option /L [search string]
// list all paths in PATH (containing search string)
if (Query = '/l') or (Query = '/L') then
begin
Query := '';
if (ParamCount () >= 2) then
begin
Query := ParamStr (2);
for i := 3 to ParamCount () do
Query := Query + ' ' + ParamStr (i);
ch := '"';
if (Query[1] = ch) and (Query[Length (Query)] = ch) then
Query := Copy (Query, 2, Length (Query) - 2);
end;
CurPath := '';
for i := 1 to Length (Path) do
begin
ch := Path[i];
if (ch = ';') then
begin
if (Query = '') or (Pos (UpCaseStr (Query), UpCaseStr (CurPath)) > 0) then
WriteLn (CurPath);
CurPath := '';
end
else
CurPath := CurPath + ch;
end;
end
else
begin
HasExt := Pos ('.', Query) > 0;
PathExt := GetEnv ('PATHEXT');
i := 0;
CurPath := GetNthVar (Path, i);
while (CurPath <> '') do
begin
if HasExt then
Check (CurPath, Query, '')
else
begin
j := 0;
CurExt := GetNthVar (PathExt, j);
while (CurExt <> '') do
begin
Check (CurPath, Query, CurExt);
Inc (j);
CurExt := GetNthVar (PathExt, j);
end;
end;
Inc (i);
CurPath := GetNthVar (Path, i);
end;
if (TotalFilesFound = 0) then
WriteLn ('File not found');
end;
end;
end.