472,972 Members | 2,052 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,972 software developers and data experts.

Delete Folder + Files

Airslash
221 100+
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
2 4875
Airslash
221 100+
Managed to solve it now :)
Jan 7 '10 #2
Airslash
221 100+
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

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

Similar topics

2
by: tvr | last post by:
Hi, I have a strange problem. I am not able to delete a published folder. Any ideas?? The files in the folder are not open, but it still says that "Please make sure files ...." I am...
4
by: Mark Reed | last post by:
Hi all, I have the following code which imports the contents of all files within a set folder which works excellently. Once it has imported from each file, it deletes the file. Is there a way that...
2
by: Keith Smith | last post by:
I have noticed that when I uninstall my app it doesn't delete the Program Files / MyApp folder if there are "new" files in it that weren't there before. It deletes all files except the "new" ones....
9
by: Paul | last post by:
I'm trying to make get my app to delete all the files in a specified folder and all the files within the folders of the specified folder. e.g. Folder 1 contains three files (File1, File2, File3)...
3
by: ad | last post by:
Hi, How can I delete all files in a folder by C# for example, I want to delete all files under c:\Test
24
by: biganthony via AccessMonster.com | last post by:
Hi, I have the following code to select a folder and then delete it. I keep getting a Path/File error on the line that deletes the actual folder. The line before that line deletes the files in...
4
by: - HAL9000 | last post by:
When un-installing an application... Is it normal practice to write a special program that erases all the files and folders for all the users of an application that reads and writes to...
2
by: Thomas Bauer | last post by:
Hello, Call DeleteFiles bgW_DeleteFilesProcess = new DeleteFiles(); bgW_DeleteFilesProcess.RunAsync( folder, 5, "*.txt", new RunWorkerCompletedEventHandler( RunWorkerCompleted_DeleteFiles_TXT )...
0
by: wolfsbane | last post by:
Alright, here it is I am trying to write a win32 app in VB 2005 to clean up user's profiles. everything works correctly except for the Delete("directory", True) statement. I get a...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
3
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.