Deleting a Directory

Usage of the framework, compiler and tools
Post Reply
User avatar
Robert
Posts: 177
Joined: Sat Sep 28, 2013 11:04 am
Location: Edinburgh, Scotland

Deleting a Directory

Post 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?
User avatar
Robert
Posts: 177
Joined: Sat Sep 28, 2013 11:04 am
Location: Edinburgh, Scotland

Re: Deleting a Directory

Post by Robert »

I've used

Code: Select all

WinApi.RemoveDirectoryW
Is this function, or should it be, provided by BlackBox?
luowy
Posts: 87
Joined: Thu Dec 17, 2015 1:32 pm

Re: Deleting a Directory

Post 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
Josef Templ
Posts: 262
Joined: Tue Sep 17, 2013 6:50 am

Re: Deleting a Directory

Post 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
luowy
Posts: 87
Joined: Thu Dec 17, 2015 1:32 pm

Re: Deleting a Directory

Post 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;
Zinn
Posts: 123
Joined: Mon Nov 24, 2014 10:47 am
Location: Frankfurt am Main
Contact:

Re: Deleting a Directory

Post 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
User avatar
Robert
Posts: 177
Joined: Sat Sep 28, 2013 11:04 am
Location: Edinburgh, Scotland

Re: Deleting a Directory

Post 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.
Post Reply