-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileUtils.pas
300 lines (273 loc) · 8.01 KB
/
FileUtils.pas
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
///////////////////////////////////////////////////////////////////////////////////////////////
//
// FileUtils.pas
// --------------------------
// Changed: 2002-12-31
// Maintain: Michael Vinther: meesoft.cjb.net || [email protected]
//
// Last change:
// GetDirList includes path from Search
//
unit FileUtils;
interface
uses Windows, SysUtils, Forms, ShellAPI, Classes, MemUtils, Controls, Messages;
var ProgramPath : string; // Path to program executable including '\'
// Append text line to file
procedure WriteLog(const FileName, LogLine: string);
// Execute file. Failure if Result<=32
function ExecuteFile(const FileName: string; const Params: string=''; const DefaultDir: string=''; ShowCmd: Integer=SW_SHOW): THandle;
// Return file name without extension
function RemoveFileExt(FileName: string): string;
// Return path name with a \ as the last character
function ForceBackslash(const PathName: string): string;
function RemoveBackslash(const PathName: string): string;
// Case sensitive compare with mask
const
SingleWildcard = '?';
MultiWildcard = '*';
function MaskCompare(Str,Mask: string): Boolean;
// Get size of file
function GetFileSize(FileName: string): Int64;
procedure GetDirList(const Search: string; List: TStrings);
// Delete file(s).
// Default is flags will recycle: FOF_ALLOWUNDO|FOF_NOCONFIRMATION|FOF_SILENT|FOF_NOERRORUI
// Multiple files can be seperated by #0, wildcards are allowed, full path must be specified
function DeleteFileEx(FileName: string; Flags: FILEOP_FLAGS=0): Boolean;
// Move file(s).
// Default is flags will recycle: FOF_NOCONFIRMMKDIR or FOF_NOCONFIRMATION or FOF_SILENT or FOF_NOERRORUI
// Multiple files can be seperated by #0, wildcards are allowed, full path must be specified
function MoveFile(Source,Dest: string; Flags: FILEOP_FLAGS=0): Boolean;
function MakeValidFileName(const Str: string): string;
// Show standard Windows dialogs
procedure ShowFileProperties(const FileName: string);
procedure ShowSearchDialog(const Directory: string);
function GetParameterFileName: string;
// Get file dropped by WM_DROPFILES
function GetDroppedFile(const Msg: TMessage; Index: Integer): string;
const
TabChar = #9;
implementation
function MakeValidFileName(const Str: string): string;
const
FileNameChars = ['A'..'Z','0'..'9','.','_','~','-','@'];
ReplaceChar = '_';
var
I : Integer;
begin
Result:=Str;
for I:=1 to Length(Result) do
if not (UpCase(Result[I]) in FileNameChars) then Result[I]:=ReplaceChar;
if Result='' then Result:='-';
if Result[1]='.' then Result:=ReplaceChar+Result;
if Length(Result)>255 then SetLength(Result,255);
end;
procedure GetDirList(const Search: string; List: TStrings);
var
SRec : TSearchRec;
E : Integer;
Path : string;
begin
Path:=ExtractFilePath(Search);
List.BeginUpdate;
E:=FindFirst(Search,faAnyFile,SRec);
try
while E=0 do
begin
if SRec.Attr and faDirectory<>0 then
begin
end
else if SRec.Attr and faVolumeID=0 then
begin
List.Add(Path+SRec.Name);
end;
E:=FindNext(SRec);
end;
finally
FindClose(SRec);
List.EndUpdate;
end;
end;
procedure WriteLog(const FileName, LogLine: string);
var Log : TextFile;
begin
try
Assign(Log,FileName);
{$I-} Append(Log); {$I+}
if IOResult<>0 then Rewrite(Log);
try
WriteLn(Log,LogLine);
finally
CloseFile(Log);
end;
except
end;
end;
// Execute file. ShowCmd is often SW_SHOW
function ExecuteFile(const FileName,Params,DefaultDir: string; ShowCmd: Integer): THandle;
begin
Result:=ShellExecute(Application.Handle,nil,
PChar(FileName),
PChar(Params),
PChar(DefaultDir),ShowCmd);
end;
function RemoveFileExt(FileName: string): string;
var P : Integer;
begin
for P:=Length(FileName) downto 1 do
if FileName[P]='\' then Break
else
if FileName[P]='.' then
begin
Result:=Copy(FileName,1,P-1);
Exit;
end;
Result:=FileName;
end;
function MaskCompare(Str,Mask: string): Boolean;
var
P : Integer;
begin
Result:=True;
for P:=1 to Length(Mask) do case Mask[P] of
SingleWildcard : if P>Length(Str) then
begin
Result:=False;
Exit;
end;
MultiWildcard : begin
if P=Length(Mask) then Exit;
Delete(Mask,1,P);
Delete(Str,1,P-1);
repeat
if MaskCompare(Str,Mask) then Exit; // Suitable substring found, return true
Delete(Str,1,1);
until Length(Str)=0;
Result:=False;
Exit;
end;
else if (P>Length(Str)) or (Str[P]<>Mask[P]) then
begin
Result:=False;
Exit;
end;
end;
if Length(Mask)<>Length(Str) then Result:=False;
end;
function ForceBackslash(const PathName: string): string;
begin
Result:=PathName;
if (PathName<>'') and (Result[Length(Result)]<>'\') then Result:=Result+'\';
end;
function RemoveBackslash(const PathName: string): string;
begin
Result:=PathName;
if (PathName<>'') and (Result[Length(Result)]='\') then SetLength(Result,Length(Result)-1);
end;
function GetFileSize(FileName: string): Int64;
var
Handle: THandle;
FindData: TWin32FindData;
begin
Result:=-1;
Handle:=FindFirstFile(PChar(FileName),FindData);
if Handle<>INVALID_HANDLE_VALUE then
begin
Windows.FindClose(Handle);
if (FindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY)=0 then
begin
Result:=FindData.nFileSizeLow or (Int64(FindData.nFileSizeHigh) shl 32);
end;
end;
end;
function DeleteFileEx(FileName: string; Flags: FILEOP_FLAGS): Boolean;
var
fos : TSHFileOpStruct;
begin
if FileName='' then
begin
Result:=False;
Exit;
end;
if FileName[Length(FileName)]<>#0 then FileName:=FileName+#0;
ZeroMem(fos,SizeOf(fos));
with fos do
begin
Wnd:=Application.Handle;
wFunc:=FO_DELETE;
pFrom:=PChar(FileName);
if Flags=0 then fFlags:=FOF_ALLOWUNDO or FOF_NOCONFIRMATION or FOF_SILENT or FOF_NOERRORUI
else fFlags:=Flags;
end;
Result:=SHFileOperation(fos)=0;
end;
function MoveFile(Source,Dest: string; Flags: FILEOP_FLAGS=0): Boolean;
var
fos : TSHFileOpStruct;
begin
if (Source='') or (Dest='') then
begin
Result:=False;
Exit;
end;
if Source[Length(Source)]<>#0 then Source:=Source+#0;
if Dest[Length(Dest)]<>#0 then Dest:=Dest+#0;
ZeroMem(fos,SizeOf(fos));
with fos do
begin
Wnd:=Application.Handle;
wFunc:=FO_MOVE;
pFrom:=PChar(Source);
pTo:=PChar(Dest);
if Flags=0 then fFlags:=FOF_NOCONFIRMMKDIR or FOF_NOCONFIRMATION or FOF_SILENT or FOF_NOERRORUI
else fFlags:=Flags;
end;
Result:=SHFileOperation(fos)=0;
end;
function GetParameterFileName: string;
var
I : Integer;
begin
Result:=ParamStr(1);
for I:=2 to ParamCount do Result:=Result+' '+ParamStr(I);
end;
procedure ShowFileProperties(const FileName: string);
var
SEI : SHELLEXECUTEINFO;
begin
ZeroMem(SEI,SizeOf(SEI));
with SEI do
begin
cbSize:=SizeOf(SEI);
fMask:=SEE_MASK_NOCLOSEPROCESS or SEE_MASK_INVOKEIDLIST or SEE_MASK_FLAG_NO_UI;
Wnd:=Application.Handle;
lpVerb:='properties';
lpFile:=PChar(FileName);
end;
if not ShellExecuteEx(@SEI) then RaiseLastWin32Error;
end;
procedure ShowSearchDialog(const Directory: string);
var
SEI : SHELLEXECUTEINFO;
begin
ZeroMem(SEI,SizeOf(SEI));
with SEI do
begin
cbSize:=SizeOf(SEI);
fMask:=SEE_MASK_NOCLOSEPROCESS or SEE_MASK_INVOKEIDLIST or SEE_MASK_FLAG_NO_UI;
Wnd:=Application.Handle;
lpVerb:='find';
lpFile:=PChar(Directory);
end;
if not ShellExecuteEx(@SEI) then RaiseLastWin32Error;
end;
function GetDroppedFile(const Msg: TMessage; Index: Integer): string;
begin
SetLength(Result,DragQueryFile(Msg.wParam,Index,nil,0));
DragQueryFile(Msg.wParam,Index,@Result[1],Length(Result)+1);
end;
var P : Integer;
initialization
ProgramPath:=ParamStr(0); P:=Length(ProgramPath);
while (P>0) and (ProgramPath[P]<>'\') do Dec(P);
SetLength(ProgramPath,P);
end.