Code: Select all
Files.dir.Delete(loc, name)
How should I delete a directory?
Code: Select all
Files.dir.Delete(loc, name)
Code: Select all
WinApi.RemoveDirectoryW
Code: Select all
(*
Delete
loc.res = 0 no error
loc.res = 1 invalid parameter (name or locator)
loc.res = 2 location or file not found
loc.res = 4 write-protection
loc.res = 5 io error
*)
PROCEDURE DeleteFile* (loc: Files.Locator; IN name: ARRAY OF CHAR; VAR res: INTEGER);
BEGIN
Files.dir.Delete(loc, name$);
res := loc.res;
END DeleteFile;
PROCEDURE Delete* (IN path, name: ARRAY OF CHAR; VAR res: INTEGER);
VAR loc: Files.Locator;
BEGIN
loc := Files.dir.This(path);
Files.dir.Delete(loc, name$);
res := loc.res;
END Delete;
PROCEDURE DeleteDialog (IN msg: ARRAY OF CHAR; VAR res: INTEGER);
VAR r: INTEGER;
BEGIN
r := WinApi.MessageBoxW(Kernel.mainWnd, msg, 'Delete', {2, 8});(* yes ,no(default)*)
IF r = 6 THEN res := 1;(* yes*)
ELSE res := 0;(* no *)
END;
END DeleteDialog;
PROCEDURE DeleteFileAsk* (loc: Files.Locator; IN name: ARRAY OF CHAR; ask: BOOLEAN);
VAR msg: ARRAY 128 OF CHAR; res: INTEGER;
BEGIN
IF ask THEN
msg := "Delete File: " + name + 0DX + 0DX + 'are you sure?';
DeleteDialog(msg, res);
IF res # 1 THEN res := 0; RETURN END; (* #yes *)
END;
DeleteFile(loc, name, res);
END DeleteFileAsk;
(* re=0: ok res #0 fialed *)
PROCEDURE RemoveDir* (IN dir: ARRAY OF CHAR; VAR res: INTEGER);
BEGIN
res := WinApi.RemoveDirectoryW(dir);
IF res # 0 THEN res := 0 ELSE res :=1 END;
END RemoveDir;
(* recursively delete dir and it's files !!! *)
PROCEDURE DeleteDir* (IN dir: ARRAY OF CHAR; VAR res: INTEGER);
VAR
loc: Files.Locator;
flist: FileList; loclist: LocList;
subdir: Files.Name;
BEGIN
loc := ThisLoc(dir);
flist := Files.dir.FileList(loc);
WHILE (flist # NIL) DO
DeleteFile(loc, flist.name, res);
flist := flist.next;
END;
loclist := Files.dir.LocList(loc);
WHILE (loclist # NIL)DO
IF dir # '' THEN subdir := dir + '\' + loclist.name; ELSE subdir := loclist.name END;
DeleteDir(subdir, res);
loclist := loclist.next;
END;
res := WinApi.RemoveDirectoryW(dir);
END DeleteDir;
PROCEDURE DeleteDirAsk* (IN dir: ARRAY OF CHAR);
VAR msg: ARRAY 128 OF CHAR; res: INTEGER;
BEGIN
msg := "Delete Directory: " + dir + 0DX + 0DX + 'are you sure?';
DeleteDialog(msg, res);
IF res # 1 THEN RETURN END;
DeleteDir(dir, res);
END DeleteDirAsk;
A directory should be empty before it can be deleted. This rule make the implementation less complicated.Josef Templ wrote:It is interesting that Delete is not supported for directories.
Is there any complication involved?
I think this is a reasonable restriction for a new basic "DeleteDirectory" procedure.Zinn wrote:A directory should be empty before it can be deleted. This rule make the implementation less complicated.