Page 1 of 1

Deleting a Directory

Posted: Tue Mar 19, 2019 3:49 pm
by Robert
When I try to delete a directory using

Code: Select all

Files.dir.Delete(loc, name)
I get the undocumemted error "loc.res = 6".
How should I delete a directory?

Re: Deleting a Directory

Posted: Tue Mar 19, 2019 4:04 pm
by Robert
I've used

Code: Select all

WinApi.RemoveDirectoryW
Is this function, or should it be, provided by BlackBox?

Re: Deleting a Directory

Posted: Wed Mar 20, 2019 3:33 am
by luowy
the framework lack this function, you had to write by yourself, below is the part of my CpcFiles:

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;
luowy

Re: Deleting a Directory

Posted: Wed Mar 27, 2019 9:58 pm
by Josef Templ
It is interesting that Delete is not supported for directories.
Is there any complication involved? I mean what happens to open files
of the deleted directory, for example. Probably handled by the OS but are there any
special cases? I could imagine to support deletion of directories in Files.dir.Delete
but a careful analysis must be done first.

- Josef

Re: Deleting a Directory

Posted: Thu Mar 28, 2019 4:07 am
by luowy
maybe the history reason(oberon no directoy concept)....

Files.dir.Delete can be extend to accept Directory name, error code can be return to the loc.res,
and "ask" option for a popup dialog window of "retry","skip".....

it's a basic function, I agree to add such extension(Delete, Rename...) to the framework;

Re: Deleting a Directory

Posted: Mon Apr 01, 2019 4:00 am
by Zinn
Josef Templ wrote:It is interesting that Delete is not supported for directories.
Is there any complication involved?
A directory should be empty before it can be deleted. This rule make the implementation less complicated.

I miss the basic file function Copy. This should be done only for one file and not for a directory. The copied file must have the same time and date as the original file.

- Helmut

Re: Deleting a Directory

Posted: Sat Apr 06, 2019 10:24 am
by Robert
Zinn wrote:A directory should be empty before it can be deleted. This rule make the implementation less complicated.
I think this is a reasonable restriction for a new basic "DeleteDirectory" procedure.

One could then use this to implement a recursive "Empty & Delete this tree" function if required.