Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

directory delete

Status
Not open for further replies.

butthead

Programmer
Feb 24, 2002
545
US
Is there a function to delete a directory
including subs. Sort of like a complement to
the forcedirectory function. I will otherwise
have to give myself a refressher course on tree's.
I guess I could use a while loop to repeatedly
got thru an array but I see nothing but ugly in
that method. I have no problem deleting all files
in the directories.

I use the find functions to add directories to an
array then go thru the array and delete all files.
I could use a sort to arrange the items in descending
order starting with the subdirectories farthest from the base. I guess my question is which is the preffered
method.

Sort, Tree, whileLoop, other.

Thanks for all your help.

tomcruz.net
 
See the help for the SHFileOperation API and SHFILEOPSTRUCT structure
 
Somewhere I have seen a recursive operation for deleting subdirectories. I'll look around and see if I can find it.
James P. Cottingham

When a man sits with a pretty girl for an hour, it seems like a minute. But let him sit on a hot stove for a minute and it's longer than any hour. That's relativity.
[tab][tab]Albert Einstein explaining his Theory of Relativity to a group of journalists.
 
I'm using the function below to get the directories loaded into an array. I then use a for loop to delete the files in each directory. what I attempt to do now is use a similiar loop to delete each directory.
The problem Im having is that when I delete the directories there is always one directory left over.

int RecurseDir (char *dir, char *wild, int x)
{
// Loads the directoryarray

struct ffblk ffblk;
int done;
char buff [512];

strcpy( buff, dir );
strcat( buff, wild );

done = findfirst(buff, &ffblk,
FA_DIREC + FA_SYSTEM
+ FA_HIDDEN + FA_RDONLY + FA_ARCH);

while( !done )
{
if(strcmp(".", ffblk.ff_name) != 0 &&
strcmp("..", ffblk.ff_name) != 0)
{
if((ffblk.ff_attrib & FA_DIREC)
== FA_DIREC)
{
// process directory
strcpy(buff, dir);
strcat(buff, ffblk.ff_name);
strcpy (directoryarray [x], buff);
strcat(buff, "\\");

x++;

x = RecurseDir(buff, wild, x);
}
else
{
//
}
}

done = findnext(&ffblk);
}

return x;
}

in this function The last deletedirectory process always returns an error but the directory name posted to the richedit is correct. I,ve been pulling my hair out for a week and I know that the problem is so simple that it is completely invisible to me.

void ClearBackupDirectory (char *targetdir)
{
char *buff1 = new char [256];
int x = 0;
int y;
int z = 0;

// Delete the files in base of the backup directory.
EmptyDir (targetdir, x);

strcpy (buff1, targetdir);
strcat (buff1, "\\");

y = RecurseDir (buff1, "*.*", 0);

for (; z < y; z++)
{
EmptyDir (directoryarray [z], x);
}

for (z = 0; z < y; z++)
{
if (!RemoveDir (directoryarray [z]))
RichEdit1->Lines->Add (&quot;Error deleting directory&quot;);
RichEdit1->Lines->Add (directoryarray [z]);
RichEdit1->Lines->Add (&quot;&quot;);
}

delete buff1;
}


Any help would be appreciated.

tomcruz.net
 
As usual when I give up the answer appears.
Ive seen it happen so many times that I have
come to expect it. The answer came the day
after I posted this thread. I was looking for
the cause in the find function. I forgot that
my erase files function changed to the selected
directory first and then deleted the files.
Of course the last directory could not be deleted.
thats the directory I was currently in.

on another note, I was suspecting that I would
have to create a tree structure to properly
delete the directories from the subdirectories up.
I noticed that the recursedirectory function
loaded the array from the top down. It occurred
to me that if I just started from the last directory
listed in the array and proceeded to the first,
the directories would be deleted in the proper order.

chdir (homedir); // the program start dir
for (z = y - 1; z > -1; z--)
{
if (!RemoveDir (directoryarray [z]))
{
ProgressForm->RichEdit1->Lines->Add (&quot;Error deleting directory&quot;);
ProgressForm->RichEdit1->Lines->Add (directoryarray [z]);
ProgressForm->RichEdit1->Lines->Add (&quot;&quot;);
}
}

I should be finnished with a beta soon and I
would like some peer review. I would hope some
will give me some critic when I have it posted.

Thanks all of you for all the help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top