MFC: How do you check if a directory exists before creating?

This is a discussion on MFC: How do you check if a directory exists before creating? within the Windows Programming forums, part of the Platform Specific Boards category; I'm creating a directory with: Code: temp.Format("%s\%d",path,p); CreateDirectory(temp,NULL); and I'd like to check to see if the create failed, but ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    72

    MFC: How do you check if a directory exists before creating?

    I'm creating a directory with:
    Code:
    	  temp.Format("%s\\%d",path,p);
    	  CreateDirectory(temp,NULL);
    and I'd like to check to see if the create failed, but it fails if the directory exists, so I'd like to check if it exists forst, then try to create & check for fail.

    How would I do that? (or is there a better way to do it?)

  2. #2
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    Try

    Code:
    /* 
     * Checks if a file or directory exists. 
     */ 
    bool checkFileExists(LPSTR dirName) { 
      WIN32_FIND_DATA  data; 
      HANDLE handle = FindFirstFile(dirName,&data); 
      return ( handle != INVALID_HANDLE_VALUE ); 
    }

  3. #3
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
    bool checkFileExists(LPCTSTR dirName) 
    { 
      WIN32_FIND_DATA  data; 
      HANDLE handle = FindFirstFile(dirName,&data); 
      if (handle != INVALID_HANDLE_VALUE)
      {
        FindClose(handle);
        return true;
      }
    
      return false;
    }
    or
    Code:
    BOOL checkPathExists(LPCTSTR szPath)
    {
    	return (GetFileAttributes(szPath) != INVALID_FILE_ATTRIBUTES);
    }
    
    BOOL isDirectory(LPCTSTR szPath)
    {
    	DWORD dwAttrib = GetFileAttributes(szPath);
    
    	return (dwAttrib != INVALID_FILE_ATTRIBUTES &&
    	        (dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
    }

  4. #4
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    Ah, I didn't know I had an open handle. Thanks anonytmouse.

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    72
    Great! thanks for the info, guys.

  6. #6
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,856
    I'm not sure about directories but with files you can look for error # 183 ERROR_ALREADY_EXISTS

    //if failed check Why with GetlastError()
    iError = GetLastError();
    if(ERROR_ALREADY_EXISTS!=iError)
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating and Empty MFC project in Visual Studio?
    By Swerve in forum Windows Programming
    Replies: 7
    Last Post: 11-01-2008, 04:43 PM
  2. Replies: 1
    Last Post: 12-10-2007, 05:26 PM
  3. Replies: 9
    Last Post: 03-03-2006, 09:11 PM
  4. check if the file exists
    By ipe in forum C Programming
    Replies: 2
    Last Post: 03-15-2003, 08:18 AM
  5. How to check a directory is exist or not?
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 03-23-2002, 09:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21