Board index » delphi » What's the standard way for checking existence of directories ?

What's the standard way for checking existence of directories ?

For my app I need to check if the directory that the user specified really
exists. The problem is that the user can also specify the root of a drive.

I tried the following:
* using FileExists appeared only to work for the root of drive C: (not for d,e,f
  & g, and not for subdirectories).
* using Findfirst('*.*', faDirectory, ...) didn't work for drive-roots, but
  worked fine for normal directories.

There has to be a way to do this right. How ?

Thank you.
-----
Alban Hertroys
e-mail: A.G.Hertro...@student.utwente.nl
WWW:    http://wit401310.student.utwente.nl/
The Netherlands
-----
...I'll make no subscription to your paradise. (Crass / Stations of the cross)

 

Re:What's the standard way for checking existence of directories ?


In article <4jmrbj$...@dinkel.civ.utwente.nl>, "A.G.Hertroys" <A.G.Hertr
o...@student.utwente.nl> writes

Quote
>For my app I need to check if the directory that the user specified really
>exists. The problem is that the user can also specify the root of a drive.

>I tried the following:
>* using FileExists appeared only to work for the root of drive C: (not for d,e,f
>  & g, and not for subdirectories).
>* using Findfirst('*.*', faDirectory, ...) didn't work for drive-roots, but
>  worked fine for normal directories.

>There has to be a way to do this right. How ?

Well I haven't tried it in Delphi, but in batch files, the standard
trick is to look for a file called NUL in the the target directory.  If
it exists, the directory exists.

--
John Appleyard                  J...@polyhdrn.demon.co.uk
Polyhedron Software Ltd.        
Programs for Programmers - QA, Compilers, Graphics
************ Visit our Web site on http://www.polyhedron.co.uk/ ************

Re:What's the standard way for checking existence of directories ?


There also exist a DirectoryExists function in the FileCtrl unit.

If you don't want to include this unit try tyhis snippet of code :

{$I-}
   Chdir(P); {The directory you want}
{$I+}
   If IOResult <> 0 Then
      MessageDlg('Directory doesn't exist',....)

Re:What's the standard way for checking existence of directories ?


HI there,

Quote
>There also exist a DirectoryExists function in the FileCtrl unit.

Note: The DirectoryExists function has a bug in it that it does not recognize a
root directory that has NO subdirectories (common on blank formatted floppies
:-)...

Just letting you know...

Cheers,
George.---

Re:What's the standard way for checking existence of directories ?


In article <4jsfj5...@pauling.wadsworth.org>,
   George Silva <si...@snappy.globalone.net> wrote:

Quote
>HI there,

>>There also exist a DirectoryExists function in the FileCtrl unit.

>Note: The DirectoryExists function has a bug in it that it does not recognize a
>root directory that has NO subdirectories (common on blank formatted floppies
>:-)...

Hmm.  I've just looked into this, and you're quite right.  If you pass DirectoryExists() a drive
letter (like DirectoryExists('A:')), it has the effect of searching to see if *any* directories exist on
that drive instead of if a particular directory exists.  (The root would not count, of course,
because all disks have that.)  The reason that DirectoryExists('A:\') fails, is that the function
strips off trailing backslashes, which are invalid except for root directories, fooling logic later
in the function into thinking you've passed in 'A:', so it reports if any directories exist, which in
your scenario, they don't.  If you wish to check if a disk exists, by checking for its root, you can
simply append another backslash to the end to fool this function, like so:
DirectoryExists('A:\\'); or, if you want a more general solution that works for all directories, this
should also work: DirectoryExists(someDir + '\.');  Hope this is helpful!

<hr><center><b>
David S. Becker  <br>
<a href="mailto:d...@plaza.ds.adp.com">
d...@plaza.ds.adp.com  </a></b><br><i>
ADP Dealer Services (Plaza R&D)  <br>
(503) 402-3236
</i></center><hr>

Re:What's the standard way for checking existence of directories ?


In article <4jmrbj$...@dinkel.civ.utwente.nl>, A.G.Hertro...@student.utwente.nl says...

Quote

>For my app I need to check if the directory that the user specified really
>exists. ...
>There has to be a way to do this right. How ?

Try checking for the existence of a file/device named 'nul', which ought
to *exist* in any valid directory.

--
Ken Irving                ...eschew obfuscation
SysTech Control
Fairbanks  AK  

Re:What's the standard way for checking existence of directories ?


In article <447cc$111621.f3@PolarNet>,
   syst...@polarnet.fnsb.ak.us (Ken Irving) wrote:

Quote
>In article <4jmrbj$...@dinkel.civ.utwente.nl>, A.G.Hertro...@student.utwente.nl says...

>>For my app I need to check if the directory that the user specified really
>>exists. ...
>>There has to be a way to do this right. How ?

>Try checking for the existence of a file/device named 'nul', which ought
>to *exist* in any valid directory.

Look up the DirectoryExists() function, this is what is needed.  Checking for a file named NUL
is an old MS-DOS batch file trick, and is only appropriate there.

<hr><center><b>
David S. Becker  <br>
<a href="mailto:d...@plaza.ds.adp.com">
d...@plaza.ds.adp.com  </a></b><br><i>
ADP Dealer Services (Plaza R&D)  <br>
(503) 402-3236
</i></center><hr>

Re:What's the standard way for checking existence of directories ?


Quote
>   d...@plaza.ds.adp.com (David S. Becker) writes:
>  >>There also exist a DirectoryExists function in the FileCtrl unit.

Different topic, but in all versions of TP that I have had (4.0 -> 7.0), the most used part
of the documentation was the part that told me what all the units were, what functions
and procedures were in each unit, and what the declaration for each was, with some
little explanation...

WHERE the hell is the same info with Delphi?  I never got any printed docs that told me
 there was a DirExists function or a FileCtrl unit...

Online help is just useless...can I BUY a real, hardcopy, paper book  or something?  I
want a book I can read that tells me the same stuff they USED to give me as part of the deal.

---
Pat Anderson <p...@aa.net>

Other Threads