Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Check for directory existance.

345 views
Skip to first unread message

jon...@my-deja.com

unread,
Sep 8, 2000, 12:27:17 PM9/8/00
to
I know that the file() function can be used to determine whether a
specific file exists, but is there any function that will determine
whether a directory exists?

I tried File("TEMP\*.*") but this didn't work <g>.

TIA


Sent via Deja.com http://www.deja.com/
Before you buy.

Rene Pilon

unread,
Sep 8, 2000, 12:55:01 PM9/8/00
to
Another problem with the File() function is that it doesn't recognize long
filenames or dir names.

What about IF FILE( "C:\MYDIR\." ) // notice the period after the last
slash.

Worth a try.


Rene Pilon

unread,
Sep 8, 2000, 12:57:02 PM9/8/00
to
Or what about:

aDir := Directory( "\temp\*.*" ) // probably make sure the path is
complete...
lIsDir := Valtype( aDir ) == "A" .And. !Empty( aDir )

T.S.LIM

unread,
Sep 8, 2000, 2:20:50 PM9/8/00
to

<jon...@my-deja.com> wrote in message news:8pb409$qg$1...@nnrp1.deja.com...

Try this
FILE(cPath + "\NUL") to check whether the directory cPath exist!

HTH
T.S.LIM


mcclym...@my-deja.com

unread,
Sep 11, 2000, 8:58:19 PM9/11/00
to
You will need to test under the particular environment you will be
running or supporting. FILE(cPath + "\NUL") is a very good suggestion
however as it works on Novell network drives, and local drives
including floppies. It does not work on NT network drives.

The most universal way (not however under flagship, etc) will be to
create a standalone function called DirExist or use the same named
function from the Overlay or Nanforum toolkits.

Points for build-your-own:

Make sure that the trailing slash is in place or else put it there

Hard code a dummy file name which should not be in place such as
"ad94ptzz.5tr" or create a random name at run-time which will be
best in a multi user environment unless you include part of the
user name somehow

nHandle := fcreate( cPath + cFileName )
if nHandle == -1
/* problem
it is left as an exercise to determine how this will be
reported / handled in your app
did not have rights, subdir did not exist, file by the
same name is being held open by another user, etc
*/
return .f.
endif

// getting here means that cPath does exist
// and the user has rights, etc
fclose( nHandle ) // results in a zero byte-file
inkey(0.1) // disk latency safety
ferase( cPath + cFileName ) // file gone

return .t.

PowerBuilder

unread,
Sep 13, 2000, 3:00:00 AM9/13/00
to
Following are the functions that I wrote in CA-Clipper over 8 years
ago to check for a directory's existence for one of my Run-Time
User-Interface I developed in Clipper. These functions worked well in
tandem as I used them extensively:

**********************************************************************
*********
FUNCTION DirExist( cDir, cMesg, Alert, aDir )
**********************************************************************
*********
//
IF cDir == NIL
RETURN ( .F. ) // Error!
END
//
cDir := Upper( AllTrim( cDir ))
//
IF ScanDir( cDir, aDir ) != SD_DIR // SD_DIR = 2
//
DEFAULT Alert TO .T., ;
cMesg TO NULL // NULL = ""
//
IF Alert
//
// NOT: BeepAlert() -> Protected!!
Alert( "Error!;" + ;
( cDir ) + "\ sub-directory does not exist!;" + ;
"Do create it by typing the command:-;;" + ;
"MD " + ( cDir ) + ";;" + ;
"- from the DOS Prompt and press RETURN;;" + ;
( cMesg ))
//
ENDIF
//
RETURN ( .F. ) // Error!!
//
ENDIF
//
RETURN ( .T. ) // Found!


**********************************************************************
*********
FUNCTION ScanDir( cDir, aDir )
**********************************************************************
*********
LOCAL nPos := 0 // Protected initialization
//
IF cDir == NIL
RETURN ( SD_NONE ) // SD_NONE = 0
END
//
// File does not find the Directories
IF File( cDir ) // It is only a file - not a directory!!
RETURN ( SD_FILE ) // SD_FILE = 1
END
//
// Assign a default value if none was supplied
// Protected/recommended position!
DEFAULT aDir TO Directory( "*.", "D" ) // Include
.\DIRectories!
//
// cDir directory will NOT be a "file" here as scanning for files
was already done above!!
cDir := AllTrim( cDir )
//
IF Len( aDir ) > 0
nPos := AScan( aDir, { | Elem | Elem[ F_NAME ] == cDir } )
END
//
// nPos > 0 IF cDir was scanned!
RETURN ( If( nPos > 0, SD_DIR, SD_NONE ) )


