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.
  • 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
    #17

    Relative file name. 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
                  • 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
                    David Crow
                    wrote on last edited by
                    #25

                    George_George wrote:

                    I am using the following program to delete all files in a specified directory.

                    Why not just use SHFileOperation()?


                    "A good athlete is the result of a good and worthy opponent." - David Crow

                    "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                    G 1 Reply Last reply
                    0
                    • G George_George

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

                      D Offline
                      D Offline
                      David Crow
                      wrote on last edited by
                      #26

                      George_George wrote:

                      Sure, gvisgr8.

                      I'm not gvisgr8.

                      George_George wrote:

                      I have enough privilege.

                      I was not concerned with privileges. I was simply suggesting replacing dozens of lines of code with a single one.


                      "A good athlete is the result of a good and worthy opponent." - David Crow

                      "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                      G 1 Reply Last reply
                      0
                      • D David Crow

                        George_George wrote:

                        I am using the following program to delete all files in a specified directory.

                        Why not just use SHFileOperation()?


                        "A good athlete is the result of a good and worthy opponent." - David Crow

                        "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

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

                        Because I suspect on Windows Mobile it is not supported -- I need to write program for both Windows desktop and Window CE. regards, George

                        1 Reply Last reply
                        0
                        • D David Crow

                          George_George wrote:

                          Sure, gvisgr8.

                          I'm not gvisgr8.

                          George_George wrote:

                          I have enough privilege.

                          I was not concerned with privileges. I was simply suggesting replacing dozens of lines of code with a single one.


                          "A good athlete is the result of a good and worthy opponent." - David Crow

                          "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

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

                          How?

                          DavidCrow wrote:

                          I was simply suggesting replacing dozens of lines of code with a single one.

                          regards, George

                          D 1 Reply Last reply
                          0
                          • G George_George

                            How?

                            DavidCrow wrote:

                            I was simply suggesting replacing dozens of lines of code with a single one.

                            regards, George

                            D Offline
                            D Offline
                            David Crow
                            wrote on last edited by
                            #29

                            George_George wrote:

                            How?

                            By using SHFileOperation() instead.


                            "A good athlete is the result of a good and worthy opponent." - David Crow

                            "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                            G 1 Reply Last reply
                            0
                            • D David Crow

                              George_George wrote:

                              How?

                              By using SHFileOperation() instead.


                              "A good athlete is the result of a good and worthy opponent." - David Crow

                              "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

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

                              Thanks DavidCrow, This function is convenient to use, but I am not sure whether we can use it on Windows CE. regards, George

                              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