Task

Verify that a file called     input.txt     and   a directory called     docs     exist.



This should be done twice:  

   once for the current working directory,   and
   once for a file and a directory in the filesystem root.


Optional criteria (May 2015):   verify it works with:

   zero-length files
   an unusual filename:     `Abdu'l-Bahá.txt  


' FB 1.05.0 Win64
 
' Enable FileExists() function to be used
#include "file.bi"
 
' Use Win32 function to check if directory exists on Windows 10
Declare Function GetFileAttributes Lib "kernel32.dll" Alias "GetFileAttributesA" _
(ByVal lpFileName As ZString Ptr) As ULong
 
Const InvalidFileAttributes As ULong = -1UL
Const FileAttributeDirectory As ULong = &H10UL
 
Sub CheckFileExists(ByRef filePath As String)
  If FileExists(filePath) Then
    Print "'"; filePath; "' exists"
  Else
    Print "'"; filePath; "' does not exist"
  End If
End Sub
 
Sub CheckDirectoryExists(ByVal dirPath As ZString Ptr)
  Dim attrib As ULong = GetFileAttributes(dirPath) 
  Dim dirExists As ULong = attrib <> InvalidFileAttributes AndAlso (attrib And FileAttributeDirectory) <> 0 
  If dirExists Then
    Print "'"; *dirPath; "' exists"
  Else
    Print "'"; *dirPath; "' does not exist"
  End If 
End Sub
 
CheckFileExists(CurDir + "\input.txt")
Dim dirPath As String = CurDir + "\docs"
CheckDirectoryExists(StrPtr(dirPath))
CheckFileExists("c:\input.txt")
CheckDirectoryExists(StrPtr("c:\docs"))
Print
Print "Press any key to quit the program"
Sleep