How to check if Directory exists ?

Dear Experienced Users!

Similar to the check fileExists_(fileNameStrWithFullpath) is there a simple solution to check if a specific directory exists?

Thanks in advance,
vasgyuszi
vasgyuszi - Tue Apr 13 06:37:27 EDT 2010

Re: How to check if Directory exists ?
vasgyuszi - Tue Apr 13 07:55:37 EDT 2010

I've found a solution with usage of fileExists_ and the fact, that each directory shall contain the "." (dot) and ".." files:
 

dir= "C:\\Programme\\IBM\\Rational\\DOORS\\lib\\dxl\\addins\\user\\test\\."
if (!fileExists_(dir)) print "The folder " dir " does not exists.\n"

 


That's it :)

I'm happy to be able helping on me. It should be a good sign.

But, when you are knowing about a better solution, please tell me!

Best regards,
vasgyuszi

 

Re: How to check if Directory exists ?
Mathias Mamsch - Tue Apr 13 08:23:15 EDT 2010

vasgyuszi - Tue Apr 13 07:55:37 EDT 2010

I've found a solution with usage of fileExists_ and the fact, that each directory shall contain the "." (dot) and ".." files:
 

dir= "C:\\Programme\\IBM\\Rational\\DOORS\\lib\\dxl\\addins\\user\\test\\."
if (!fileExists_(dir)) print "The folder " dir " does not exists.\n"

 


That's it :)

I'm happy to be able helping on me. It should be a good sign.

But, when you are knowing about a better solution, please tell me!

Best regards,
vasgyuszi

 

Very clever solution!! If you are developing for linux it might fail, since I guess that "." & ".." stuff is only for windows. And since that is a DOS relic you might also want to check that this works for network shares, if you do a library function.

If you ever need more information about the directory checkout the 'Stat' type.

Regards, Mathias

Note that the fileExists_ function is defined in the fileops.inc file. Without it you can do something like this:
 

Stat s = create "C:\\Windows"
if (null s) { print "Not found"; halt }
if (directory s) print "Its a directory"  else print "No directory?" 
delete s

Re: How to check if Directory exists ?
vasgyuszi - Tue Apr 13 09:41:38 EDT 2010

Mathias Mamsch - Tue Apr 13 08:23:15 EDT 2010

Very clever solution!! If you are developing for linux it might fail, since I guess that "." & ".." stuff is only for windows. And since that is a DOS relic you might also want to check that this works for network shares, if you do a library function.

If you ever need more information about the directory checkout the 'Stat' type.

Regards, Mathias

Note that the fileExists_ function is defined in the fileops.inc file. Without it you can do something like this:
 

Stat s = create "C:\\Windows"
if (null s) { print "Not found"; halt }
if (directory s) print "Its a directory"  else print "No directory?" 
delete s

Thanks Mathias!

I forgot to mention my development environment is windows based, our DOORS 9.2 installations runs under WinXP Professional. I've looked for solutions under this limitations.

Best regrds,
vasgyuszi

A slightly elegant form (imho) of my example would be:

dir= "C:\\Programme\\IBM\\Rational\\DOORS\\lib\\dxl\\addins\\user\\test\\"
if (!fileExists_(dir".")) print "The folder " dir " does not exists.\n"

Re: How to check if Directory exists ?
faisal.zahidi@boeing.com - Thu Mar 24 21:06:27 EDT 2011

i am trying to create a windows directory and a text file under the new directory. if the directory already exists, i just want to create the file. I am getting an access error "can not open the folder".
For example,

Stream outF

string dirname="c\temp\foldername"
string filename="test.txt"
if(!fielExists(dir"."))
{
mkdir(dir, "")
outF=write(dirname filename)
}
else
{
outF=write(dirname filename)
}

Re: How to check if Directory exists ?
llandale - Fri Mar 25 14:12:53 EDT 2011

These are the two functions I use to figure out if a named Windows string is a file or a directory or neither:

//*********************
bool    fIsFile(string in_NameFile)
{     // Does the named windows file actually exist.
        // Name should be fully qualified
        return(!null in_NameFile and canOpenFile(in_NameFile, false))
}     // end fIsFile()
 
//**************************
bool    fIsDir(string in_NameDir)
{     // Does the named windows Directory (folder) actually exist.
 
                // First, if its actually a file then it isn't a directory Folder
        if (null in_NameDir or fIsFile(in_NameDir)) return(false)
 
        string NameFile = "", ErrMess
        noError()
        for NameFile in directory(in_NameDir) do
        {  break
        }
        ErrMess = lastError()
                   // Its a directory if there was no DXL error looking at its contents.
              // if (!null ErrMess) print "\t" in_NameDir "\t" fFormatSysErr(ErrMess) "\n"
        bool    IsDir   = (null ErrMess)
// print "isDirectory '" in_NameDir "'\t" IsDir "\n"
        return(IsDir)
}     // fIsDir()


I don't recall why this 6-year-old code is better than the traditional method in the other posts, but I'm reasonable sure I would not have bothered writing them unless they were.

Looking at the code I'm guessing that "IsDir = !null directory(in_NameDir)" would work, perhaps someone can verify that.

 

 

  • Louie