Thread: Function: check if it's a directory

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    5

    Question Function: check if it's a directory

    Hi all! Is there a Windows function that check me if it's a directory? If it doesn't exist, is there a mode?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    Or even more succintly
    PathIsDirectory Function (Windows)

  4. #4
    Registered User
    Join Date
    Jul 2011
    Posts
    5
    thank you both!

    For other user that they will have same problem, this is the solution:
    Code:
    ...
    isDirectory = GetFileAttributes( struttura.cFileName );
    if ( isDirectory == FILE_ATTRIBUTE_DIRECTORY )
    cout << "it's a dir" << endl;
    else 
    cout << "it isn't a dir" << endl;
    ....

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It's still wrong -> File Attribute Constants (Windows)
    The result is a bit-mask, so your example would fail for a hidden directory.

    You need to do
    Code:
    isDirectory = GetFileAttributes( struttura.cFileName ); 
    if ( ( isDirectory & FILE_ATTRIBUTE_DIRECTORY ) == FILE_ATTRIBUTE_DIRECTORY )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    Quote Originally Posted by Francesco Leone View Post
    Code:
    isDirectory = GetFileAttributes( struttura.cFileName );
    You do realise the attributes of the file/folder are present in the dwFileAttributes member of the WIN32_FIND_DATA structure?

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Francesco Leone View Post
    Hi all! Is there a Windows function that check me if it's a directory? If it doesn't exist, is there a mode?
    Use the PathIsDirectory() function...

    Obviously if it doesn't exist the PathIsDirectory() function is going to return false...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Check if a directory exist?
    By kevinawad in forum C++ Programming
    Replies: 12
    Last Post: 10-26-2008, 02:42 AM
  2. How can i check a directory for certain files?
    By patrioticpar883 in forum C++ Programming
    Replies: 13
    Last Post: 02-01-2008, 05:27 PM
  3. Check directory program is run from?
    By willc0de4food in forum C Programming
    Replies: 10
    Last Post: 06-29-2005, 02:36 AM
  4. Check if file is a directory
    By cristane in forum C Programming
    Replies: 3
    Last Post: 06-16-2005, 02:58 PM
  5. How to check a directory is exist or not?
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 03-23-2002, 10:13 AM