You are not logged in. You can browse in the PowerBASIC Community, but you must click Login (top right) before you can post. If this is your first visit, check out the FAQ or Sign Up.
Function FSO_FileExists(ByVal FileSpec$)Export As Long
Local fd As WIN32_FIND_DATA
Local fAttr As Dword
Local hFind&
If Len(FileSpec$)= 0 Then Function = %false:Exit Function
hFind& = FindFirstFile(ByVal StrPtr(FileSpec$), fd)
If hFind& = %INVALID_HANDLE_VALUE Then Function = %False:Exit Function
Call FindClose(hFind&)
fAttr = fd.dwFileAttributes
Function = %True
'NotDirectory|NotTemporary
If (Bit(fAttr, 4)=1) Or (Bit(fAttr,8)=1) Then Function = %false
End Function
There is an EXISTS() function in COMMON32.BAS using DIR$(). It is not quite as comprehensive as Fred's though, but is equally useful since it uses just a single line of code.
Code:
FUNCTION Exist(file AS STRING) EXPORT AS INTEGER
FUNCTION = LEN( DIR$(file, 17) ) > 0
END FUNCTION
There is an EXISTS() function in COMMON32.BAS using DIR$(). It is not quite as comprehensive as Fred's though, but is equally useful since it uses just a single line of code.
Code:
FUNCTION Exist(file AS STRING) EXPORT AS INTEGER
FUNCTION = LEN( DIR$(file, 17) ) > 0
END FUNCTION
I have tried this in the latest version of PB/CC and get the following error when trying to compile my cpde:
Code:
Error 480 in C:\Documents and Settings\Administrator\My Documents\hunter.bas(42:030): Parameter mismatches definition
Line 42: IF Exist(Listing(idx)) THEN
Basically what I am trying to do is compare a list of files loaded from a text file to a directory of files on disk to make sure they exist.
TESTD = PathIsDirectoryA(BYCOPY TMPNAME)
? STR$(TESTD) + " ASC"
IF TESTD = 16 OR TESTD = 8208 THEN
'
ELSE
? " This IS not A Folder _ Directory "
END IF
TESTE = PathIsDirectoryEmptyA(BYCOPY TMPNAME)
If TESTE = 0 OR TESTE = 8224 Then
? " Folder _ Directory is NOT Empty "
EXIT Function
ELSE
' RmDir TmpName
'?"EMPTY"
End If
Close
PathIsDirectoryA return if directory
PathIsDirectoryEmptyA return if directory is empty
What's the significance of 16, 8208 and 8224? Are they just return values you got for specific tests?
What happens if your file/folder names are Unicode?
'
Code:
#COMPILE EXE
#DIM ALL
FUNCTION PBMAIN() AS LONG
LOCAL wsName AS WSTRING
wsName = "ABE"
? IsFileOrFolder(wsName)
END FUNCTION
FUNCTION IsFileOrFolder(s AS WSTRING) AS WSTRING
LOCAL lresult AS LONG
IF ISFILE(s) THEN
lresult = 1 'file
ELSE
IF ISFOLDER(s) THEN
IF DIR$(s & "\*.*",%HIDDEN OR %SYSTEM OR %SUBDIR) = "" THEN
lresult = 2 ' empty folder
ELSE
IF DIR$(s & "\*.*",%HIDDEN OR %SYSTEM) = "" THEN
lresult = 3 ' folder, has subfolder but no files
ELSE
lresult = 4 ' folder has files
END IF
END IF
END IF
END IF
FUNCTION = s & " is " & CHOOSE$(lResult," a File","an Empty Folder","a Folder with sub folders but no files","a Folder containing files" ELSE "Neither a file nor a folder")
END FUNCTION
'
An AND with his result variables from API calls would have given TRUE or FALSE answer
So he'd get the wrong result if the folder was not archived or was indexed or had any other attributes set (Hidden, System...) ?
Both of the API functions he is calling just return a BOOLEAN according to MS: https://docs.microsoft.com/en-us/win...thisdirectorya
Return value
Type: BOOL
Returns (BOOL)FILE_ATTRIBUTE_DIRECTORY if the path is a valid directory; otherwise, FALSE.
https://docs.microsoft.com/en-us/win...irectoryemptya
Return value
Type: BOOL
Returns TRUE if pszPath is an empty directory. Returns FALSE if pszPath is not a directory, or if it contains at least one file other than "." or "..".
"For a list of related APIs and topics, see the See Also section."
No mention of PathIsDirectory or PathIsDirectoryEmpty in that section.
Would you rely on them returing a full FileAttribute mask value?
I'd stick with a boolean (IsTrue or IsFalse or =0 or <>0) test if using those functions,
Posts 7 and 8. "The problem with using DIR$(filespec) in an EXIST function is,it throws off DIR$ (without filespec) continuity in the rest of your app."
and "Thats persactly why I needed this..."
Which could also be a problem in my Post #13
But straighforward use of ISFILE and ISFOLDER is not a problem.
We process personal data about users of our site, through the use of cookies and other technologies, to deliver our services, and to analyze site activity. For additional details, refer to our Privacy Policy.
By clicking "I AGREE" below, you agree to our Privacy Policy and our personal data processing and cookie practices as described therein. You also acknowledge that this forum may be hosted outside your country and you consent to the collection, storage, and processing of your data in the country where this forum is hosted.
Comment