User Tools

Site Tools


checking_20whether_20a_20directory_20exists

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
checking_20whether_20a_20directory_20exists [2018/03/31 13:19]
127.0.0.1 external edit
checking_20whether_20a_20directory_20exists [2019/04/25 22:42]
richardrussell Added caution about Windows 10
Line 2: Line 2:
  
 //by Richard Russell, May 2006, amended May 2010//\\ \\  It's easy to check whether a file exists: you just try to open it:\\  //by Richard Russell, May 2006, amended May 2010//\\ \\  It's easy to check whether a file exists: you just try to open it:\\ 
 +<code bb4w>
         DEF FNcheckfile(file$)         DEF FNcheckfile(file$)
         LOCAL F%         LOCAL F%
Line 7: Line 8:
         IF F% CLOSE #F%         IF F% CLOSE #F%
         = F%         = F%
 +</​code>​
 This function returns a non-zero value if the file exists and zero if not (strictly speaking it may also return zero if the file exists but is not accessible, but that's usually what is wanted).\\ \\  To check for the existence of a directory (folder) you can use the fact that the '​virtual'​ file NUL exists in every directory:​\\ ​ This function returns a non-zero value if the file exists and zero if not (strictly speaking it may also return zero if the file exists but is not accessible, but that's usually what is wanted).\\ \\  To check for the existence of a directory (folder) you can use the fact that the '​virtual'​ file NUL exists in every directory:​\\ ​
 +<code bb4w>
         DEF FNcheckdir(dir$)         DEF FNcheckdir(dir$)
         LOCAL F%         LOCAL F%
Line 13: Line 16:
         IF F% CLOSE #F%         IF F% CLOSE #F%
         = F%         = F%
-This function returns a non-zero value if the directory exists and zero if not. \\ \\  If you want to know whether a file **or** directory with a given name exists you can use a combination of the above functions. However an alternative is to use Windows API calls:​\\ ​+</​code>​ 
 +This function returns a non-zero value if the directory exists and zero if not.  ​**Caution**:​ In at least some versions of Windows 10 this does not work, and will return a non-zero value for a file as well as a directory. ​ This appears to be a bug in Windows. 
 + 
 +If you want to know whether a file **or** directory with a given name exists you can use a combination of the above functions. However an alternative is to use Windows API calls:​\\ ​ 
 +<code bb4w>
         DEF FNcheckdir(name$)         DEF FNcheckdir(name$)
         LOCAL dir%, sh%         LOCAL dir%, sh%
Line 21: Line 28:
         IF sh%<>​-1 SYS "​FindClose",​ sh% ELSE = 0         IF sh%<>​-1 SYS "​FindClose",​ sh% ELSE = 0
         = 8 - (!dir% AND 16)         = 8 - (!dir% AND 16)
 +</​code>​
 This function returns a negative value if a directory with the specified name exists, a positive value if a file of that name exists, and zero if neither a file nor a directory of that name exists. This function returns a negative value if a directory with the specified name exists, a positive value if a file of that name exists, and zero if neither a file nor a directory of that name exists.
checking_20whether_20a_20directory_20exists.txt · Last modified: 2019/04/25 22:42 by richardrussell