473,385 Members | 1,325 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 4909
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.