Check if a directory exists

[Autolink] Menu

 
/* sample code to test if a directory exists with restoring all       */
/* directories                                                        */

  do forever
    call lineOut , "Enter the name of the directory to test " ,
                   "(RETURN to end): "
    thisDir = strip( lineIn() )
    if thisDir = "" then
      leave

    if DirExist( thisDir ) <> "" then
      say "The directory <" || thisDir || "> exist."
    else
      say "The directory <" || thisDir || "> does not exist."
  end /* do forever */

exit 0

/* ------------------------------------------------------------------ */
/* function: Check if a directory exists                              */
/*                                                                    */
/* call:     DirExist( testDir )                                      */
/*                                                                    */
/* where:    testDir - name of the directory to test                  */
/*                                                                    */
/* returns:  full name of the directory or "" if the directory        */
/*           don't exist                                              */
/*                                                                    */
DirExist: PROCEDURE
  parse arg testDir .

                        /* init the return code                       */
  thisRC = ""

                        /* test for missing or invalid parameter      */
  testDir = strip( testDir )                                 /* v3.20 */
  if testDir = "" then                                       /* v3.20 */
    signal DirDoesNotExist                                   /* v3.20 */

  if right( testDir, 1 ) = '\' then                          /* v3.20 */
    testDir = dbrright( testDir,1 )                          /* v3.20 */
  testDir = testDir || '\.'                                  /* v3.20 */

                        /* install a temporary error handler to check */
                        /* if the drive with the directory to test is */
                        /* ready                                      */
  SIGNAL ON NOTREADY NAME DirDoesNotExist

                        /* check if the drive is ready                */
  call stream testDir || "\*", "D"


                        /* save the current directory of the current  */
                        /* drive                                      */
  curDir = directory()

                        /* save the current directory of the drive    */
                        /* with the directory to test                 */
  curDir1 = directory( fileSpec( "drive", testDir ) )


                        /* test if the directory exists               */
  thisRC = directory( testDir )

                        /* restore the current directory of the drive */
                        /* with the directory to test                 */
  call directory curDir1

                        /* restore the current directory of the       */
                        /* current drive                              */
  call directory curDir
DirDoesNotExist:

return thisRC


[Back: Get the current filesystem]
[Next: Work on directory trees]