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. deleting all files from a folder

deleting all files from a folder

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
17 Posts 8 Posters 17 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.
  • H Hamid Taebi

    Is this good? WIN32_FIND_DATA find; SetCurrentDirectory("c:\\temp"); HANDLE handle=FindFirstFile("*.mdb",&find); while(FindNextFile(handle,&find)!=0) DeleteFile(find.cFileName); FindClose(handle); or you can use from //SHFileOperation(...);

    _**


    **_

    WhiteSky


    N Offline
    N Offline
    normanS
    wrote on last edited by
    #3

    Because you do findfirst() and findnext() before you do DeleteFile(), I think this will leave one mdb file behind.

    H 1 Reply Last reply
    0
    • T Tara14

      Hi, I have developed an application which creates .mdb files in a specific folder named store. This folder is created during installation. But when the program is uninstalled, since there are additional(new) files in the folder store, the folder does not get deleted from the computer. I thought, maybe I should create a small .exe program that deletes all the .mdbfiles from the store folder before the uninstall is run. The problem here is that I do not know the names of the files in the folder. Is there anyway or any function by which I can find and delete all .mdb files from the folder store? Thanks.

      _


      Fortitudine Vincimus!_

      M Offline
      M Offline
      Michael Dunn
      wrote on last edited by
      #4

      You can use SHFileOperation() which takes wildcards.

      --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ

      S T 3 Replies Last reply
      0
      • N normanS

        Because you do findfirst() and findnext() before you do DeleteFile(), I think this will leave one mdb file behind.

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

        No problem we can use from a Do while and yes its right. WIN32_FIND_DATA find; SetCurrentDirectory("c:\\temp"); HANDLE handle=FindFirstFile("*.mdb",&find); do DeleteFile(find.cFileName);//or you can use from //SHFileOperation(...); while(FindNextFile(handle,&find)!=0); FindClose(handle);

        _**


        **_

        WhiteSky


        T 1 Reply Last reply
        0
        • M Michael Dunn

          You can use SHFileOperation() which takes wildcards.

          --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ

          S Offline
          S Offline
          Sarath C
          wrote on last edited by
          #6

          is it safe(shell API have some dependency on the version?) to use shell API, if we there are some other win32 APi to do the same task? is it faster than Win32 APIs?

          SaRath.
          _"Before you can learn to recognize what's wrong, you must learn to recognize what's right" - Raymond Chen."


          My Blog | Understanding State Pattern_

          K 1 Reply Last reply
          0
          • S Sarath C

            is it safe(shell API have some dependency on the version?) to use shell API, if we there are some other win32 APi to do the same task? is it faster than Win32 APIs?

            SaRath.
            _"Before you can learn to recognize what's wrong, you must learn to recognize what's right" - Raymond Chen."


            My Blog | Understanding State Pattern_

            K Offline
            K Offline
            kakan
            wrote on last edited by
            #7

            According to my experience, it's risky to do changes in a directory structure while doing a FindFirst/Findnext loop. I have had problems doing that (such as deleting the found files) in the past. IMO, ShFileOperation is safer, and I would use it.

            Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson

            1 Reply Last reply
            0
            • H Hamid Taebi

              No problem we can use from a Do while and yes its right. WIN32_FIND_DATA find; SetCurrentDirectory("c:\\temp"); HANDLE handle=FindFirstFile("*.mdb",&find); do DeleteFile(find.cFileName);//or you can use from //SHFileOperation(...); while(FindNextFile(handle,&find)!=0); FindClose(handle);

              _**


              **_

              WhiteSky


              T Offline
              T Offline
              Tara14
              wrote on last edited by
              #8

              This is the program that I tried.

              #include "afxdisp.h"

              void main()
              {

              WIN32\_FIND\_DATA find;
              char	aExePath\[\_MAX\_PATH\];
              char	aDrive\[\_MAX\_DRIVE\];
              char	aDir\[\_MAX\_DIR\];
              CString	aPath;
              
              ::GetModuleFileName( AfxGetInstanceHandle(),aExePath,sizeof(aExePath));
              
              \_splitpath(aExePath,aDrive,aDir,NULL,NULL);
              
              aPath.Format("%s%s",aDrive,aDir);
              aPath =  aPath + "sbh\_store";
              
              SetCurrentDirectory(aPath); 
              HANDLE handle=FindFirstFile("\*.mdb",&find);
              do
              DeleteFile(find.cFileName);
              while(FindNextFile(handle,&find)!=0);
              FindClose(handle);
              

              }

              Here I get the error

              nafxcwd.lib(thrdcore.obj) : error LNK2001: unresolved external symbol __endthreadex
              nafxcwd.lib(thrdcore.obj) : error LNK2001: unresolved external symbol __beginthreadex

              When I comment out CString aPath; ::GetModuleFileName( AfxGetInstanceHandle(),aExePath,sizeof(aExePath)); I do not get the unresolved external symbol error. I guess I am missing something. Please can you tell me what I am not doing right. Have I missed out a header? -- modified at 5:40 Friday 18th August, 2006

              _


              Fortitudine Vincimus!_

              P 1 Reply Last reply
              0
              • M Michael Dunn

                You can use SHFileOperation() which takes wildcards.

                --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ

                T Offline
                T Offline
                Tara14
                wrote on last edited by
                #9

                I tried using SHFileOperation. But the program is crashing. This is what I did:

                SHFILEOPSTRUCT del;

                del.hwnd = NULL;
                del.wFunc = FO\_DELETE;
                del.pFrom = "C:\\\\temp\\\\\*.mdb";
                del.fFlags = FOF\_FILESONLY;
                
                SHFileOperation(&del);
                

                Something is wrong here. I tried searching the net, but I am not able to figure it out. Please can you help me. Thanks.

                _


                Fortitudine Vincimus!_

                P 1 Reply Last reply
                0
                • T Tara14

                  This is the program that I tried.

                  #include "afxdisp.h"

                  void main()
                  {

                  WIN32\_FIND\_DATA find;
                  char	aExePath\[\_MAX\_PATH\];
                  char	aDrive\[\_MAX\_DRIVE\];
                  char	aDir\[\_MAX\_DIR\];
                  CString	aPath;
                  
                  ::GetModuleFileName( AfxGetInstanceHandle(),aExePath,sizeof(aExePath));
                  
                  \_splitpath(aExePath,aDrive,aDir,NULL,NULL);
                  
                  aPath.Format("%s%s",aDrive,aDir);
                  aPath =  aPath + "sbh\_store";
                  
                  SetCurrentDirectory(aPath); 
                  HANDLE handle=FindFirstFile("\*.mdb",&find);
                  do
                  DeleteFile(find.cFileName);
                  while(FindNextFile(handle,&find)!=0);
                  FindClose(handle);
                  

                  }

                  Here I get the error

                  nafxcwd.lib(thrdcore.obj) : error LNK2001: unresolved external symbol __endthreadex
                  nafxcwd.lib(thrdcore.obj) : error LNK2001: unresolved external symbol __beginthreadex

                  When I comment out CString aPath; ::GetModuleFileName( AfxGetInstanceHandle(),aExePath,sizeof(aExePath)); I do not get the unresolved external symbol error. I guess I am missing something. Please can you tell me what I am not doing right. Have I missed out a header? -- modified at 5:40 Friday 18th August, 2006

                  _


                  Fortitudine Vincimus!_

                  P Offline
                  P Offline
                  prasad_som
                  wrote on last edited by
                  #10

                  see this solution

                  Prasad Notifier using ATL

                  T 1 Reply Last reply
                  0
                  • M Michael Dunn

                    You can use SHFileOperation() which takes wildcards.

                    --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ

                    T Offline
                    T Offline
                    Tara14
                    wrote on last edited by
                    #11

                    OH! I got it!! The pTo member of the SHFILEOPSTRUCT structure shoud be assigned NULL! Thanks.

                    _


                    Fortitudine Vincimus!_

                    1 Reply Last reply
                    0
                    • T Tara14

                      I tried using SHFileOperation. But the program is crashing. This is what I did:

                      SHFILEOPSTRUCT del;

                      del.hwnd = NULL;
                      del.wFunc = FO\_DELETE;
                      del.pFrom = "C:\\\\temp\\\\\*.mdb";
                      del.fFlags = FOF\_FILESONLY;
                      
                      SHFileOperation(&del);
                      

                      Something is wrong here. I tried searching the net, but I am not able to figure it out. Please can you help me. Thanks.

                      _


                      Fortitudine Vincimus!_

                      P Offline
                      P Offline
                      prasad_som
                      wrote on last edited by
                      #12

                      you need to set del.pTo ,too. i.e. set it to NULL if you are not using it. code will be,

                      SHFILEOPSTRUCT del;
                      del.hwnd = NULL;
                      del.wFunc = FO_DELETE;
                      del.pFrom = "C:\\temp\\*.mdb";
                      del.fFlags = FOF_FILESONLY;
                      del.pTo = NULL;
                      SHFileOperation(&del);

                      Oh ! you got it right already. :) -- modified at 5:59 Friday 18th August, 2006

                      Prasad Notifier using ATL

                      1 Reply Last reply
                      0
                      • P prasad_som

                        see this solution

                        Prasad Notifier using ATL

                        T Offline
                        T Offline
                        Tara14
                        wrote on last edited by
                        #13

                        Ah! How did I miss it. Thanks a billion!

                        _


                        Fortitudine Vincimus!_

                        H 1 Reply Last reply
                        0
                        • T Tara14

                          Hi, I have developed an application which creates .mdb files in a specific folder named store. This folder is created during installation. But when the program is uninstalled, since there are additional(new) files in the folder store, the folder does not get deleted from the computer. I thought, maybe I should create a small .exe program that deletes all the .mdbfiles from the store folder before the uninstall is run. The problem here is that I do not know the names of the files in the folder. Is there anyway or any function by which I can find and delete all .mdb files from the folder store? Thanks.

                          _


                          Fortitudine Vincimus!_

                          K Offline
                          K Offline
                          Kleser
                          wrote on last edited by
                          #14

                          hi, i don't want to give you an answer, instead i would like to ask about how you can make your app create an mdb file. because this is what i trying to do right now. if you don't mind, would you share me you little secret? thanks in advance. cheers -- modified at 10:25 Friday 18th August, 2006

                          T 1 Reply Last reply
                          0
                          • T Tara14

                            Ah! How did I miss it. Thanks a billion!

                            _


                            Fortitudine Vincimus!_

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

                            Is your problem solve

                            _**


                            **_

                            WhiteSky


                            T 1 Reply Last reply
                            0
                            • K Kleser

                              hi, i don't want to give you an answer, instead i would like to ask about how you can make your app create an mdb file. because this is what i trying to do right now. if you don't mind, would you share me you little secret? thanks in advance. cheers -- modified at 10:25 Friday 18th August, 2006

                              T Offline
                              T Offline
                              Tara14
                              wrote on last edited by
                              #16

                              Hello, I use DAO to make the connection. There are some really nice samples that come in msdn. I wrote my own little class to do the stuff taking help from the samples. Here is the database creation part. My database needed to be locked with a password.

                              //////////////////////////////////////////////////////
                              /////////********* CREATE DATABASE ***********///////////////////////////

                              void CDBcreate::CreateDatabase(CString m_strDBName)
                              {

                              int dwOptions =0;
                              dwOptions |=dbVersion30;

                              //Release table if selected
                              if (m_pTableDef)
                              {
                              m_pTableDef->Close();
                              delete m_pTableDef;
                              m_pTableDef = NULL;
                              }

                              //close database if open
                              if (m_pDatabase)
                              {
                              m_pDatabase->Close();
                              delete m_pDatabase;
                              m_pDatabase=NULL;
                              }

                              //create new database
                              m_pDatabase = new CDaoDatabase;
                              m_pDatabase->Create(m_strDBName, dbLangGeneral,dwOptions);

                              CString strConnect( _T( ";pwd=" ) );
                              COleVariant NewPassword( "password", VT_BSTRT ),OldPassword( "", VT_BSTRT );
                              DAO_CHECK( m_pDatabase->m_pDAODatabase->NewPassword( V_BSTR( &OldPassword ),
                              V_BSTR( &NewPassword ) ) );
                              m_bOpen=TRUE; // flag of currently open table

                              }

                              /////////********* END CREATE DATABASE ***********///////////////////////////

                              _


                              Fortitudine Vincimus!_

                              1 Reply Last reply
                              0
                              • H Hamid Taebi

                                Is your problem solve

                                _**


                                **_

                                WhiteSky


                                T Offline
                                T Offline
                                Tara14
                                wrote on last edited by
                                #17

                                Yes. Changing the settings helped. But there is something I do not understand. I used ::GetModuleFileName( AfxGetInstanceHandle(),aExePath,sizeof(aExePath)); in my application using mfc. There I had no problem. But in the C++ program that I wrote to delete files form a folder, AfxGetInstanceHandle() was causing assersion failure. I could'nt figure out why. When I changed it to NULL >::GetModuleFileName( NULL,aExePath,sizeof(aExePath)); it worked fine. Why?

                                _


                                Fortitudine Vincimus!_

                                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