Directory or Folder exists?

User avatar
DGDanforth
Posts: 1061
Joined: Tue Sep 17, 2013 1:16 am
Location: Palo Alto, California, USA
Contact:

Directory or Folder exists?

Post by DGDanforth »

Given a path how can you determine whether a directory (folder) exists for that path?
-Doug
cfbsoftware
Posts: 204
Joined: Wed Sep 18, 2013 10:06 pm
Contact:

Re: Directory or Folder exists?

Post by cfbsoftware »

Code: Select all

VAR
  loc: Files.Locator; 
...
...
  loc := Files.dir.This (path); (* never returns NIL. loc.res # 0 if path is invalid. *)
See it in use in Basics: BasicFiles by Rainer Neubauer.
User avatar
Robert
Posts: 1024
Joined: Sat Sep 28, 2013 11:04 am
Location: Edinburgh, Scotland

Re: Directory or Folder exists?

Post by Robert »

cfbsoftware wrote:

Code: Select all

VAR
  loc: Files.Locator; 
...
...
  loc := Files.dir.This (path); (* never returns NIL. loc.res # 0 if path is invalid. *)
See it in use in Basics: BasicFiles by Rainer Neubauer.
But this does not work (for me, BlackBox 1.6). Have you tested it?

I think it is an open question whether it should work. So, is the fact it does not work a bug?
User avatar
DGDanforth
Posts: 1061
Joined: Tue Sep 17, 2013 1:16 am
Location: Palo Alto, California, USA
Contact:

Re: Directory or Folder exists?

Post by DGDanforth »

cfbsoftware wrote:

Code: Select all

VAR
  loc: Files.Locator; 
...
...
  loc := Files.dir.This (path); (* never returns NIL. loc.res # 0 if path is invalid. *)
See it in use in Basics: BasicFiles by Rainer Neubauer.
loc.res # 0 if path is invalid.
That is not my experience. I can write anything into the path and loc.res=0, always.
cfbsoftware
Posts: 204
Joined: Wed Sep 18, 2013 10:06 pm
Contact:

Re: Directory or Folder exists?

Post by cfbsoftware »

Hmmm... Looking at the source code (Files.This and Files.NewLocator) It appears that it doesn't care if the folder exists or not - it only returns non-zero if the first character of the filename is "\" or "/" and the second is character is not "\" or "/":

\\Temp is OK
\Temp is not
\/Temp is OK!
User avatar
Josef Templ
Posts: 2047
Joined: Tue Sep 17, 2013 6:50 am

Re: Directory or Folder exists?

Post by Josef Templ »

Directory.This() has no place to put a result code in except in the Locator object returned.
That's probably why it always returns # NIL.
Locator.This() uses its own result code field (res) for that purpose
and returns NIL in case of an error.
I don't know why a Directory does not have a res field but it doesn't
and it would be horribly incompatibe to change that.

- Josef
User avatar
DGDanforth
Posts: 1061
Joined: Tue Sep 17, 2013 1:16 am
Location: Palo Alto, California, USA
Contact:

Re: Directory or Folder exists?

Post by DGDanforth »

Here is what I found for testing the existence of a directory/folder

Code: Select all

	PROCEDURE FolderExists* (path: CHARS): BOOLEAN;
	VAR lpFileName: WinApi.PtrWSTR; attr: SET;
	BEGIN
		lpFileName := SYSTEM.VAL(WinApi.PtrWSTR, SYSTEM.ADR(path[0]));
		attr :=BITS(WinApi.GetFileAttributesW(lpFileName));
		RETURN attr = WinApi.FILE_ATTRIBUTE_DIRECTORY
	END FolderExists;
Simple, but finding it wasn't.
-Doug
User avatar
Robert
Posts: 1024
Joined: Sat Sep 28, 2013 11:04 am
Location: Edinburgh, Scotland

Re: Directory or Folder exists?

Post by Robert »

1 - I was about to open an issue for the Locator bugs, but Josef beat me to it - probably a good thing!

2 - Doug, did you try the suggestion I posted in simple form, on the other thread, on 14-Dec (http://forum.blackboxframework.org/view ... 9&start=18)?

3 - I think there are several missing routines in Files, such as "Does path exist?" & "CopyFile". Maybe we could start a Wiki page where we post suggestions, and when we have a good collection (good both in terms of routine signatures and implementation) then we can sensibly discuss if this collection should be added to BlackBox, or maybe just posted as a Module on Component Pascal Collections - or just left on the Wiki so people can cut and paste them as required.
User avatar
DGDanforth
Posts: 1061
Joined: Tue Sep 17, 2013 1:16 am
Location: Palo Alto, California, USA
Contact:

Re: Directory or Folder exists?

Post by DGDanforth »

Robert wrote:
2 - Doug, did you try the suggestion I posted in simple form, on the other thread, on 14-Dec (http://forum.blackboxframework.org/view ... 9&start=18)?
No I didn't because of the (seeming) need to access a dll. My approach just uses a WinApi call (WinApi.GetFileAttributesW).
-Doug
User avatar
Robert
Posts: 1024
Joined: Sat Sep 28, 2013 11:04 am
Location: Edinburgh, Scotland

Re: Directory or Folder exists?

Post by Robert »

DGDanforth wrote:No I didn't because of the (seeming) need to access a dll. My approach just uses a WinApi call (WinApi.GetFileAttributesW)
Yes, my suggestion requires accessing a Windows dll.
But WinApi is a Windows dll, so what is the difference?
Post Reply