Please note the File() function does "not" find DIRECTORIES and
"cannot" sense HIDDEN files! Whereas the Directory() function can be
used
to build up a list of Files & Folders. The trick here is to check for
the file first and then
build up an array of files+folders and check for the existence of the
element that
cannot be a file if it exists!

But again, it fails if the file is HIDDEN even if we included hidden
files in the Directory()!

Hope these are simpler and serve your purpose. :~)


Shekar
NJ

======================================================================
==

jon...@my-deja.com wrote in message <8pb409$qg$1...@nnrp1.deja.com>...

Przemyslaw Czerpak

unread,
Sep 16, 2000, 3:00:00 AM9/16/00
to
On Wed, 13 Sep 2000 19:44:03 -0400,
PowerBuilder <SSR...@UPS.Com> wrote:
>Following are the functions that I wrote in CA-Clipper over 8 years
>ago to check for a directory's existence for one of my Run-Time
>User-Interface I developed in Clipper. These functions worked well in
>tandem as I used them extensively:
[...]

function IsDir( cDir )
return ( !file( cDir ) .and. file( cDir + "\nul" ) )

easier, I hope ;-)

regars,
Przemek

Amilcar Camargo

unread,
Sep 16, 2000, 3:00:00 AM9/16/00
to
In article <8pb409$qg$1...@nnrp1.deja.com>,
jon...@my-deja.com wrote:

>I know that the file() function can be used to determine whether a
>specific file exists, but is there any function that will determine
>whether a directory exists?
>
>I tried File("TEMP\*.*") but this didn't work <g>.
>
>TIA

Hi jon_sk:

The following function will work under almost all OS and circumstances:

FUNCTION ISDIRECTORY( cFile )
LOCAL lRet := .F.

If Empty( cFile ) // No name
RETURN lRet
Endif

cFile := Trim( cFile )
DO WHILE Subs( cFile, -1 ) == "\" // Trim out trailing '\'
cFile := Subs( cFile,1,Len( cFile ) - 1 )
ENDDO

lRet := ASCAN( DIRECTORY( cFile, "D" ), {|x| "D" $ x[F_ATTR] } ) > 0

RETURN lRet

HTH
--
Amilcar Camargo
Sand, S. A.
Guatemala, C. A.

Phil Barnett

unread,
Sep 16, 2000, 3:00:00 AM9/16/00
to

Problem. Always returns .T. on Netware...

--
Phil Barnett mailto:midnight @ the-oasis.net
Oasis WWW http://www.the-oasis.net
FTP Site ftp://ftp.the-oasis.net
Clipper FAQ http://www.the-oasis.net/clipper.html
Harbour Project http://www.Harbour-Project.org

If you are banking on a free software project
getting 'Finished', then you are probably a
poor investor in the first place.

Przemyslaw Czerpak

unread,
Oct 6, 2000, 3:00:00 AM10/6/00
to
On Sat, 16 Sep 2000 22:30:18 -0400,
Phil Barnett <dev...@iag.net> wrote:
>On Sat, 16 Sep 2000 10:55:58 +0000 (UTC), dru...@polbox.com
>(Przemyslaw Czerpak) wrote:
>
>>On Wed, 13 Sep 2000 19:44:03 -0400,
>>PowerBuilder <SSR...@UPS.Com> wrote:
>>>Following are the functions that I wrote in CA-Clipper over 8 years
>>>ago to check for a directory's existence for one of my Run-Time
>>>User-Interface I developed in Clipper. These functions worked well in
>>>tandem as I used them extensively:
>>[...]
>>
>>function IsDir( cDir )
>>return ( !file( cDir ) .and. file( cDir + "\nul" ) )
>>
>>easier, I hope ;-)
>
>Problem. Always returns .T. on Netware...

You are right.
Netware clients redirects DOS devices.

probaly function 'file' for
FOO\LPT1
BAR\AUX
FOO_BAR\COM2
BAR_FOO\CLOCK$
...

will retern .T. even the directories (FOO,BAR,...) don't exist
on netware volumes.

regards,
Przemek

0 new messages