can not delete a file

This is a discussion on can not delete a file within the C Programming forums, part of the General Programming Boards category; Hello everyone, I am using the following program to delete all files in a specified directory. But when running, no ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    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
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What file are you trying to delete when it fails? You can't delete directories, so that may be a problem - perhaps you're trying to delete "." or ".."?

    --
    Mats

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    It's windows related (should go in the Windows programming board), but anyway.

    DeleteFile is relative to the current working directory, so you want to delete the file via it's full path.

    Such that
    Code:
     
    char fullPath[MAX_PATH];
    /* ... */
    /* in the loop */
    sprintf(fullPath, "c:\\temp\\non_empty_dir\\%s", FindFileData.cFileName);
    DeleteFile(fullPath);
    Or set the Working directory to that directory you are scanning (SetCurrentDirectory() or something)

    hth.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Zacs, yes, that's another good point - and although this code is written with Windows API, the same would apply if it was written with standard C functions - they file you delete is relative to current directory.

    There's still no code to check if the name is "." or ".." tho', which shouldn't really be deleted.

    --
    Mats

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by matsp
    There's still no code to check if the name is "." or ".." tho', which shouldn't really be deleted.
    However, that being said you should check if it's a file.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,823
    > rtn = DeleteFile(&(FindFileData.cFileName));
    So do you use & because you really like them, or just so you can see warnings in your code and ignore them?

    http://msdn2.microsoft.com/en-us/library/aa363915.aspx
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by zacs7 View Post
    However, that being said you should check if it's a file.

    You mean as opposed to a directory? Yes, good point. But even so, "." and ".." would need special treatment, as you still don't want to delete those (and I dont _THINK_ it's "ok" to create a file called ".." in the root of a FAT/NTFS file-system).

    ..
    Mats

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by matsp View Post
    You mean as opposed to a directory? Yes, good point. But even so, "." and ".." would need special treatment, as you still don't want to delete those (and I dont _THINK_ it's "ok" to create a file called ".." in the root of a FAT/NTFS file-system).

    ..
    Mats
    Apparently nor ext2/3

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 11:39 PM
  2. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 05:27 PM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 02:19 PM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 04:30 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21