Need help w/ a simple recursive function
-
I wrote a function a while back to clean out a directory and all of its contents and tried re-writing the same one tonight but can't seem to get it right. Any chance someone can point out my mistake?
short ClearEntireDirectory(char *szDirectory) { // Function to work on a directory until everything is wiped out HANDLE hFile; WIN32_FIND_DATA wfd; if ( SetCurrentDirectory(szDirectory) == 0 ) return 1; hFile = FindFirstFile("*.*", &wfd); while ( FindNextFile(hFile, &wfd) != 0 ) { if ( wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY && strcmp(wfd.cFileName, "..") != 0 && strcmp(wfd.cFileName, ".") != 0 ) { if ( ClearEntireDirectory(wfd.cFileName) == 1 ) return 1; } DeleteFile(wfd.cFileName); } if ( SetCurrentDirectory("..") == 0 ) return 1; if ( RemoveDirectory(szDirectory) == 0 ) return 1; FindClose(hFile); return 0; }
I keep getting error 32 (ERROR_SHARING_VIOLATION) when I try to ultimatelly remove the directory. -
I wrote a function a while back to clean out a directory and all of its contents and tried re-writing the same one tonight but can't seem to get it right. Any chance someone can point out my mistake?
short ClearEntireDirectory(char *szDirectory) { // Function to work on a directory until everything is wiped out HANDLE hFile; WIN32_FIND_DATA wfd; if ( SetCurrentDirectory(szDirectory) == 0 ) return 1; hFile = FindFirstFile("*.*", &wfd); while ( FindNextFile(hFile, &wfd) != 0 ) { if ( wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY && strcmp(wfd.cFileName, "..") != 0 && strcmp(wfd.cFileName, ".") != 0 ) { if ( ClearEntireDirectory(wfd.cFileName) == 1 ) return 1; } DeleteFile(wfd.cFileName); } if ( SetCurrentDirectory("..") == 0 ) return 1; if ( RemoveDirectory(szDirectory) == 0 ) return 1; FindClose(hFile); return 0; }
I keep getting error 32 (ERROR_SHARING_VIOLATION) when I try to ultimatelly remove the directory.I'm such an idiot!!! It would help if FindClose cam before RemoveDirectory!!!!!
-
I wrote a function a while back to clean out a directory and all of its contents and tried re-writing the same one tonight but can't seem to get it right. Any chance someone can point out my mistake?
short ClearEntireDirectory(char *szDirectory) { // Function to work on a directory until everything is wiped out HANDLE hFile; WIN32_FIND_DATA wfd; if ( SetCurrentDirectory(szDirectory) == 0 ) return 1; hFile = FindFirstFile("*.*", &wfd); while ( FindNextFile(hFile, &wfd) != 0 ) { if ( wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY && strcmp(wfd.cFileName, "..") != 0 && strcmp(wfd.cFileName, ".") != 0 ) { if ( ClearEntireDirectory(wfd.cFileName) == 1 ) return 1; } DeleteFile(wfd.cFileName); } if ( SetCurrentDirectory("..") == 0 ) return 1; if ( RemoveDirectory(szDirectory) == 0 ) return 1; FindClose(hFile); return 0; }
I keep getting error 32 (ERROR_SHARING_VIOLATION) when I try to ultimatelly remove the directory.The calls to
SetCurrentDirectory()
are unnecessary. If the usage of this function is low, it probably makes no difference, but if it is an oft-used function, changing directories so many times is not efficient.
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen