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!

_rmdir keeps failing to delete folder

Status
Not open for further replies.

biot023

Programmer
Nov 8, 2001
403
GB
Hallo. I have a folder that my code has just cleared of all files, but I still cannot get it to delete.
Eg. This code would produce the message "Delete failed.":

if(_rmdir("C:\\test_del")){
ShowMessage("Delete failed.");
}

Can anyone think why this might be?
Like I say, there are no files or other folders in the folder I'm trying to zap.

Any & all help gratefully received.
Cheers,
Douglas JL Common sense is what tells you the world is flat.
 

the RemoveDir function might work.

Are you sure there is no read-only attrib on the subdir ?
 
Yep - there's absolutely no read-only or anything else I can spot.
In fact, I'm testing on directories that I only just created for the purpose.
-- Just tried RemoveDir & got the same problem.
Cheers for the input, though!

Thanks,
Douglas JL
Common sense is what tells you the world is flat.
 
Are you using winnt or win2k ?
I have noticed that you cannot remove a directory if explorer still has the directory open. For some reason explorer keeps the directory locked.
I can image that you are testing your program while explorer is open to see the result.
 
Are there hidden files in the directory?
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.
 
No - there are no hidden files, and I just tested it w/out explorer open (I'm using NT) - I'm afraid it still won't delete.
If the worst comes to the worst, I could always create a temporary batch file to handle deletes & run it with ShellExecute...

Cheers,
Douglas JL

Common sense is what tells you the world is flat.
 
Can you post your code ?
Maybe we can find the problem then.
 
Yes, post your code. Also, if you use a shell program, why not just use deltree or rmdir instead aof creating a batch file? 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.
 
Well, it's not pretty, but here's the code (hennep may well recognise some of the nifty footwork done with the ffblk to find directories...)

list<string> full_dir;
list<string> full_file;
ffblk ffblk;
Screen->Cursor=crHourGlass;

string olddir=listLocalDir->Directory.c_str();
string targetdir=(olddir[olddir.length()-1]=='\\')?olddir:eek:lddir+&quot;\\&quot;;
targetdir+=listLocalDir->Items->Strings[listLocalDir->ItemIndex].c_str();
if(!DirectoryExists(targetdir.c_str())){
targetdir=olddir;
}

// get all local directories and files
full_dir.push_back(targetdir);
list<string>::iterator iter=full_dir.begin();
while(iter!=full_dir.end()){
string& p_i=*iter;
string filter=p_i+&quot;\\*&quot;;
int result=findfirst(filter.c_str(),&ffblk,FA_DIREC);
while(!result){
if((ffblk.ff_attrib & FA_DIREC)==FA_DIREC){
string dir=ffblk.ff_name;
if(dir!=&quot;.&quot; && dir!=&quot;..&quot;){
string fulldir=p_i+&quot;\\&quot;;
fulldir+=dir;
full_dir.push_back(fulldir);
}
}
result=findnext(&ffblk);
}
filter=p_i+&quot;\\*.*&quot;;
result=findfirst(filter.c_str(),&ffblk,FA_ARCH);
while(!result){
string fullfile=p_i+&quot;\\&quot;;
fullfile+=ffblk.ff_name;
full_file.push_back(fullfile);
result=findnext(&ffblk);
}
iter++;
}

//delete all files
iter=full_file.begin();
while(iter!=full_file.end()){
string& p_i=*iter;
if(remove(p_i.c_str())){
string msg=&quot;Failed to delete file \&quot;&quot;;
msg+=p_i+&quot;\&quot;&quot;;
ShowMessage(msg.c_str());
Screen->Cursor=crDefault;
return;
}
iter++;
}

//delete all folders
full_dir.reverse();
iter=full_dir.begin();
while(iter!=full_dir.end()){
string& p_i=*iter;
if(!RemoveDir(p_i.c_str())){
string msg=&quot;Failed to delete directory \&quot;&quot;;
msg+=p_i+&quot;\&quot;&quot;;
ShowMessage(msg.c_str());
Screen->Cursor=crDefault;
return;
}
iter++;
}
listLocalDir->Update();
listLocalFiles->Update();
Screen->Cursor=crDefault;

It is the last section that deletes the folder(s), and that always fails in NT (and sometimes 98).
If this is too ugly a clump of code, I could try to refine it to essentials, but I wanted to give an idea of the processes that are taking place.
& thanks alot for your ongoing help!

Cheers,
Douglas JL

Common sense is what tells you the world is flat.
 
This look similar to what I've done in the past. Just for kicks, do a GetCurrentDir after deleting the files and see where you are. If you are in the directory to be deleted, do a SetCurrentDir to the direcory above and then try to delete 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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top