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

_rmdir: I use absolute path and full control directories : (

Status
Not open for further replies.

SuperTeone

Programmer
Jun 25, 2001
6
IT
Thanks for your tips, but I use absolute paths and full control directories (to everyone), so... the problem must be something different :(

And, for Jerry, voila' the code:


void RemoveDirectory(CString path)
// path contains the absoulute path of directory

int morefiles=1,err;
char strpath[128];
CString str,rmstr;
HANDLE hfind;
WIN32_FIND_DATA datafind;
str=path+"/*";
hfind=FindFirstFile(LPCTSTR(str),&datafind);
//start to find files in the directory
while(hfind!=INVALID_HANDLE_VALUE && morefiles!=0)
{
if (strcmp(datafind.cFileName,".")!=0 &&
strcmp (datafind.cFileName,"..")!=0)
{
// Remove all files in the directory exept . and ..
rmstr=path+"/"+datafind.cFileName;
err=remove(rmstr);
}
morefiles=FindNextFile(hfind,&datafind);
}
strcpy(strpath,LPCTSTR(path));
err=_rmdir(strpath); // DOESN'T WORK!!!!
 
Look what I found in a MSDN sample. It deletes a directory if it is empty with RemoveDirectory API function.

So try to call this function after you have deleted all the files in the directory. Of course if it works, I think you will have to mix your code with this one to optimize performance.

BOOL DeleteHashDirectory(VOID) {
TCHAR szSigDir[PATH_SIZE];
TCHAR szFiles[PATH_SIZE];
WIN32_FIND_DATA FindFileData;
HANDLE hFindFile;

GetSigDir(szSigDir);

lstrcpy(szFiles, szSigDir);
lstrcat(szFiles, TEXT("\\*"));

// Does this directory exist?

if (GetFileAttributes(szSigDir) == 0xFFFFFFFF) {
return TRUE;
}

hFindFile = FindFirstFile(szFiles, &FindFileData);

FindNextFile(hFindFile, &FindFileData);

if (!FindNextFile(hFindFile, &FindFileData)) {
FindClose(hFindFile);
SetFileAttributes(szSigDir, FILE_ATTRIBUTE_NORMAL);
if (!RemoveDirectory(szSigDir)) {
ErrorMsg(TEXT("DeleteHashDirectory: RemoveDirectory failed."));
return FALSE;
}
}
else
FindClose(hFindFile);

return TRUE;
}

Hope this helps,
s-)

Blessed is he who in the name of justice and good will, shepards the week through the valley of darknees...
 
Does anyone know why I would receive this error from the above code (last message)?

C:\Windows\Desktop\RedixCPPExample\Text1.cpp(3) : error C2146: syntax error : missing ';' before identifier 'DeleteHashDirectory'

 
i can assume 2 things... the previous line before the 'DeleteHashDirectory' is missing a ; or you have an include or a macro before the 'DeleteHashDirectory' and that is causing it.

matt
 
Since this appears to be a user-defined function there needs to be a function declaration before it is defined. This is can either be done with a header file or declaring it in the same file as the code is in.

Try adding this to the above code:

BOOL DeleteHashDirectory(VOID);
.
.
.//The same code as above pasted in

Most likely the reason for the error was that you just took the code from this post and didn't get the header files and such that would've been included if you got the code from the MSDN example.



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top