'dirExists' function ;)

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
petan
Posts: 683
Joined: Feb 16, 2010 15:34
Location: Europe
Contact:

'dirExists' function ;)

Post by petan »

(Not sured if this was posted previously...by anyone)
This function 'dirExists' is similar to built-in FB command 'fileExists'.
Really helpfull in case of your own big dBs managing, etc.

Code: Select all

function dirExists(byval somePathToTest as string) as integer
 'return values as written on wikiDoc 6.8.2016
 '0 -> folder exists    Other values -> NON existing folder
 Dim As String whereWeCall=curdir
 Dim As Integer result=-1
 result=ChDir(somePathToTest)	'try to change a folder
 ChDir(whereWeCall)				'return to calling point ! MUST BE
 return result
end function
Enjoy ;)

edited -

Here is version for ANY custom logics values of integer,
and line #include "vbcompat.bi" is not needed in this case ;)

Code: Select all

(...)
Dim shared As Integer myTrue=1,myFalse=-1	'global custom True/False logic values of integer ! 
  'you can set it as you want, e.g.  1/0 or 1/-1 or 0/1 or 0/-1 ... or whatever
(...)
declare function dirExists(byval somePathToTest as string) as integer
(...)
function dirExists(byval somePathToTest as string) as integer
 Dim As String whereWeCall=curdir
  'myTrue,myFalse are a global integer values for TRUE/FALSE logic of CUSTOM
 ChDir(somePathToTest)   'try to change a folder
/' 
 'multi line code
 if curdir<>whereWeCall then 
	return myTrue		'folder changed , so exists
 else
	return myFalse		'No change , so Not existing
 end if
 '/
 'or one line code
 if curdir<>whereWeCall then return myTrue else return myFalse		
 ChDir(whereWeCall)            'return to calling point ! MUST BE
end function
(...)
Usage:

Code: Select all

? dirExists("augy-11")						'or
? dirExists("/home/knoppix/sGUI")			'or


if dirExists("augy-11") then ...			'or
if not dirExists("augy-11") then ...		'or
if dirExists("augy-11")=myTrue then ...		'or
if dirExists("augy-11")<>myTrue then ...
(...)
Last edited by petan on Aug 15, 2016 0:08, edited 1 time in total.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: 'dirExists' function ;)

Post by Tourist Trap »

petan wrote:

Code: Select all

function dirExists(byval somePathToTest as string) as typeOf(err())
     Dim As String whereWeCall=curdir
     Dim As Integer result=-1
     result=ChDir(somePathToTest)	'try to change a folder
     ChDir(whereWeCall)				'return to calling point ! MUST BE
     '
     return result
end function
Thanks a lot for this petan!

I would like to share with you a little trick I use to avoid writting:

'return values as written on wikiDoc 6.8.2016
'0 -> folder exists
Other values -> NON existing folder
I use "f() as typeOf(err())".
That is possible because the compiler can resolve what is typeOf( err() ) at compile time. And what this means for me is that it relies on how this function is currently defined with all the related conventions. This most often means that I simply write a wrapper that returns a value that is a standard error code of the language (0 == success).

Just a convention whatever, for self-documenting the code.

I suppose here that "chDir()" is one of those function bounded to "err()". Not all the built-in functions are! We still had a discussion about this with fxm if I remember well.
fxm
Moderator
Posts: 12083
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: 'dirExists' function ;)

Post by fxm »

petan wrote:This function 'dirExists' is similar to built-in FB command 'fileExists'.
Yes, but so why the return logic is different?
I would prefer:

Code: Select all

Function DirExists(Byval somePathToTest As Zstring Ptr) As Long
  '-1 -> folder exists    0 -> NON existing folder
  Dim As String whereWeCall = CurDir
  Dim As Long result = ChDir(*somePathToTest)   'try to change a folder
  ChDir(whereWeCall)            'return to calling point ! MUST BE
  Return Not result
End Function
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: 'dirExists' function ;)

Post by Tourist Trap »

Damn with the convention of FileExists then this is exactly the kind of example where typeOf(err()) doesn't apply ??

Whatever, I also do things like that to highlight a special convention for the return code:

Code: Select all

#define _dirExistsSpecialConventionReturnType			integer
function dirExists(byval somePathToTest as string) as _dirExistsSpecialConventionReturnType
     Dim As String whereWeCall=curdir
     Dim As Integer result=-1
     result=ChDir(somePathToTest)   'try to change a folder
     ChDir(whereWeCall)            'return to calling point ! MUST BE
     '
     return result
