FB II provides FN fileExists(fName$,fVol) which checks for the existence of a file named "fName$" in a folder whose WDrefNum% = fVol.
To check for the existence of a folder named "fldrName$" in a folder whose WDrefNum% = inThisWDrefNum%, I came up with:
CLEAR LOCAL
LOCAL FN folderExists%(fldrName$,inThisWDrefNum%)
DIM copyWD%, indx%, pathName$, fldrExists%, itemName$
DIM srchFldr$
copyWD% = inThisWDrefNum%
indx% = -1
pathName$ = FN convertWDRef$("",copyWD%)
LONG IF RIGHT$(fldrName$,1) <> ":"
srchFldr$ = fldrName$ + ":"
XELSE
srchFldr$ = fldrName$
END IF
DO
itemName$ = FILES$(indx,,pathName$,inThisWDrefNum%)
DEC(indx)
LONG IF RIGHT$(itemName$,1) = ":"
LONG IF itemName$ = srchFldr$
fldrExists% = _true
itemName$ = ""
XELSE
fldrExists% = _false
END IF
XELSE
fldrExists% = _false
END IF
UNTIL itemName$ = ""
END FN = fldrExists%
Is there a faster or more sensible way of doing this not using FILES$?
How about:
CLEAR LOCAL
LOCAL FN IsFileOrFolderThere(theFileOrFolderName$, vRefNum%)
DIM pBlock.128
pBlock.ioCompletion& = _nil
pBlock.ioNamePtr& = @theFileOrFolderName$
pBlock.ioVRefNum% = vRefNum%
pBlock.ioDirID& = 0
pBlock.ioFDirIndex% = 0
osErr% = FN GETCATINFO(@pBlock)
END FN = osErr%
theFileOrFolderName$ = "SubFolder:"
osErr% = FN IsFileOrFolderThere(theFileOrFolderName$, <>)
PRINT "IsFileOrFolderThere:";osErr%
OR
theFileOrFolderName$ = "HD:Folder:SubFolder:"
osErr% = FN IsFileOrFolderThere(theFileOrFolderName$, 0)
PRINT "IsFileOrFolderThere:";osErr%
I would use FN GETCATINFO to do it. That will tell you (a) whether the named item exists and (b) (if it exists) whether it's a file or a folder. I assume that you want to return _false in cases where the named item exists but it's a file.
LOCAL FN FolderExists(fldrName$,inThisWDrefNum%)
DIM pb.108, OSErr, itemExists, itsAFolder
pb.ioNamePtr& = @fldrName$
pb.ioVRefNum% = inThisWDrefNum%
pb.ioDirID& = 0
pb.ioDirIndex% = 0
OSErr = FN GETCATINFO(@pb)
itemExists = (OSErr = _noErr)
itsAFolder = ((PEEK(@pb.ioFlAttrib) AND BIT(4)) <> 0)
END FN = itemExists AND itsAFolder