Page 1 of 2

Directory or Folder exists?

Posted: Tue Dec 13, 2016 8:09 am
by DGDanforth
Given a path how can you determine whether a directory (folder) exists for that path?
-Doug

Re: Directory or Folder exists?

Posted: Tue Dec 13, 2016 11:07 am
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.

Re: Directory or Folder exists?

Posted: Tue Dec 13, 2016 12:24 pm
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?

Re: Directory or Folder exists?

Posted: Tue Dec 13, 2016 1:10 pm
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.

Re: Directory or Folder exists?

Posted: Tue Dec 13, 2016 1:34 pm
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!

Re: Directory or Folder exists?

Posted: Tue Dec 13, 2016 1:52 pm
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

Re: Directory or Folder exists?

Posted: Fri Dec 16, 2016 6:18 am
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

Re: Directory or Folder exists?

Posted: Fri Dec 16, 2016 9:46 am
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.

Re: Directory or Folder exists?

Posted: Sat Dec 17, 2016 12:19 am
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

Re: Directory or Folder exists?

Posted: Sat Dec 17, 2016 9:00 am
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?