CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Nov 2001
    Location
    Ukraine (Odesa, Lviv)
    Posts
    74

    How I can check file for existance?

    subj???

  2. #2
    Join Date
    May 1999
    Location
    West Sussex, England
    Posts
    1,939

    Re: How I can check file for existance?

    Code:
            CFileStatus    status;
            if (CFile::GetStatus(filename, status))
            {
                // file exists
    Please use meaningful question titles - "Help me" does not let me know whether I can help with your question, and I am unlikely to bother reading it.
    Please remember to rate useful answers. It lets us know when a question has been answered.

  3. #3
    Join Date
    Nov 2001
    Location
    Ukraine (Odesa, Lviv)
    Posts
    74

    Re: How I can check file for existance?

    10x, Roger

    also FindFirstFile

    sorry for sore dull ques...

  4. #4
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: How I can check file for existance?

    This question has been asked countless times. You should do a search before posting question.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  5. #5
    Join Date
    Sep 2004
    Location
    Redlands, CA
    Posts
    74

    Re: How I can check file for existance?

    You can also check for a file existance or a folder existence like that:

    HANDLE hFind = INVALID_HANDLE_VALUE;
    WIN32_FIND_DATA fileFound;

    // Search for the specified file
    hFind = FindFirstFile(filePath, &fileFound);

    // if file to be copied exists
    if (!(hFind == INVALID_HANDLE_VALUE))
    {
    // Do something here
    }

    I hope it helps!
    Poka!

  6. #6
    Join Date
    Nov 2001
    Posts
    323

    Re: How I can check file for existance?

    I prefer to use GetFileAttributes to check the existence of a file.
    And cilu's absolutely correct: you should always search CG and Google well before you post.
    Best Regards,

    --Zim
    If you find this post useful, please rate it.
    _________________________________
    "Have you the brain worms?!?!?"

  7. #7
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: How I can check file for existance?

    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
      }
    }

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