473,761 Members | 6,001 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Delete Folder + Files

Airslash
221 New Member
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 4938
Airslash
221 New Member
Managed to solve it now :)
Jan 7 '10 #2
Airslash
221 New Member
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
3088
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 working on windows 2000 Advanced server with IIS 5.
4
2238
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 instead of deleting the file, I can move it to a log somewhere? Private Sub Command0_Click() 'procedure to import all files in a directory and delete them. 'assumes they are all the correct format for an ASCII delimited import. Dim strfile As...
2
6853
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. Is there a way to make an uninstall so that it just completely wipes out my Program Files / MyApp folder no matter what?
9
8336
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) and two folders (Subfolder 1, Subfolder 2). .......I need to delete File1, File2, File3. Subfolder 1 contains FileA. .......Need to delete FileA. Subfolder 2 contains FileB, FileC, FileD.
3
84080
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
7561
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 the folder but I get the error message when the procedure attempts to delete the folder selected in the BrowseforFolderbyPath function. How can I get it to delete the folder that the user has selected in the Browse for Folder dialog?
4
2214
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 SpecialFolder.ApplicationData (C:\Documents and Settings\Username\Application Data) ? Or, is there some way to get the ms installer to do this for you? Is there some way to use Application.UserAppDataPath to ease finding
2
24111
by: Thomas Bauer | last post by:
Hello, Call DeleteFiles bgW_DeleteFilesProcess = new DeleteFiles(); bgW_DeleteFilesProcess.RunAsync( folder, 5, "*.txt", new RunWorkerCompletedEventHandler( RunWorkerCompleted_DeleteFiles_TXT ) ); I delete all files, which older than 5 days and I use a background thread.
0
2953
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 System.IO.DirectoryNotFoundException: Could not find a part of the path. Then it gives me a random file name from the UserProfile\Local Settings\Temporary Internet Files\Content.IE5\SomeRandomFolder. This is absolutley driving me nuts. So I am gonn give you a basic...
0
9531
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9345
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10115
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9775
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8780
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7332
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5229
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5373
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2752
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.