CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1
  1. #1
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Windows SDK File System: How to check for the existence of a directory/file?

    Q: How to check for the existence of a directory/file?

    A:

    Code:
    CString szPath("c:\\windows");
    CString szFile("c:\\test.txt");
    
    DWORD dwAttr = GetFileAttributes(szPath);
    if(dwAttr == 0xffffffff)
    {
      DWORD dwError = GetLastError();
      if(dwError == ERROR_FILE_NOT_FOUND)
      {
        // file not found
      }
      else if(dwError == ERROR_PATH_NOT_FOUND)
      {
        // path not found
      }
      else if(dwError == ERROR_ACCESS_DENIED)
      {
        // file or directory exists, but access is denied
      }
      else
      {
        // some other error has occured
      }
    }
    else
    {
      if(dwAttr & FILE_ATTRIBUTE_DIRECTORY)
      {
        // this is a directory
        if(dwAttr & FILE_ATTRIBUTE_ARCHIVE)
          // Directory is archive file
        if(dwAttr & FILE_ATTRIBUTE_COMPRESSED)
          // Directory is compressed
        if(dwAttr & FILE_ATTRIBUTE_ENCRYPTED)
          // Directory is encrypted
        if(dwAttr & FILE_ATTRIBUTE_HIDDEN)
          // Directory is hidden
        if(dwAttr & FILE_ATTRIBUTE_READONLY)
          // Directory is read-only
        if(dwAttr & FILE_ATTRIBUTE_REPARSE_POINT)
          // Directory has an associated reparse point
        if(dwAttr & FILE_ATTRIBUTE_SYSTEM)
          // Directory is part or used exclusively by the operating system
      }
      else
      {
        // this is an ordinary file
        if(dwAttr & FILE_ATTRIBUTE_ARCHIVE)
          // File is archive file
        if(dwAttr & FILE_ATTRIBUTE_COMPRESSED)
          // File is compressed
        if(dwAttr & FILE_ATTRIBUTE_ENCRYPTED)
          // File is encrypted
        if(dwAttr & FILE_ATTRIBUTE_HIDDEN)
          // File is hidden
        if(dwAttr & FILE_ATTRIBUTE_NOT_CONTENT_INDEXED)
          // File will not be indexed
        if(dwAttr & FILE_ATTRIBUTE_OFFLINE)
          // Data of file is not immediately available
        if(dwAttr & FILE_ATTRIBUTE_READONLY)
          // File is read-only
        if(dwAttr & FILE_ATTRIBUTE_REPARSE_POINT)
          // File has an associated reparse point
        if(dwAttr & FILE_ATTRIBUTE_SPARSE_FILE)
          // File is a sparse file
        if(dwAttr & FILE_ATTRIBUTE_SYSTEM)
          // File is part or used exclusively by the operating system
        if(dwAttr & FILE_ATTRIBUTE_TEMPORARY)
          // File is being used for temporary storage
      }
    }
    Last edited by ovidiucucu; November 11th, 2022 at 01:20 AM. Reason: fix typo

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured