Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. To check and Create Folder in Win32 API

To check and Create Folder in Win32 API

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++jsontutorialquestion
10 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Suresh H
    wrote on last edited by
    #1

    Hello all, I want to check for some folder in C:\ drive if that folder exists by default then no action if that folder does not exits I have to create the folder. For that I have used below code CFileFind finder; CString szPath , szCMD; szPath = "c:\\A\\B"; if(!finder.FindFile(szPath)) { szCMD = "mkdir "; szCMD += szPath; system((LPCTSTR)szCMD); } This code properly in MFC application , I want to know how to do it in Win32 API application?? Please help me with this problem. Thanking you, Suresh HC.

    C R H J C 5 Replies Last reply
    0
    • S Suresh H

      Hello all, I want to check for some folder in C:\ drive if that folder exists by default then no action if that folder does not exits I have to create the folder. For that I have used below code CFileFind finder; CString szPath , szCMD; szPath = "c:\\A\\B"; if(!finder.FindFile(szPath)) { szCMD = "mkdir "; szCMD += szPath; system((LPCTSTR)szCMD); } This code properly in MFC application , I want to know how to do it in Win32 API application?? Please help me with this problem. Thanking you, Suresh HC.

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #2

      You may use FindFirstFile API. :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

      1 Reply Last reply
      0
      • S Suresh H

        Hello all, I want to check for some folder in C:\ drive if that folder exists by default then no action if that folder does not exits I have to create the folder. For that I have used below code CFileFind finder; CString szPath , szCMD; szPath = "c:\\A\\B"; if(!finder.FindFile(szPath)) { szCMD = "mkdir "; szCMD += szPath; system((LPCTSTR)szCMD); } This code properly in MFC application , I want to know how to do it in Win32 API application?? Please help me with this problem. Thanking you, Suresh HC.

        R Offline
        R Offline
        Rajesh R Subramanian
        wrote on last edited by
        #3

        #include "shlwapi.h"

        TCHAR szDir[20];

        _tcscpy( szDir,_T("D:\\MyNewDir\\") );

        if(!PathFileExists(szDir))//the directory already exists
        if( CreateDirectory(szDir,NULL) )
        //MessageBox(NULL,_T("Created"),NULL,MB_OK);

        PS: You will need to link to shlwapi.lib for using PathFileExists()


        Nobody can give you wiser advice than yourself. - Cicero ப்ரம்மா

        S 1 Reply Last reply
        0
        • S Suresh H

          Hello all, I want to check for some folder in C:\ drive if that folder exists by default then no action if that folder does not exits I have to create the folder. For that I have used below code CFileFind finder; CString szPath , szCMD; szPath = "c:\\A\\B"; if(!finder.FindFile(szPath)) { szCMD = "mkdir "; szCMD += szPath; system((LPCTSTR)szCMD); } This code properly in MFC application , I want to know how to do it in Win32 API application?? Please help me with this problem. Thanking you, Suresh HC.

          H Offline
          H Offline
          Hamid Taebi
          wrote on last edited by
          #4

          See PathFileExists


          WhiteSky


          1 Reply Last reply
          0
          • S Suresh H

            Hello all, I want to check for some folder in C:\ drive if that folder exists by default then no action if that folder does not exits I have to create the folder. For that I have used below code CFileFind finder; CString szPath , szCMD; szPath = "c:\\A\\B"; if(!finder.FindFile(szPath)) { szCMD = "mkdir "; szCMD += szPath; system((LPCTSTR)szCMD); } This code properly in MFC application , I want to know how to do it in Win32 API application?? Please help me with this problem. Thanking you, Suresh HC.

            J Offline
            J Offline
            James R Twine
            wrote on last edited by
            #5

            IF the directory has to exist regardless of it being there already or your having to create it, just call ::CreateDirectory(...)!  If the directory already exists, it will return an error, if it does not, it will attempt to create it.    No need for all that other crap unless you really need to know if it exists beforehand.    Peace!

            -=- James
            Please rate this message - let me know if I helped or not! * * * If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
            Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
            See DeleteFXPFiles

            1 Reply Last reply
            0
            • R Rajesh R Subramanian

              #include "shlwapi.h"

              TCHAR szDir[20];

              _tcscpy( szDir,_T("D:\\MyNewDir\\") );

              if(!PathFileExists(szDir))//the directory already exists
              if( CreateDirectory(szDir,NULL) )
              //MessageBox(NULL,_T("Created"),NULL,MB_OK);

              PS: You will need to link to shlwapi.lib for using PathFileExists()


              Nobody can give you wiser advice than yourself. - Cicero ப்ரம்மா

              S Offline
              S Offline
              Suresh H
              wrote on last edited by
              #6

              Thank you guys CPallini , brahmma , WhiteSky , James R. Twine thanks for the examples. Now I modified the code to as follows

              = "C:\\A\\B"; char *lpStr; lpStr = buf; ret = PathFileExists(lpStr); if(ret == 1) { CreateDirectory(lpStr,NULL); }
              How to link shlwapi.lib file to code ??? I am getting below error error LNK2001: unresolved external symbol __imp__PathFileExistsA@4 fatal error LNK1120: 1 unresolved externals

              H P 2 Replies Last reply
              0
              • S Suresh H

                Thank you guys CPallini , brahmma , WhiteSky , James R. Twine thanks for the examples. Now I modified the code to as follows

                = "C:\\A\\B"; char *lpStr; lpStr = buf; ret = PathFileExists(lpStr); if(ret == 1) { CreateDirectory(lpStr,NULL); }
                How to link shlwapi.lib file to code ??? I am getting below error error LNK2001: unresolved external symbol __imp__PathFileExistsA@4 fatal error LNK1120: 1 unresolved externals

                H Offline
                H Offline
                Hamid Taebi
                wrote on last edited by
                #7

                Open property window of your project and include lib file to it(I dont have vs2003 now) but I think its on linker additional path(its first item on right panel)


                WhiteSky


                S 1 Reply Last reply
                0
                • H Hamid Taebi

                  Open property window of your project and include lib file to it(I dont have vs2003 now) but I think its on linker additional path(its first item on right panel)


                  WhiteSky


                  S Offline
                  S Offline
                  Suresh H
                  wrote on last edited by
                  #8

                  Thanks WhiteSky i did the changes its working now. Thank you very much.

                  1 Reply Last reply
                  0
                  • S Suresh H

                    Thank you guys CPallini , brahmma , WhiteSky , James R. Twine thanks for the examples. Now I modified the code to as follows

                    = "C:\\A\\B"; char *lpStr; lpStr = buf; ret = PathFileExists(lpStr); if(ret == 1) { CreateDirectory(lpStr,NULL); }
                    How to link shlwapi.lib file to code ??? I am getting below error error LNK2001: unresolved external symbol __imp__PathFileExistsA@4 fatal error LNK1120: 1 unresolved externals

                    P Offline
                    P Offline
                    PJ Arends
                    wrote on last edited by
                    #9

                    Suresh H wrote:

                    How to link shlwapi.lib file to code ???

                    When you include the header file simply add a pragma to link the lib

                    #include <shlwapi.h>
                    #pragma comment(lib, "shlwapi.lib")

                    Lot easier than mucking around in the project settings and you will never forget it if you reuse the file in another project.


                    You may be right I may be crazy -- Billy Joel -- Within you lies the power for good, use it!!!

                    1 Reply Last reply
                    0
                    • S Suresh H

                      Hello all, I want to check for some folder in C:\ drive if that folder exists by default then no action if that folder does not exits I have to create the folder. For that I have used below code CFileFind finder; CString szPath , szCMD; szPath = "c:\\A\\B"; if(!finder.FindFile(szPath)) { szCMD = "mkdir "; szCMD += szPath; system((LPCTSTR)szCMD); } This code properly in MFC application , I want to know how to do it in Win32 API application?? Please help me with this problem. Thanking you, Suresh HC.

                      C Offline
                      C Offline
                      Cristian Amarie
                      wrote on last edited by
                      #10

                      Use CreateFile with OPEN_EXISTING to detect if directory exists, and, if not, use CreateDirectory(Ex) to create it.

                      Nuclear launch detected

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups