program Project2;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.IOUtils,
System.SysUtils;
type
TArchiveFolder =
class
private
FID : TGUID;
FFullName :
string;
FFirstLoad: Boolean;
function GetID: TGUID;
function GetName:
string;
function GetIdFileName:
string;
function GetIsInitialized: Boolean;
procedure EnsureFirstLoad;
public
property FullName :
string read FFullName;
property ID : TGUID
read GetID;
property IsInitialized: Boolean
read GetIsInitialized;
property name :
string read GetName;
public
constructor Create(
const Name:
string );
public
procedure Init( CreateIfNotExists: Boolean = False );
procedure Reload;
end;
{ TArchiveFolder }
constructor TArchiveFolder.Create(
const Name:
string );
begin
if TFile.Exists(
name )
then
raise Exception.Create( '
Fehlermeldung' );
inherited Create;
FFullName := TPath.GetFullPath(
name );
end;
procedure TArchiveFolder.EnsureFirstLoad;
begin
if not FFirstLoad
then
begin
Reload;
FFirstLoad := True;
end;
end;
function TArchiveFolder.GetID: TGUID;
begin
EnsureFirstLoad;
Result := FID;
end;
function TArchiveFolder.GetIdFileName:
string;
begin
Result := TPath.Combine( FFullName, '
.folderid' );
end;
function TArchiveFolder.GetIsInitialized: Boolean;
begin
EnsureFirstLoad;
Result := FID <> TGUID.Empty;
end;
function TArchiveFolder.GetName:
string;
begin
Result := TPath.GetFileName( FFullName );
end;
procedure TArchiveFolder.Init( CreateIfNotExists: Boolean = False );
begin
Reload;
if IsInitialized
then
raise Exception.Create( '
Fehlermeldung' );
if not TDirectory.Exists( FFullName )
then
begin
if CreateIfNotExists
then
TDirectory.CreateDirectory( FFullName )
else
raise Exception.Create( '
Fehlermeldung' );
end;
TFile.WriteAllText( GetIdFileName( ), TGUID.NewGuid.ToString );
Reload;
end;
procedure TArchiveFolder.Reload;
var
lIdFileName:
string;
begin
lIdFileName := GetIdFileName( );
if not TFile.Exists( lIdFileName )
then
FID := TGUID.Empty
else
FID := TGUID.Create( TFile.ReadAllText( lIdFileName ) );
end;
procedure Main;
var
fld: TArchiveFolder;
begin
fld := TArchiveFolder.Create( '
.' );
try
if not fld.IsInitialized
then
begin
WriteLn( '
Init ',fld.FullName );
fld.Init( );
end;
WriteLn( fld.ID.ToString( ) );
finally
fld.Free;
end;
fld := TArchiveFolder.Create( '
.' );
try
if not fld.IsInitialized
then
begin
WriteLn( '
Init ',fld.FullName );
fld.Init( );
end;
WriteLn( fld.ID.ToString( ) );
finally
fld.Free;
end;
end;
begin
try
Main( );
except
on E:
Exception do
WriteLn( E.ClassName, '
: ', E.
Message );
end;
ReadLn;
end.