CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    can not delete a file

    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?

    Code:
    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

  2. #2
    Join Date
    Mar 2004
    Location
    (Upper-) Austria
    Posts
    2,899

    Re: can not delete a file

    As the error suggests: Do you have enough privilegues to delete the file/s?
    I am not offering technical guidiance via email or IM
    Come on share your photo with us! CG members photo album!
    Use the Code Tags!

  3. #3
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Re: can not delete a file

    Sure NoHero,


    Quote Originally Posted by NoHero
    As the error suggests: Do you have enough privilegues to delete the file/s?
    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

  4. #4
    Join Date
    Mar 2004
    Location
    (Upper-) Austria
    Posts
    2,899

    Re: can not delete a file

    Code:
    // delete 1st file
    rtn = DeleteFile(&(FindFileData.cFileName));
    You are already passing a pointer to the first element of an array to DeleteFile. No reason to take the pointer to the pointer...

    Try:

    Code:
    rtn = DeleteFile(FindFileData.cFileName);
    I am not offering technical guidiance via email or IM
    Come on share your photo with us! CG members photo album!
    Use the Code Tags!

  5. #5
    Join Date
    Aug 1999
    Location
    <Classified>
    Posts
    6,882

    Re: can not delete a file

    Adding to NoHero, Are you sure that your application has no open handles to those files ?
    Regards,
    Ramkrishna Pawar

  6. #6
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Re: can not delete a file

    You are right, NoHero!


    Quote Originally Posted by NoHero
    Code:
    // delete 1st file
    rtn = DeleteFile(&(FindFileData.cFileName));
    You are already passing a pointer to the first element of an array to DeleteFile. No reason to take the pointer to the pointer...

    Try:

    Code:
    rtn = DeleteFile(FindFileData.cFileName);

    regards,
    George

  7. #7
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Re: can not delete a file

    Thanks Krishnaa, no other applications have any handles to the files.

    Quote Originally Posted by Krishnaa
    Adding to NoHero, Are you sure that your application has no open handles to those files ?

    regards,
    George

  8. #8
    Join Date
    Mar 2004
    Location
    (Upper-) Austria
    Posts
    2,899

    Re: can not delete a file

    Quote Originally Posted by George2
    You are right, NoHero!
    You are welcome
    But I have to admit, my eyes are getting sloppy
    I am not offering technical guidiance via email or IM
    Come on share your photo with us! CG members photo album!
    Use the Code Tags!

  9. #9
    Join Date
    May 2005
    Posts
    4,954

    Re: can not delete a file

    Just aside note, it better using ::SHFileOperation() since DeleteFile() will fail in deleting read only files.

    Cheers
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  10. #10
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Re: can not delete a file

    Thanks golanshahar,


    Quote Originally Posted by golanshahar
    Just aside note, it better using ::SHFileOperation() since DeleteFile() will fail in deleting read only files.

    Cheers
    SHFileOperation can not be used for Windows CE platform.


    regards,
    George

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