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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

remove directory

Status
Not open for further replies.

ceodav

Programmer
Feb 21, 2003
106
IT
hi,

i want to delete the content of a directory (files and subdiretory included) and the directory itself...
so i wrote the code below which uses a recursive function...

i can delete all the files but not the diretory because i get the error 32 : ERROR_SHARING_VIOLATION.

void DeleteDir(const char *directory)
{
CFileFind finder;
BOOL bWorking;

if (_chdir(directory)!=0)
{
//not exist...
return;
}

DeleteFile();


//look for subdir...
bWorking = finder.FindFile("*.*");


while (bWorking)
{
bWorking = finder.FindNextFile();


if (finder.IsDirectory())
{

if (!finder.IsDots())
{
DeleteDir(LPCSTR(finder.GetFilePath()));
}

}

}


if(!RemoveDirectory(directory))
{
char d[256];
sprintf(d,"error %d\n",GetLastError());
MessageBox(NULL, d, "Error", MB_ICONERROR);
}



}

void DeleteFile()
{
CFileFind finder;
BOOL bWorking;

bWorking = finder.FindFile("*.*");

while (bWorking)
{
bWorking = finder.FindNextFile();
DeleteFile(finder.GetFileName());
}

}


the function RemoveDir doesn't work.

but if i write the path like

RemoveDirectory("C:\\test");

it delete it.

how can i do?

thanks in advance

Davide
 
I believe the _chdir call is where the problem may lie. I think that because you are in the directory. Possibly try chdir("..") before you call RemoveDirectory

Matt
 
share violation means what you some applications are working inside that directory. So, if there is an application started in this directory, or some applications reads a file from this directory or some applications has set that directory or some subdirectory as current, you sould at the first close that application.

Ion Filipski
1c.bmp
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top