CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Mar 2002
    Posts
    47

    How can i check whether a particular directory exists or not?

    How can i check if some directory exists or not.
    For example if i want to check whether C:\TEMP exists or not. Also if this directory does not exist, how can i create this directory.


  2. #2

    Re: How can i check whether a particular directory exists or not?

    Hi,
    You can try the following,

    if(CreateDirectory("C:\\temp123",NULL) == 0)
    {
    if(ERROR_ALREADY_EXISTS ==GetLastError())
    AfxMessageBox("Already existing");
    }



    If the folder doesn't exist it will create it!!

    Regards,
    Sreedharan.
    Regards,
    Sreedharan

  3. #3
    Join Date
    Aug 1999
    Location
    Wisconsin
    Posts
    507

    Re: How can i check whether a particular directory exists or not?

    Use PathIsDirectory(..)



  4. #4
    Join Date
    Nov 2001
    Location
    San Diego
    Posts
    102
    What does this mean:
    unresolved external symbol __imp__PathIsDirectoryA@4
    Get this when using PathIsDirectory()

  5. #5
    Join Date
    Oct 2003
    Posts
    24
    Originally posted by mike@spb
    What does this mean:
    unresolved external symbol __imp__PathIsDirectoryA@4
    Get this when using PathIsDirectory()
    I get exactly the same thing when using PathIsDirectory. I've included shlwapi.h, specified the shlwapi.lib in the project settings but get this error whenever I included a call to a method in the the lib. What's going on?

  6. #6
    Join Date
    Dec 2002
    Posts
    437
    u can use CFile::GetStatus

  7. #7
    Join Date
    Oct 2003
    Posts
    24
    I ended up using _access() which has the same results but i still have no idea why I couldn't compile using PathIsDirectory. It must be something to do with project setttings in VS because my colleague created a new project exactly the same as my own one , imported the lib and included the header. Worked first time.

  8. #8
    Join Date
    Aug 1999
    Location
    Wisconsin
    Posts
    507
    Here's an alternate suggestion:

    Code:
        char path[] = "C:\\temp";
        SHFILEINFO shFileInfo;
    	if (SHGetFileInfo((LPCSTR)path, 0,
    		&shFileInfo, sizeof(SHFILEINFO), SHGFI_TYPENAME) != 0)
    	{
    		// Then file or folder exists
    		:
    	}
    	else
    	{
    		// File or folder does not exist
                       :
             }

  9. #9
    Join Date
    Aug 2004
    Location
    Bangalore
    Posts
    10

    Thumbs up Re: How can i check whether a particular directory exists or not?

    In C#,Its pretty simple.

    string destination = @"E:\Songs\sumi songs\fav\";
    if (!Directory.Exists(destination))
    Directory.CreateDirectory(destination);


    C# provides static class "Directory" which is helpfull to do many directory or folder related functionality

  10. #10
    Join Date
    Nov 2007
    Posts
    613

    Re: How can i check whether a particular directory exists or not?

    My understanding is that you need access to the directory for the temporary files. It it's so, simply call the GetTempPath function.

  11. #11
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: How can i check whether a particular directory exists or not?


  12. #12

    Re: MakeSureDirectoryPathExists

    One consideration with MakeSureDirectoryPathExists is that it requires one to bring Dbghelp.dll into the address space of one's app, which may be fine if one is using other functions from DbgHelp.dll. One may not wish to load DbgHelp.dll just for the sake of this one function, however.

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