Delete Folder + Files

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Airslash
    New Member
    • Nov 2007
    • 221

    Delete Folder + Files

    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?

    Code:
    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.");
    }
  • Airslash
    New Member
    • Nov 2007
    • 221

    #2
    Managed to solve it now :)

    Comment

    • Airslash
      New Member
      • Nov 2007
      • 221

      #3
      Ok not solved....

      I have currently this code:

      Code:
      void 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)
      	{
      		// Now keep removing files untill none are
      		// found.
      		while(FindNextFile(hFind, &findFileData))
      		{
      			// Check if we are not working with the . or .. folder.
      			if(findFileData.cFileName[0] == '.')
      			{
                  	// We are, so skip this entire step.
      				continue;
      			}
      			else
      			{
      				// 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
      				int funcResult = SHFileOperation(&fileInfo);
       
      				// Check if we deleted the file properly
      				if(0 != funcResult)
      				{
      					listDebug->Items->Add("Delete Error: " + AnsiString(funcResult));
      					listDebug->Items->Add(" -- FileName: " + AnsiString(findFileData.cFileName));
      				}
      			}
      		}
      	}
      	// Close the handle.
      	if(0 == FindClose(hFind))
      	{
      		// Failed to close the Handle on a specific search location
      		listDebug->Items->Add("Failed to close searchHandle");
          	listDebug->Items->Add("   -- Location: " + Location);
          	listDebug->Items->Add(GetLastError());
      	}
      	else
      	{
      		// Now Delete the folder itself.
      		RemoveDir(Location);
       
      		// Tell the user we cleaned it.
      		listDebug->Items->Add(Location + " has been cleaned and removed.");
      	}
      }
      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?

      Comment

      Working...