-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimportfolder.pas
380 lines (329 loc) · 9.43 KB
/
importfolder.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
// **
// * Census 2, a NONMEM project manager
// *
// * Copyright 2017, Justin J Wilkins.
// *
// * This is free software; you can redistribute it and/or modify it
// * under the terms of the GNU Lesser General Public License as
// * published by the Free Software Foundation; either version 2.1 of
// * the License, or (at your option) any later version.
// *
// * This software is distributed in the hope that it will be useful,
// * but WITHOUT ANY WARRANTY; without even the implied warranty of
// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// * Lesser General Public License for more details.
// *
// * You should have received a copy of the GNU Lesser General Public
// * License along with this software; if not, write to the Free
// * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
// * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
// */
unit importfolder;
{$mode delphi}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ShellCtrls,
ComCtrls, StdCtrls, CheckLst;
type
{ TfrmImportFolder }
TfrmImportFolder = class(TForm)
btnImport: TButton;
btnCancel: TButton;
btnAll: TButton;
btnNone: TButton;
chkWarn: TCheckBox;
clbFiles: TCheckListBox;
GroupBox1: TGroupBox;
grpFiles: TGroupBox;
stvDir: TShellTreeView;
procedure btnAllClick(Sender: TObject);
procedure btnCancelClick(Sender: TObject);
procedure btnImportClick(Sender: TObject);
procedure btnNoneClick(Sender: TObject);
procedure dlgDirClose(Sender: TObject);
procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
procedure FormCreate(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure GetFiles(DirStr: string; filelist: TStrings; rec: Boolean);
procedure FindFiles(FilesList: TStrings; StartDir, FileMask: string);
Procedure ShellTreeViewSetTextPath(STV:TShelltreeview; Path:String);
procedure stvDirChange(Sender: TObject; Node: TTreeNode);
private
{ private declarations }
public
{ public declarations }
end;
var
frmImportFolder: TfrmImportFolder;
rNode: TTreeNode;
const
{$ifdef windows}
strOS = 'Windows';
strDelim = '\'
{$endif}
{$ifdef unix}
strOS = 'Linux';
strDelim = '/'
{$endif};
implementation
uses main, progress, failedimport;
{$R *.lfm}
{ TfrmImportFolder }
procedure TfrmImportFolder.FormShow(Sender: TObject);
begin
ShellTreeViewSetTextPath(stvDir, ExtractFilePath(frmMain.zConn.Database));
end;
Procedure TfrmImportFolder.ShellTreeViewSetTextPath(STV:TShelltreeview; Path:String);
Var
Str: TStringList;
ANode: TTreeNode;
i: integer;
strE: string;
Begin
If Not DirectoryExistsUTF8(Path) Then
begin
//ShowMessage('Folder not found');
Exit;
end;
Str := TStringList.Create;
Str.Delimiter := strDelim;
Str.DelimitedText := StringReplace(Path, ' ', 'ç£', [rfReplaceAll]);
For i := Str.Count - 1 Downto 0 Do
If Str[i]='' Then
Str.Delete(i);
{$ifdef unix}
Str.Insert(0, '/');
{$endif}
//showmessage(str.Text);
{$ifdef windows}
Str[0] := Str[0] + strDelim;
{$endif}
STV.BeginUpdate;
ANode := STV.TopItem;
For i := 0 To Str.Count - 1 Do
Begin
strE := StringReplace(str[i], 'ç£', ' ', [rfReplaceAll]);
//showmessage(strE);
While (ANode <> Nil) And (ANode.Text <> strE) Do
Begin
//ShowMessage(Anode.Text);
ANode := ANode.GetNextSibling;
End;
If Anode<>Nil Then
Begin
Anode.Expanded := True;
ANode.Selected := True;
Anode := ANode.GetFirstChild;
End
Else
begin
str.free;
STV.EndUpdate;
Exit;
end;
End;
str.free;
STV.EndUpdate;
End;
procedure TfrmImportFolder.stvDirChange(Sender: TObject; Node: TTreeNode);
var
n: Integer;
lstFiles: TStrings;
begin
lstFiles := TStringList.Create;
stvDir.GetFilesInDir(stvDir.GetSelectedNodePath(), '*.xml', [otNonFolders], lstFiles, fstAlphabet);
//GetFiles(stvDir.GetSelectedNodePath() + strDelim + '*.xml', lstFiles, False);
clbFiles.Clear;
for n := 0 to lstFiles.Count - 1 do
begin
clbFiles.Items.Add(ExtractFileName(lstFiles[n]));
clbFiles.Checked[n] := True;
end;
//ShowMessage(lstFiles.Text);
//FindFiles(lstFiles, dlgDir.FileName, '*.xml');
if lstFiles.Count = 0 then
begin
//MessageDlg('No suitable XML output files found in this directory.', mtInformation,
// [mbOK], 0);
Exit;
end;
end;
procedure TfrmImportFolder.btnCancelClick(Sender: TObject);
begin
Close;
end;
procedure TfrmImportFolder.btnImportClick(Sender: TObject);
var
n, intItems: Integer;
lstFailed: TStringList;
begin
try
if not Assigned(frmProgress) then
frmProgress := TfrmProgress.Create(Application);
frmProgress.Show;
frmProgress.Tag := 0;
frmProgress.Caption := 'Importing runs...';
lstFailed := TStringList.Create;
intItems := 0;
frmProgress.prgImport.Position := 0;
for n := 0 to clbFiles.Count - 1 do
if clbFiles.Checked[n] then
intItems := intItems + 1;
frmProgress.prgImport.Max := intItems;
for n := 0 to clbFiles.Count - 1 do
begin
if clbFiles.Checked[n] then
begin
frmProgress.pnlImport.Caption := clbFiles.Items[n];
frmProgress.Refresh;
Application.ProcessMessages;
//Showmessage(stvDir.GetSelectedNodePath());
try
frmMain.CaptureRunXML(stvDir.GetSelectedNodePath() + strDelim + clbFiles.Items[n], chkWarn.Checked);
except
lstFailed.Add(clbFiles.Items[n]);
end;
frmProgress.prgImport.Position := frmProgress.prgImport.Position + 1;
frmProgress.Refresh;
Application.ProcessMessages;
// interrupt
if frmProgress.Tag = 1 then
begin
MessageDlg('Import operation interrupted...', mtInformation, [mbOK], 0);
Exit;
end;
end;
end;
finally
frmProgress.Close;
if lstFailed.Count > 0 then
begin
if not Assigned(frmFailImports) then
frmFailImports := TfrmFailImports.Create(Application);
frmFailImports.Memo1.Lines.Assign(lstFailed);
frmFailImports.ShowModal;
end;
lstFailed.Free;
Close;
end;
end;
procedure TfrmImportFolder.btnNoneClick(Sender: TObject);
var
n: Integer;
begin
for n := 0 to clbFiles.Items.Count - 1 do
begin
clbFiles.Checked[n] := False;
end;
end;
procedure TfrmImportFolder.dlgDirClose(Sender: TObject);
begin
end;
procedure TfrmImportFolder.FormClose(Sender: TObject;
var CloseAction: TCloseAction);
begin
CloseAction := caFree;
frmImportFolder := nil;
end;
procedure TfrmImportFolder.FormCreate(Sender: TObject);
begin
end;
procedure TfrmImportFolder.btnAllClick(Sender: TObject);
var
n: Integer;
begin
for n := 0 to clbFiles.Items.Count - 1 do
begin
clbFiles.Checked[n] := True;
end;
end;
procedure TfrmImportFolder.GetFiles(DirStr: string; filelist: TStrings; rec: Boolean);
var
DirInfo: TSearchRec;
r: Integer;
pattern: string;
blnRecurse: Boolean;
//aNode, rNode: TTreeNode;
begin
pattern := ExtractFileName(DirStr);
if Pos('*', pattern) > 0 then
DirStr := ExtractFilePath(DirStr)
else
pattern := '*.*';
if DirStr[Length(DirStr)] <> strDelim then
DirStr := DirStr + strDelim;
if SetCurrentDir((DirStr)) then
begin
//rNode := tvDir.Items.Add(nil, DirStr);
r := FindFirst(pattern, FaAnyfile, DirInfo);
while r = 0 do
begin
if (DirInfo.Attr and faDirectory) = 0 then
begin
filelist.Add(DirStr + DirInfo.Name);
//aNode := tvDir.Items.AddChild(rNode, DirInfo.Name);
end;
r := FindNext(DirInfo);
end;
FindClose(DirInfo);
if rec then
begin
r := FindFirst('*.*', FaAnyfile, DirInfo);
while r = 0 do
begin
if (DirInfo.Attr and faDirectory) <> 0 then
if (DirInfo.Name <> '.') and (DirInfo.Name <> '..') then
begin
//aNode := tvDir.Items.AddChild(rNode, DirStr + DirInfo.Name + strDelim);
GetFiles(DirStr + DirInfo.Name + strDelim + '*.xml', filelist, True);
end;
r := FindNext(DirInfo);
end;
end;
FindClose(DirInfo);
end;
end;
procedure TfrmImportFolder.FindFiles(FilesList: TStrings; StartDir, FileMask: string);
var
SR: TSearchRec;
DirList: TStringList;
IsFound: Boolean;
i: integer;
aNode: TTreeNode;
begin
{ if StartDir[length(StartDir)] <> strDelim then
StartDir := StartDir + strDelim; }
{ Build a list of the files in directory StartDir
(not the directories!) }
{
IsFound :=
FindFirst(StartDir + FileMask, faAnyFile-faDirectory, SR) = 0;
while IsFound do
begin
FilesList.Add(StartDir + SR.Name);
aNode := tvDir.Items.AddChild(rNode, SR.Name);
IsFound := FindNext(SR) = 0;
end;
FindClose(SR);
// Build a list of subdirectories
DirList := TStringList.Create;
IsFound := FindFirst(StartDir + '*.*', faAnyFile, SR) = 0;
while IsFound do begin
if ((SR.Attr and faDirectory) <> 0) and
(SR.Name[1] <> '.') then
begin
DirList.Add(StartDir + SR.Name);
//aNode := tvDir.Items.AddChild(rNode, SR.Name);
end;
IsFound := FindNext(SR) = 0;
end;
FindClose(SR);
// Scan the list of subdirectories
for i := 0 to DirList.Count - 1 do
begin
rNode := tvDir.Items.AddChild(rNode, SR.Name);
FindFiles(FilesList, DirList[i], FileMask);
end;
DirList.Free; }
end;
end.