end function
#undef dirExistsSpecialConventionReturnType			
It puts a little of troube over FB that not all the file handling functions are following the same standard. Err() for all would have been very nice, since this is also the winapi convention.

Even further in (what I call) self-documentation:

Code: Select all

#macro _specialReturnType(info)
	 integer
#endmacro
function dirExists(byval somePathToTest as string) as _specialReturnType(not_an_Err_like)
     Dim As String whereWeCall=curdir
     Dim As Integer result=-1
     result=ChDir(somePathToTest)   'try to change a folder
     ChDir(whereWeCall)            'return to calling point ! MUST BE
     '
     return (not result)
end function
#undef specialReturnType
vdecampo
Posts: 2992
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Re: 'dirExists' function ;)

Post by vdecampo »

Why not just...

Code: Select all

#Include "dir.bi"

Function DirExists(path As String) As Integer
	Return Dir(path,fbDirectory) <> ""
End Function
Returns TRUE or FALSE

-Vince
petan
Posts: 683
Joined: Feb 16, 2010 15:34
Location: Europe
Contact:

Re: 'dirExists' function ;)

Post by petan »

Hi, vdecampo
Thanks for this really pretty good code !
Due my occupied time I did no tests with 'dir' command, also I am not familiar with it yet,
so your code is very helpfull for me.

Screenshot shows testing both functionsImage
picture demonstrates using of inverted logic in that ones.

As fxm rightly said, used logic can be different by everyone's own needs.(Tuning is easy.)
Therefore I wrote the code as simply as possible, by wikiDoc.
Keeping similar logic as 'fileExists' has - it's good idea, fxm, thanks.(First post will be updated tonight)

Due fact the 'fileExists' needs to have included "file.bi" or "vbcompat.bi"
and 'dirExists' needs "dir.bi" or "vbcompat.bi", the best shot is to use #include "vbcompat.bi" for both in app's sourcecode.
adele
Posts: 47
Joined: Jun 13, 2015 19:33

Re: 'dirExists' function ;); Different Solution

Post by adele »

Hi all,

I do not know if with LINUX there are "forbidden folders" too. If yes, you can`t CD to such a folder, even if it exists, but you might want to know whether it exists or not (maybe before you try to create one with a name already used).

I Cannot prove that the code runs on LINUX too, but with Win it`s OK.

BTW: I can not understand the discussion concerning the return code.
If I ask "IsDIR <thisdir>" then either YES (TRUE) or NO (FALSE), where NO is Zero==0==FALSE

Until now, I used one of the following functions:

Code: Select all


REM 'DirExist eq ExistDIR

#include ONCE "dir.bi"

function Exist (fn as String) as integer
 var result=0
 var s1=fn
 dir (s1,&h7F,@result)  ' &h3F should work too, just "or" all fb* values listed in 
 return result 
End Function

function ExistDir (fn as String) as integer
 var result=0
  if (Exist(fn) AND fbDirectory) <> 0 then return result 
  return 0 
End Function

function IsDIR (fn as String) as Boolean
  return (Exist(fn) AND fbDirectory) <> 0  
End Function

function IsFILE (fn as String) as Boolean
  return (Exist(fn) AND fbDirectory) = 0  
End Function


? IsDIR   ( "C:\System volume information" )
? IsFILE  ( "C:\System volume information" )
? IsDIR ("\.")
? Exist ("\.")
? IsDIR ("\\")
? IsDIR  (CurDir)
? IsFILE (CurDir)

Sleep
Stop
Have a nice day

adi
petan
Posts: 683
Joined: Feb 16, 2010 15:34
Location: Europe
Contact:

Re: 'dirExists' function ;)

Post by petan »

Hi adele,
thanks for sharing your code !
Another solution with booleans on FB 1.04+ version.
Thats also answer why I made the code with any custom logics.I use FB 1.01 without booleans.
And some people still use only FB 0.24+, so 'dirExists()' is very easy to implement/enhance in their old programs.

Primarily I use it in a path checks of KNOWN/visible dBs folders hierarchy, os testing hidden/system flag is unneeded.
Your idea is interesting for testing on linux, thx.
Nice day to you too :D
Post Reply