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
- void TForm1::CleanLocation(AnsiString Location)
- {
- // Prepare our Location to handle a wildcard search
- AnsiString FilePattern = Location + "\\*\0";
- // Create a HANDLE to the first file in the location.
- WIN32_FIND_DATA findFileData;
- HANDLE hFind = FindFirstFile(FilePattern.c_str(), &findFileData);
- // Check if we are able to load data from our location.
- if(hFind != INVALID_HANDLE_VALUE)
- {
- // There are files in the target Location.
- // First we call FindNextFile twice, to skip through
- // the .. files
- FindNextFile(hFind, &findFileData);
- FindNextFile(hFind, &findFileData);
- // Now keep removing files untill none are
- // found.
- while(FindNextFile(hFind, &findFileData))
- {
- // Check if we have a Directory
- if((findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
- {
- // We have a Directory, call ourselves again but this time with
- // a path to the current directory.
- AnsiString SubPath = Location + AnsiString("\\") + AnsiString(findFileData.cFileName);
- CleanLocation(SubPath);
- }
- // Delete the file
- // First create the SHFILEOPSTRUCT to set up the info
- // for Windows.
- SHFILEOPSTRUCT fileInfo;
- fileInfo.wFunc = FO_DELETE;
- fileInfo.pFrom = FilePattern.c_str();
- fileInfo.pTo = NULL;
- fileInfo.fFlags = FOF_SILENT | FOF_NOCONFIRMATION | FOF_FILESONLY | FOF_NOERRORUI;
- // perform the delete
- SHFileOperation(&fileInfo);
- }
- }
- // Close our handle
- FindClose(hFind);
- // Now Delete the folder itself.
- RemoveDir(Location);
- // Tell the user we cleaned it.
- listDebug->Items->Add(Location + " has been cleaned and removed.");
- }