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? -
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.");
-
}
-
2 4938
Managed to solve it now :)
Ok not solved....
I have currently this 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?
Sign in to post your reply or Sign up for a free account.
Similar topics |
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.
|
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...
|
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?
|
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.
|
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
| |
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?
|
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
|
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.
|
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...
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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...
| |