Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3428216
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:55:56+00:00 2026-05-18T06:55:56+00:00

How to check if Directory already Exists in MFC(VC++) ? I am using below

  • 0

How to check if Directory already Exists in MFC(VC++) ?
I am using below code to get current application Path and there i am creating NDSLog folder
so that all my Logfiles should place there , now i want to check the condition if NDSLog folder already exists dont create it .How to do that ?

Thanks.

char strPathName[_MAX_PATH];
    ::GetModuleFileName(NULL, strPathName, _MAX_PATH);

    // The following code will allow you to get the path.
    CString newPath(strPathName);
    int fpos = newPath.ReverseFind('\\');

    if (fpos != -1)
    newPath = newPath.Left(fpos+1);     
    newPath += "NDSLog\\" ;

    CreateDirectory(newPath,NULL); 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T06:55:57+00:00Added an answer on May 18, 2026 at 6:55 am

    The simplest way to check if a file/directory exists is to use GetFileAttributes:

    if (GetFileAttributes(newPath) == INVALID_FILE_ATTRIBUTES) {
      CreateDirectory(newPath,NULL);
    }
    

    Note that the function will return INVALID_FILE_ATTRIBUTES even if it fails due to some other reason, such as not having permissions to access the file, so you should check the return value of CreateDirectory to make sure that it succeeded.

    Actually, you don’t need to check whether the directory already exists; CreateDirectory will set an error code if the directory already exists:

    if (!CreateDirectory(newPath,NULL)) {
      if (GetLastError() == ERROR_ALREADY_EXISTS) {
        // directory already exists
      } else {
        // creation failed due to some other reason
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to check whether a Unicode directory exists in Perl. I am using
I need to check a directory to see if there are any files whose
I need to check file extension when search over a directory. if using re
I have code using boost to list directory contents, iterate through each file, and
I'm trying to verify that a directory exists using Fortan90. On various sites I
I have a bit of code that is supposed to check whether a directory
How do I check if a directory exist on the sdcard in android?
I have directory with > 1000 .html files, and would like to check all
does anyone know how to check if a file or directory is either a
I am struggling with a method of walking a directory tree to check existence

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.