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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. can not delete a file

can not delete a file

Scheduled Pinned Locked Moved C / C++ / MFC
comhelpquestion
30 Posts 8 Posters 1 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.
  • D Dominik Reichl

    Try DeleteFile(FindFileData.cFileName); instead of DeleteFile(&(FindFileData.cFileName)); :rose:


    Too many passwords to remember? Try KeePass Password Safe!

    G Offline
    G Offline
    George_George
    wrote on last edited by
    #5

    I have tried and the error is the same. Any further debug ideas? regards, George

    J 1 Reply Last reply
    0
    • G George_George

      Sure Jonathan, I have the privilege. I am admin and I can manually delete such files. I think there should be something wrong with my program, but through debugging for almost an hour, I still can not find the reason. Any ideas? regards, George

      J Offline
      J Offline
      Jonathan Darka
      wrote on last edited by
      #6

      I think the problem is that the first file you will 'find' is the current folder '.', you will get an access denied if you try to delete this, so you need to exclude both '.' and '..' from your wildcard search, something like this (obviously could be optimised, this is just to show an example): Also, you could make it recursive if you wanted to delete sub folders too.

      int remove_non_empty_directory(const char* path)
      {
      WIN32_FIND_DATA FindFileData;
      int rtn;

      HANDLE hFind = FindFirstFile(path, &FindFileData);
      if (hFind == INVALID\_HANDLE\_VALUE)
      {
      	FindClose(hFind);
      	return -1;
      }
      else
      {
              bool bDelete = true;
              if((\_tcsicmp(FindFileData.cFileName, "..") == 0) || (\_tcsicmp(FindFileData.cFileName, ".") == 0))
                  bDelete = false;
      
              if(bDelete) // delete 1st file
              {
      	rtn = DeleteFile((LPCSTR)FindFileData.cFileName);
                  // .... original error handling code
              }
      
              // List all the other files in the directory and delete all files
          while (FindNextFile(hFind, &FindFileData) != 0)
              {
                  bDelete = true;
                  if((\_tcsicmp(FindFileData.cFileName, "..") == 0) || (\_tcsicmp(FindFileData.cFileName, ".") == 0))
                      bDelete = false;
      
                  if(bDelete)
      	    rtn = DeleteFile((LPCSTR)FindFileData.cFileName);
      
      	FindClose(hFind);
      }
      
      return 0;
      

      }

      And change this code:

      DeleteFile(&(FindFileData.cFileName));

      to this:

      DeleteFile((LPCSTR)FindFileData.cFileName);

      regards, -- modified at 7:09 Monday 30th July, 2007


      Jonathan Wilkes Darka[Xanya.net]

      G 1 Reply Last reply
      0
      • G George_George

        I have tried and the error is the same. Any further debug ideas? regards, George

        J Offline
        J Offline
        Jonathan Darka
        wrote on last edited by
        #7

        George_George wrote:

        Any further debug ideas?

        See my last post regarding '.' and '..'


        Jonathan Wilkes Darka[Xanya.net]

        G 1 Reply Last reply
        0
        • G George_George

          Hello everyone, I am using the following program to delete all files in a specified directory. But when running, no files could be deleted, and the related error information is, failed with error 5 -- access denied. Anything wrong with the program? remove_non_empty_directory ("c:\\temp\\non_empty_dir\\*"); // ErrorExit implementation from MSDN // http://msdn2.microsoft.com/en-us/library/ms680582.aspx int remove_non_empty_directory (const char* path) { WIN32_FIND_DATA FindFileData; HANDLE hFind; int rtn; hFind = FindFirstFile(path, &FindFileData); if (hFind == INVALID_HANDLE_VALUE) { FindClose(hFind); return -1; } else { // delete 1st file rtn = DeleteFile(&(FindFileData.cFileName)); if (0 == rtn) { ErrorExit (NULL); } // List all the other files in the directory and delete all files while (FindNextFile(hFind, &FindFileData) != 0) { rtn = DeleteFile(&(FindFileData.cFileName)); } FindClose(hFind); } return 0; } thanks in advance, George

          S Offline
          S Offline
          SandipG
          wrote on last edited by
          #8

          I think it happens because FindFile Handle i.e. hFind is open. I have faced similar issue with CFileFind.Try to delete after you close the handle.

          G 1 Reply Last reply
          0
          • J Jonathan Darka

            I think the problem is that the first file you will 'find' is the current folder '.', you will get an access denied if you try to delete this, so you need to exclude both '.' and '..' from your wildcard search, something like this (obviously could be optimised, this is just to show an example): Also, you could make it recursive if you wanted to delete sub folders too.

            int remove_non_empty_directory(const char* path)
            {
            WIN32_FIND_DATA FindFileData;
            int rtn;

            HANDLE hFind = FindFirstFile(path, &FindFileData);
            if (hFind == INVALID\_HANDLE\_VALUE)
            {
            	FindClose(hFind);
            	return -1;
            }
            else
            {
                    bool bDelete = true;
                    if((\_tcsicmp(FindFileData.cFileName, "..") == 0) || (\_tcsicmp(FindFileData.cFileName, ".") == 0))
                        bDelete = false;
            
                    if(bDelete) // delete 1st file
                    {
            	rtn = DeleteFile((LPCSTR)FindFileData.cFileName);
                        // .... original error handling code
                    }
            
                    // List all the other files in the directory and delete all files
                while (FindNextFile(hFind, &FindFileData) != 0)
                    {
                        bDelete = true;
                        if((\_tcsicmp(FindFileData.cFileName, "..") == 0) || (\_tcsicmp(FindFileData.cFileName, ".") == 0))
                            bDelete = false;
            
                        if(bDelete)
            	    rtn = DeleteFile((LPCSTR)FindFileData.cFileName);
            
            	FindClose(hFind);
            }
            
            return 0;
            

            }

            And change this code:

            DeleteFile(&(FindFileData.cFileName));

            to this:

            DeleteFile((LPCSTR)FindFileData.cFileName);

            regards, -- modified at 7:09 Monday 30th July, 2007


            Jonathan Wilkes Darka[Xanya.net]

            G Offline
            G Offline
            George_George
            wrote on last edited by
            #9

            Thanks Jonathan, I have tried and slightly modified your program to make it compile. :-) Now the error condition is, error number 2 -- system can not find specified file. I am confused since the file actually exists. I need to using absolute path other than relative path to delete the file? int remove_non_empty_directory(const char* path) { WIN32_FIND_DATA FindFileData; int rtn; HANDLE hFind = FindFirstFile(path, &FindFileData); int bDelete = 0; if (hFind == INVALID_HANDLE_VALUE) { FindClose(hFind); return -1; } else { if((_tcscmp((LPCSTR)FindFileData.cFileName, "..") == 0) || (_tcscmp((LPCSTR)FindFileData.cFileName, ".") == 0)) { bDelete = 0; } if(bDelete) // delete 1st file { rtn = DeleteFile((LPCSTR)FindFileData.cFileName); // .... original error handling code } // List all the other files in the directory and delete all file while (FindNextFile(hFind, &FindFileData) != 0) { bDelete = 1; if((_tcscmp((LPCSTR)FindFileData.cFileName, "..") == 0) || (_tcscmp((LPCSTR)FindFileData.cFileName, ".") == 0)) { bDelete = 0; } if(bDelete) { rtn = DeleteFile((LPCSTR)FindFileData.cFileName); if (0 == rtn) { ErrorExit (NULL); } } } FindClose(hFind); } return 0; } regards, George

            J 2 Replies Last reply
            0
            • G George_George

              Thanks Jonathan, I have tried and slightly modified your program to make it compile. :-) Now the error condition is, error number 2 -- system can not find specified file. I am confused since the file actually exists. I need to using absolute path other than relative path to delete the file? int remove_non_empty_directory(const char* path) { WIN32_FIND_DATA FindFileData; int rtn; HANDLE hFind = FindFirstFile(path, &FindFileData); int bDelete = 0; if (hFind == INVALID_HANDLE_VALUE) { FindClose(hFind); return -1; } else { if((_tcscmp((LPCSTR)FindFileData.cFileName, "..") == 0) || (_tcscmp((LPCSTR)FindFileData.cFileName, ".") == 0)) { bDelete = 0; } if(bDelete) // delete 1st file { rtn = DeleteFile((LPCSTR)FindFileData.cFileName); // .... original error handling code } // List all the other files in the directory and delete all file while (FindNextFile(hFind, &FindFileData) != 0) { bDelete = 1; if((_tcscmp((LPCSTR)FindFileData.cFileName, "..") == 0) || (_tcscmp((LPCSTR)FindFileData.cFileName, ".") == 0)) { bDelete = 0; } if(bDelete) { rtn = DeleteFile((LPCSTR)FindFileData.cFileName); if (0 == rtn) { ErrorExit (NULL); } } } FindClose(hFind); } return 0; } regards, George

              J Offline
              J Offline
              Jonathan Darka
              wrote on last edited by
              #10

              which line is producing error 2, and what is the value of FindFileData.cFileName at that point ?


              Jonathan Wilkes Darka[Xanya.net]

              G 1 Reply Last reply
              0
              • G George_George

                Hello everyone, I am using the following program to delete all files in a specified directory. But when running, no files could be deleted, and the related error information is, failed with error 5 -- access denied. Anything wrong with the program? remove_non_empty_directory ("c:\\temp\\non_empty_dir\\*"); // ErrorExit implementation from MSDN // http://msdn2.microsoft.com/en-us/library/ms680582.aspx int remove_non_empty_directory (const char* path) { WIN32_FIND_DATA FindFileData; HANDLE hFind; int rtn; hFind = FindFirstFile(path, &FindFileData); if (hFind == INVALID_HANDLE_VALUE) { FindClose(hFind); return -1; } else { // delete 1st file rtn = DeleteFile(&(FindFileData.cFileName)); if (0 == rtn) { ErrorExit (NULL); } // List all the other files in the directory and delete all files while (FindNextFile(hFind, &FindFileData) != 0) { rtn = DeleteFile(&(FindFileData.cFileName)); } FindClose(hFind); } return 0; } thanks in advance, George

                D Offline
                D Offline
                Don Box
                wrote on last edited by
                #11

                Hello George_George, I'll tell u the exact problem u r having and how to rectify it. Your Code:------------------------------------------------- int remove_non_empty_directory (const char* path) { WIN32_FIND_DATA FindFileData; HANDLE hFind; int rtn; hFind = FindFirstFile(path, &FindFileData); if (hFind == INVALID_HANDLE_VALUE) { FindClose(hFind); return -1; } else { // delete 1st file rtn = DeleteFile(&(FindFileData.cFileName)); `` if (0 == rtn) { ErrorExit (NULL); } // List all the other files in the directory and delete all files while (FindNextFile(hFind, &FindFileData) != 0) { rtn = DeleteFile(&(FindFileData.cFileName)); `} FindClose(hFind); }` `` ---------------------------------------------------------- The problem lies in the highlighted lines. FindFileData.cFileName contains only the FILE NAME, NOT PATH. So the problem. Rectify it by replacing the highlighted lines by DeleteFile(path);

                Come online at:- jubinc@skype

                J G 2 Replies Last reply
                0
                • G George_George

                  Hello everyone, I am using the following program to delete all files in a specified directory. But when running, no files could be deleted, and the related error information is, failed with error 5 -- access denied. Anything wrong with the program? remove_non_empty_directory ("c:\\temp\\non_empty_dir\\*"); // ErrorExit implementation from MSDN // http://msdn2.microsoft.com/en-us/library/ms680582.aspx int remove_non_empty_directory (const char* path) { WIN32_FIND_DATA FindFileData; HANDLE hFind; int rtn; hFind = FindFirstFile(path, &FindFileData); if (hFind == INVALID_HANDLE_VALUE) { FindClose(hFind); return -1; } else { // delete 1st file rtn = DeleteFile(&(FindFileData.cFileName)); if (0 == rtn) { ErrorExit (NULL); } // List all the other files in the directory and delete all files while (FindNextFile(hFind, &FindFileData) != 0) { rtn = DeleteFile(&(FindFileData.cFileName)); } FindClose(hFind); } return 0; } thanks in advance, George

                  G Offline
                  G Offline
                  gvisgr8
                  wrote on last edited by
                  #12

                  Are you sure that those files are not read only. As once i wasalso facing the same problem but after checking out read only attribute. it was working fine...

                  Who am I? Do you know me....:omg:

                  G 1 Reply Last reply
                  0
                  • G George_George

                    Hello everyone, I am using the following program to delete all files in a specified directory. But when running, no files could be deleted, and the related error information is, failed with error 5 -- access denied. Anything wrong with the program? remove_non_empty_directory ("c:\\temp\\non_empty_dir\\*"); // ErrorExit implementation from MSDN // http://msdn2.microsoft.com/en-us/library/ms680582.aspx int remove_non_empty_directory (const char* path) { WIN32_FIND_DATA FindFileData; HANDLE hFind; int rtn; hFind = FindFirstFile(path, &FindFileData); if (hFind == INVALID_HANDLE_VALUE) { FindClose(hFind); return -1; } else { // delete 1st file rtn = DeleteFile(&(FindFileData.cFileName)); if (0 == rtn) { ErrorExit (NULL); } // List all the other files in the directory and delete all files while (FindNextFile(hFind, &FindFileData) != 0) { rtn = DeleteFile(&(FindFileData.cFileName)); } FindClose(hFind); } return 0; } thanks in advance, George

                    B Offline
                    B Offline
                    Bryster
                    wrote on last edited by
                    #13

                    Hi I was just looking at your problem and why not try someting completely different. You could create a batch file which deletes the files within a folder. 1. Create a new text file for writing to (say on your c:\) with the extension of ".BAT" 2. Write to that file the path of the directory you want to delete, fprintf(cfile,"del %s\*.*",pathname) 3. Close the file 4. Using the SPOOL command run the file. Just a quick solution to your problem. Bry.:-D

                    G 1 Reply Last reply
                    0
                    • G George_George

                      Thanks Jonathan, I have tried and slightly modified your program to make it compile. :-) Now the error condition is, error number 2 -- system can not find specified file. I am confused since the file actually exists. I need to using absolute path other than relative path to delete the file? int remove_non_empty_directory(const char* path) { WIN32_FIND_DATA FindFileData; int rtn; HANDLE hFind = FindFirstFile(path, &FindFileData); int bDelete = 0; if (hFind == INVALID_HANDLE_VALUE) { FindClose(hFind); return -1; } else { if((_tcscmp((LPCSTR)FindFileData.cFileName, "..") == 0) || (_tcscmp((LPCSTR)FindFileData.cFileName, ".") == 0)) { bDelete = 0; } if(bDelete) // delete 1st file { rtn = DeleteFile((LPCSTR)FindFileData.cFileName); // .... original error handling code } // List all the other files in the directory and delete all file while (FindNextFile(hFind, &FindFileData) != 0) { bDelete = 1; if((_tcscmp((LPCSTR)FindFileData.cFileName, "..") == 0) || (_tcscmp((LPCSTR)FindFileData.cFileName, ".") == 0)) { bDelete = 0; } if(bDelete) { rtn = DeleteFile((LPCSTR)FindFileData.cFileName); if (0 == rtn) { ErrorExit (NULL); } } } FindClose(hFind); } return 0; } regards, George

                      J Offline
                      J Offline
                      Jonathan Darka
                      wrote on last edited by
                      #14

                      George_George wrote:

                      I have tried and slightly modified your program to make it compile.

                      :-)

                      George_George wrote:

                      I need to using absolute path other than relative path to delete the file?

                      Yes, in this case you should. regards,


                      Jonathan Wilkes Darka[Xanya.net]

                      G 1 Reply Last reply
                      0
                      • D Don Box

                        Hello George_George, I'll tell u the exact problem u r having and how to rectify it. Your Code:------------------------------------------------- int remove_non_empty_directory (const char* path) { WIN32_FIND_DATA FindFileData; HANDLE hFind; int rtn; hFind = FindFirstFile(path, &FindFileData); if (hFind == INVALID_HANDLE_VALUE) { FindClose(hFind); return -1; } else { // delete 1st file rtn = DeleteFile(&(FindFileData.cFileName)); `` if (0 == rtn) { ErrorExit (NULL); } // List all the other files in the directory and delete all files while (FindNextFile(hFind, &FindFileData) != 0) { rtn = DeleteFile(&(FindFileData.cFileName)); `} FindClose(hFind); }` `` ---------------------------------------------------------- The problem lies in the highlighted lines. FindFileData.cFileName contains only the FILE NAME, NOT PATH. So the problem. Rectify it by replacing the highlighted lines by DeleteFile(path);

                        Come online at:- jubinc@skype

                        J Offline
                        J Offline
                        Jonathan Darka
                        wrote on last edited by
                        #15

                        Don Box wrote:

                        Rectify it by replacing the highlighted lines by DeleteFile(path);

                        That won't work as he is most likely passing in just a folder name, or a folder name with a wildcard attached. He should take the original path (excluding wildcard) and then add the filename from FindFileData.cFileName and then Delete that. regards,


                        Jonathan Wilkes Darka[Xanya.net]

                        G 1 Reply Last reply
                        0
                        • J Jonathan Darka

                          which line is producing error 2, and what is the value of FindFileData.cFileName at that point ?


                          Jonathan Wilkes Darka[Xanya.net]

                          G Offline
                          G Offline
                          George_George
                          wrote on last edited by
                          #16

                          Relative file name. regards, George

                          1 Reply Last reply
                          0
                          • J Jonathan Darka

                            George_George wrote:

                            I have tried and slightly modified your program to make it compile.

                            :-)

                            George_George wrote:

                            I need to using absolute path other than relative path to delete the file?

                            Yes, in this case you should. regards,


                            Jonathan Wilkes Darka[Xanya.net]

                            G Offline
                            G Offline
                            George_George
                            wrote on last edited by
                            #17

                            Thanks Jonathan, I have tried that if I am using absolute path, it works! regards, George

                            1 Reply Last reply
                            0
                            • J Jonathan Darka

                              George_George wrote:

                              Any further debug ideas?

                              See my last post regarding '.' and '..'


                              Jonathan Wilkes Darka[Xanya.net]

                              G Offline
                              G Offline
                              George_George
                              wrote on last edited by
                              #18

                              Yes, they are the key points. regards, George

                              1 Reply Last reply
                              0
                              • S SandipG

                                I think it happens because FindFile Handle i.e. hFind is open. I have faced similar issue with CFileFind.Try to delete after you close the handle.

                                G Offline
                                G Offline
                                George_George
                                wrote on last edited by
                                #19

                                Hi SandipG, I have tried that it does not matter. regards, George

                                1 Reply Last reply
                                0
                                • D Don Box

                                  Hello George_George, I'll tell u the exact problem u r having and how to rectify it. Your Code:------------------------------------------------- int remove_non_empty_directory (const char* path) { WIN32_FIND_DATA FindFileData; HANDLE hFind; int rtn; hFind = FindFirstFile(path, &FindFileData); if (hFind == INVALID_HANDLE_VALUE) { FindClose(hFind); return -1; } else { // delete 1st file rtn = DeleteFile(&(FindFileData.cFileName)); `` if (0 == rtn) { ErrorExit (NULL); } // List all the other files in the directory and delete all files while (FindNextFile(hFind, &FindFileData) != 0) { rtn = DeleteFile(&(FindFileData.cFileName)); `} FindClose(hFind); }` `` ---------------------------------------------------------- The problem lies in the highlighted lines. FindFileData.cFileName contains only the FILE NAME, NOT PATH. So the problem. Rectify it by replacing the highlighted lines by DeleteFile(path);

                                  Come online at:- jubinc@skype

                                  G Offline
                                  G Offline
                                  George_George
                                  wrote on last edited by
                                  #20

                                  Thanks Don! It works! Cool! regards, George

                                  1 Reply Last reply
                                  0
                                  • J Jonathan Darka

                                    Don Box wrote:

                                    Rectify it by replacing the highlighted lines by DeleteFile(path);

                                    That won't work as he is most likely passing in just a folder name, or a folder name with a wildcard attached. He should take the original path (excluding wildcard) and then add the filename from FindFileData.cFileName and then Delete that. regards,


                                    Jonathan Wilkes Darka[Xanya.net]

                                    G Offline
                                    G Offline
                                    George_George
                                    wrote on last edited by
                                    #21

                                    You are always right, Jonathan! regards, George

                                    J 1 Reply Last reply
                                    0
                                    • G gvisgr8

                                      Are you sure that those files are not read only. As once i wasalso facing the same problem but after checking out read only attribute. it was working fine...

                                      Who am I? Do you know me....:omg:

                                      G Offline
                                      G Offline
                                      George_George
                                      wrote on last edited by
                                      #22

                                      Sure, gvisgr8. I have enough privilege. regards, George

                                      D 1 Reply Last reply
                                      0
                                      • B Bryster

                                        Hi I was just looking at your problem and why not try someting completely different. You could create a batch file which deletes the files within a folder. 1. Create a new text file for writing to (say on your c:\) with the extension of ".BAT" 2. Write to that file the path of the directory you want to delete, fprintf(cfile,"del %s\*.*",pathname) 3. Close the file 4. Using the SPOOL command run the file. Just a quick solution to your problem. Bry.:-D

                                        G Offline
                                        G Offline
                                        George_George
                                        wrote on last edited by
                                        #23

                                        Thanks Bryster, I need to implement through Windows API to be more efficient. :-) regards, George

                                        1 Reply Last reply
                                        0
                                        • G George_George

                                          You are always right, Jonathan! regards, George

                                          J Offline
                                          J Offline
                                          Jonathan Darka
                                          wrote on last edited by
                                          #24

                                          George_George wrote:

                                          You are always right, Jonathan!

                                          Only most of the time ;-) best wishes,


                                          Jonathan Wilkes Darka[Xanya.net]

                                          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