Directory or Folder exists?
- DGDanforth
- Posts: 1061
- Joined: Tue Sep 17, 2013 1:16 am
- Location: Palo Alto, California, USA
- Contact:
Directory or Folder exists?
Given a path how can you determine whether a directory (folder) exists for that path?
-Doug
-Doug
-
- Posts: 204
- Joined: Wed Sep 18, 2013 10:06 pm
- Contact:
Re: Directory or Folder exists?
Code: Select all
VAR
loc: Files.Locator;
...
...
loc := Files.dir.This (path); (* never returns NIL. loc.res # 0 if path is invalid. *)
Re: Directory or Folder exists?
But this does not work (for me, BlackBox 1.6). Have you tested it?cfbsoftware wrote:See it in use in Basics: BasicFiles by Rainer Neubauer.Code: Select all
VAR loc: Files.Locator; ... ... loc := Files.dir.This (path); (* never returns NIL. loc.res # 0 if path is invalid. *)
I think it is an open question whether it should work. So, is the fact it does not work a bug?
- DGDanforth
- Posts: 1061
- Joined: Tue Sep 17, 2013 1:16 am
- Location: Palo Alto, California, USA
- Contact:
Re: Directory or Folder exists?
cfbsoftware wrote:See it in use in Basics: BasicFiles by Rainer Neubauer.Code: Select all
VAR loc: Files.Locator; ... ... loc := Files.dir.This (path); (* never returns NIL. loc.res # 0 if path is invalid. *)
That is not my experience. I can write anything into the path and loc.res=0, always.loc.res # 0 if path is invalid.
-
- Posts: 204
- Joined: Wed Sep 18, 2013 10:06 pm
- Contact:
Re: Directory or Folder exists?
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!
\\Temp is OK
\Temp is not
\/Temp is OK!
- Josef Templ
- Posts: 2047
- Joined: Tue Sep 17, 2013 6:50 am
Re: Directory or Folder exists?
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
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
- DGDanforth
- Posts: 1061
- Joined: Tue Sep 17, 2013 1:16 am
- Location: Palo Alto, California, USA
- Contact:
Re: Directory or Folder exists?
Here is what I found for testing the existence of a directory/folder
Simple, but finding it wasn't.
-Doug
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;
-Doug
Re: Directory or Folder exists?
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.
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.
- DGDanforth
- Posts: 1061
- Joined: Tue Sep 17, 2013 1:16 am
- Location: Palo Alto, California, USA
- Contact:
Re: Directory or Folder exists?
No I didn't because of the (seeming) need to access a dll. My approach just uses a WinApi call (WinApi.GetFileAttributesW).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)?
-Doug
Re: Directory or Folder exists?
Yes, my suggestion requires accessing a Windows dll.DGDanforth wrote:No I didn't because of the (seeming) need to access a dll. My approach just uses a WinApi call (WinApi.GetFileAttributesW)
But WinApi is a Windows dll, so what is the difference?