forked from sirodcar/commonx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBatch.pas
More file actions
132 lines (96 loc) · 3.2 KB
/
Batch.pas
File metadata and controls
132 lines (96 loc) · 3.2 KB
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
unit Batch;
// Contains Code for doing batch-file-like things in a console app.. with
// visual feedback
interface
uses
commands_file, commands_system, ConsoleGlobal, consoletools, dir, dirfile, systemx, typex, numbers, commandprocessor, stringx, sysutils, exe, filemirror;
type
TBatch = class
class function VerifyAccessToFolder(sRemote: string): boolean;
class function MapDrive(sDriveLetterPlusColon, sRemote: string): boolean;
class procedure ReplicateFolder(sRemote, sLocal: string;
bDeleteExtras: boolean; bSilent: boolean = false);
class function ExeBegin(sExe: string; sParams: string; sWkDir: string):Tcmd_RunExe;
class procedure ExeFinish(c: Tcmd_RunExe);
class procedure Movefile(sSource, sDest: string);
end;
implementation
{ TBatch }
class function TBatch.ExeBegin(sExe, sParams, sWkDir: string): Tcmd_RunExe;
begin
con.WriteEx('Launching: `cE`'+sExe+NLR);
con.WriteEx('Params : `cE`'+stringreplace(sParams,'`','``',[rfReplaceAll])+NLR);
result := Tcmd_RunExe.create;
result.Prog := sExe;
result.Params := sParams;
result.WorkingDir :=sWkDir;
// result.batchwrap := true;
result.CaptureConsoleoutput := true;
result.Start;
end;
class procedure TBatch.ExeFinish(c: Tcmd_RunExe);
begin
c.waitfor;
con.WriteLn(c.ConsoleOutput);
c.Free;
end;
class function TBatch.MapDrive(sDriveLetterPlusColon, sRemote: string): boolean;
begin
exe.RunProgramAndWait(GetSystemDir+'net.exe', '/DELETE '+sDriveLetterPlusColon,'', true);
exe.RunProgramAndWait(GetSystemDir+'net.exe', 'use '+sDriveLetterPlusColon+' '+quote(sRemote),'', true);
result := VerifyAccessToFolder(sDriveLetterPlusColon+'\');
end;
class procedure TBatch.Movefile(sSource, sDest: string);
begin
con.writeEx('`cE`Moving: `cF`'+sSource+'`cE`->`cF`'+sDest+NLR);
var mf := TFileCopyCommand.create;
try
mf.Source := sSource;
mf.Destination := sDest;
mf.Start;
con.WatchCommand(mf);
mf.WaitFor;
finally
mf.free;
end;
end;
class procedure TBatch.ReplicateFolder(sRemote, sLocal: string;
bDeleteExtras: boolean; bSilent: boolean = false);
var
fil: TFileInformation;
begin
if not bSilent then begin
con.SetTextAttribute(CC_YELLOW);
con.WriteEx('`cE`Replicating: `cF`');
con.WriteEx(sRemote);
con.WriteEx('`cE`->`cF');
con.WriteEx(sLocal+CRLF);
end else
con.Twiddle;
if not bDeleteExtras then
NotImplemented;
var cop := FileMirror.BeginReplicate(sRemote, sLocal, 'c:\trash');
if not bSilent then
con.WatchCommand(cop);
cop.WaitFor;
con.WriteEx('Waiting for files to copy'+NLR);
while commands_file.fileCommands.commandcount > commands_file.fileCommands.CompleteCount do begin
con.WriteEx('Waiting on '+fileCommands.CommandCount.tostring+' commands ');
con.Twiddle;
sleep(100);
end;
while BGCmd.commandcount > BGCmd.CompleteCount do begin
con.WriteEx('Waiting on '+BGCmd.CommandCount.tostring+' commands ');
con.Twiddle;
sleep(100);
end;
cop.free;
con.WriteOk(true);
end;
class function TBatch.VerifyAccessToFolder(sRemote: string): boolean;
begin
con.WriteLn('Verifying Access to '+sRemote);
result := DirectoryExists(sRemote);
con.WriteOK(result);
end;
end.