By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,320 Members | 2,212 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,320 IT Pros & Developers. It's quick & easy.

Delete Folder + Files

Airslash
100+
P: 221
Hi,

I'm currently working on a function to delete a folder and its files + subfolders.
The function currently works for the target folder, but refuses to delete the subfolders and files in the subfolder.

When I debugged the code it returned value 1024 n the SHFileOperation function. I have no idea what it means.

anyone could tell me why the function is not deleting it's subdirectory?

Expand|Select|Wrap|Line Numbers
  1. void TForm1::CleanLocation(AnsiString Location)
  2. {
  3.     // Prepare our Location to handle a wildcard search
  4.     AnsiString FilePattern = Location + "\\*\0";
  5.     // Create a HANDLE to the first file in the location.
  6.     WIN32_FIND_DATA findFileData;
  7.     HANDLE hFind = FindFirstFile(FilePattern.c_str(), &findFileData);
  8.  
  9.     // Check if we are able to load data from our location.
  10.     if(hFind  != INVALID_HANDLE_VALUE)
  11.     {
  12.         // There are files in the target Location.
  13.         // First we call FindNextFile twice, to skip through
  14.         // the .. files
  15.         FindNextFile(hFind, &findFileData);
  16.         FindNextFile(hFind, &findFileData);
  17.  
  18.         // Now keep removing files untill none are
  19.         // found.
  20.         while(FindNextFile(hFind, &findFileData))
  21.         {
  22.             // Check if we have a Directory
  23.             if((findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
  24.             {
  25.                 // We have a Directory, call ourselves again but this time with
  26.                 // a path to the current directory.
  27.                 AnsiString SubPath = Location + AnsiString("\\") + AnsiString(findFileData.cFileName);
  28.                 CleanLocation(SubPath);
  29.             }
  30.  
  31.             // Delete the file
  32.             // First create the SHFILEOPSTRUCT to set up the info
  33.             // for Windows.
  34.             SHFILEOPSTRUCT fileInfo;
  35.             fileInfo.wFunc = FO_DELETE;
  36.             fileInfo.pFrom = FilePattern.c_str();
  37.             fileInfo.pTo = NULL;
  38.             fileInfo.fFlags = FOF_SILENT | FOF_NOCONFIRMATION | FOF_FILESONLY | FOF_NOERRORUI;
  39.  
  40.             // perform the delete
  41.             SHFileOperation(&fileInfo);
  42.         }
  43.     }
  44.  
  45.     // Close our handle
  46.     FindClose(hFind);
  47.  
  48.     // Now Delete the folder itself.
  49.     RemoveDir(Location);
  50.  
  51.     // Tell the user we cleaned it.
  52.     listDebug->Items->Add(Location + " has been cleaned and removed.");
  53. }
  54.  
Jan 7 '10 #1
Share this Question
Share on Google+
2 Replies


Airslash
100+
P: 221
Managed to solve it now :)
Jan 7 '10 #2

Airslash
100+
P: 221
Ok not solved....

I have currently this code:

Expand|Select|Wrap|Line Numbers
  1. void CleanLocation(AnsiString Location)
  2. {
  3.     // Prepare our Location to handle a wildcard search
  4.     AnsiString FilePattern = Location + "\\*\0";
  5.     // Create a HANDLE to the first file in the location.
  6.     WIN32_FIND_DATA findFileData;
  7.     HANDLE hFind = FindFirstFile(FilePattern.c_str(), &findFileData);
  8.  
  9.     // Check if we are able to load data from our location.
  10.     if(hFind  != INVALID_HANDLE_VALUE)
  11.     {
  12.         // Now keep removing files untill none are
  13.         // found.
  14.         while(FindNextFile(hFind, &findFileData))
  15.         {
  16.             // Check if we are not working with the . or .. folder.
  17.             if(findFileData.cFileName[0] == '.')
  18.             {
  19.                 // We are, so skip this entire step.
  20.                 continue;
  21.             }
  22.             else
  23.             {
  24.                 // Check if we have a Directory
  25.                 if((findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
  26.                 {
  27.                     // We have a Directory, call ourselves again but this time with
  28.                     // a path to the current directory.
  29.                     AnsiString SubPath = Location + AnsiString("\\") + AnsiString(findFileData.cFileName);
  30.                     CleanLocation(SubPath);
  31.                 }
  32.  
  33.                 // Delete the file
  34.                 // First create the SHFILEOPSTRUCT to set up the info
  35.                 // for Windows.
  36.                 SHFILEOPSTRUCT fileInfo;
  37.                 fileInfo.wFunc = FO_DELETE;
  38.                 fileInfo.pFrom = FilePattern.c_str();
  39.                 fileInfo.pTo = NULL;
  40.                 fileInfo.fFlags = FOF_SILENT | FOF_NOCONFIRMATION | FOF_FILESONLY | FOF_NOERRORUI;
  41.  
  42.                 // perform the delete
  43.                 int funcResult = SHFileOperation(&fileInfo);
  44.  
  45.                 // Check if we deleted the file properly
  46.                 if(0 != funcResult)
  47.                 {
  48.                     listDebug->Items->Add("Delete Error: " + AnsiString(funcResult));
  49.                     listDebug->Items->Add(" -- FileName: " + AnsiString(findFileData.cFileName));
  50.                 }
  51.             }
  52.         }
  53.     }
  54.     // Close the handle.
  55.     if(0 == FindClose(hFind))
  56.     {
  57.         // Failed to close the Handle on a specific search location
  58.         listDebug->Items->Add("Failed to close searchHandle");
  59.         listDebug->Items->Add("   -- Location: " + Location);
  60.         listDebug->Items->Add(GetLastError());
  61.     }
  62.     else
  63.     {
  64.         // Now Delete the folder itself.
  65.         RemoveDir(Location);
  66.  
  67.         // Tell the user we cleaned it.
  68.         listDebug->Items->Add(Location + " has been cleaned and removed.");
  69.     }
  70. }
  71.  
And when I ran this code with Location = C:\WriteDaemon
it ended up deleting ALL files on the C-root and the WriteDaemon folders.

Could someone point out if my logic is wrong somewhere in the code?
Jan 8 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.