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
|
{ Directory cleaner - command-line interface
Copyright (C) 2007 Michael Van Canneyt michael@freepascal.org
This source is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your option)
any later version.
This code 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 General Public License for more
details.
A copy of the GNU General Public License is available on the World Wide Web
at <http://www.gnu.org/copyleft/gpl.html>. You can also obtain it by writing
to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
Boston, MA 02110-1335, USA.
}
program cleandir;
{$mode objfpc}{$H+}
uses
Classes,
SysUtils,
CustApp,
dircleaner;
ResourceString
SErrNoConfig = 'No configuration file found.';
SErrInvalidConfig = 'Configuration file "%s" does not exist.';
SErrNoDirectories = 'No directories to clean';
SUsage0 = 'Usage: %s [options] directory1 ... directoryN';
SUsage1 = 'Where options is one or more of:';
SUsageh = '-h --help This help message';
SUsagec = '-c --config=file Filename for configuration data';
SUsagev = '-v --verbose Log more messages';
SUsagel = '-l --logonly Only log actions, do not execute commands';
SUsageq = '-q --quiet Do not log actions (disables -l)';
SUsages = '-s --stoponerror Stop at the first error.';
SUsager = '-r --recurse Recurse directories specified on command-line';
SUsaged = '-d --standarddirs Clean standard dirs from configuration file';
Type
{ TCleanDirs }
TRunMode = (rmHelp,rmExecute);
TCleanDir = Class (TCustomApplication)
private
FCleaner: TCleanDirs;
Function ProcessCommandLine : TRunMode;
Public
Constructor Create(AOwner : TComponent); override;
Procedure DoRun; override;
Procedure Usage;
Procedure OnLog(Msg : String);
Property Cleaner : TCleanDirs Read FCleaner;
end;
{ TCleanDir }
Var
Application : TCleanDir;
function TCleanDir.ProcessCommandLine: TRunMode;
Const
OptCount = 8;
Opts : Array[1..OptCount] of string =
('help','config','verbose','logonly','quiet',
'stoponerror','recurse','standarddirs');
OptChars = 'hcvlqsrd';
Var
FN : String;
OptL,Dirs : TStrings;
I : integer;
begin
Result:=rmHelp;
If HasOption('h','help') then
Exit;
Dirs:=TStringList.Create;
try
OptL:=TStringList.Create;
try
For I:=1 to OptCount do
OptL.Add(Opts[i]);
FN:=CheckOptions(OptChars,OptL,Nil,Dirs);
finally
OptL.Free;
end;
If (FN<>'') then
begin
Writeln(StdErr,FN);
Exit;
end;
FN:='';
If HasOption('c','config') then
begin
FN:=Self.GetOptionValue('c','config');
If Not FileExists(FN) then
begin
Writeln(StdErr,format(SErrInvalidConfig,[FN]));
Exit;
end;
end
else
begin
FN:=GetAppConfigFile(false,false);
If Not FileExists(FN) then
begin
FN:=GetAppConfigFile(True,false);
If Not FileExists(FN) then
FN:='';
end;
If (FN='') then
begin
Writeln(StdErr,SErrNoConfig);
Exit;
end;
end;
FCleaner.LoadFromFile(FN);
If Not HasOption('q','quiet') then
begin
FCleaner.OnLog:=@Self.OnLog;
FCleaner.LogAllFiles:=HasOption('v','verbose');
FCleaner.LogOnly:=HasOption('l','logonly');
end;
If Hasoption('s','stoponerror') then
FCleaner.StopOnError:=True;
if Not HasOption('d','standarddirs') then
FCleaner.Directories.Clear;
If (Dirs.Count>0) then
begin
For I:=0 to Dirs.Count-1 do
With FCleaner.Directories.AddDirectory do
begin
Path:=Dirs[i];
Enabled:=True;
Recurse:=HasOption('r','recurse');
end;
end;
If (FCleaner.Directories.Count=0) then
Writeln(StdErr,SErrNoDirectories)
else
Result:=rmExecute;
Finally
Dirs.Free;
end;
end;
constructor TCleanDir.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FCleaner:=TCleanDirs.Create(Self);
StopOnException:=True;
end;
procedure TCleanDir.DoRun;
begin
Case ProcessCommandLine of
rmHelp : Usage;
rmExecute : FCleaner.Execute;
end;
Terminate;
end;
procedure TCleanDir.Usage;
begin
Writeln(Format(SUsage0,[ExtractFileName(paramstr(0))]));
Writeln(SUsage1);
Writeln(SUsagec);
Writeln(SUsaged);
Writeln(SUsageh);
Writeln(SUsagel);
Writeln(SUsageq);
Writeln(SUsager);
Writeln(SUsages);
Writeln(SUsagev);
end;
procedure TCleanDir.OnLog(Msg: String);
begin
Writeln(Msg);
end;
begin
OnGetApplicationName:=@CleanDirApp;
Application:=TCleandir.Create(nil);
Application.Title:='cleandisk';
Application.Run;
Application.Free;
end.
|