//Programm zum rekursiven loeschen von leeren Verzeichnisse // siehe auch http://www.charlescurley.com/fn.html #include <windows.h> #include <stdio.h> #include <string.h> char *generic = "*"; BOOL recurse = 1; // shall we recurse? Default is yes. char szPath[16000]; //------------------------------------------------------------------------------------- /* concatenate path suffix to path prefix */ __inline char *dircat(register char *pfx, register char *sfx) { extern char *strcat(); register char *p; register int nb, npfx; nb = (npfx = strlen(pfx)) + 1 + strlen(sfx) + 1; if(( p = malloc(nb)) == (char *)0) exit(1); strcpy(p, pfx); if(npfx != 0 && pfx[npfx-1] != '\\') lstrcat(p, "\\"); return lstrcat(p, sfx); } //------------------------------------------------------------------------------------- /* search the directory specified by dirname */ int find(char *name,char *spec) { register char *globname, *newname; WIN32_FIND_DATA FileData; HANDLE hFile; if (recurse) { globname = dircat(name, generic); if((hFile = FindFirstFile(globname, &FileData)) != INVALID_HANDLE_VALUE) { do { if (!(!strcmp (FileData.cFileName, ".") || !strcmp (FileData.cFileName, ".."))) { if(FileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { newname = dircat(name, FileData.cFileName); find(newname, spec); //hier versuchen leere Verzeichnisse zu löschen if(RemoveDirectory(newname)){ printf("leeres Verzeichnis: %s geloescht\n", newname); } free(newname); } } } while (FindNextFile(hFile, &FileData) != 0); } FindClose( hFile ); } return 0; } //------------------------------------------------------------------------------------- int main(int argc,char *argv[]) { char *spec, *path; printf("Programm zum rekursiven loeschen von leeren Verzeichnisse : %s\n",argv[0]); if((argc == 1) || (argc > 2) ) { printf("Nutzung von : %s \n",argv[0]); printf("Beispiel : %s h:\\meinVerzeichnis\\\n",argv[0]); printf(" oder : %s \\\\meinUNCServer\\meinVerzeichnis\\\n",argv[0]); return 1; } path = argv[1]; spec = generic; find(path, spec); return 0; }