// ddir1.cpp: Remove subdirectory tree
#include <stdio.h>
#include <stdlib.h>
#include <io.h>
#include <string.h>
#include <sys/stat.h>
#include <dirent.h>
#include <dir.h>
#include <iostream.h>
#include <fstream.h>
#include <strstream.h>
#include <except.h>
// Put response file in root directory
char response_file[L_tmpnam+1] = "\\";
// (Change this to "/" for UNIX)
main(int argc, char **argv)
{
char *old_path = getcwd(NULL,MAXDIR);
void rd(char *);
// Initialize response file for DOS "del" command
tmpnam(response_file+1);
ofstream(response_file) << "Y\n";
// Delete the directories
while (--argc)
{
try
{
rd(*++argv);
}
// Catch exceptions
catch(const char *x)
{
cerr << x << endl;
}
}
// Clean-up
remove(response_file);
chdir(old_path);
return 0;
}
void rd(char* dir)
{
// Log onto the directory that is to be deleted
cout << strlwr(dir) << endl;
if (chdir(dir))
throw "Invalid directory";
// Delete all normal files via 0S shell
const size_t SH_CMD_LEN = 14;
char sh_cmd[L_tmpnam+SH_CMD_LEN+1];
ostrstream(sh_cmd,sizeof sh_cmd) << "del *.* <"
<< response_file
<< " >null"
<< ends;
system(sh_cmd);
// Delete any remaining directory entries
DIR *dirp;
struct dirent *entry;
if ((dirp = opendir(".")) == NULL)
throw "Error opening directory";
while ((entry = readdir(dirp)) != NULL)
{
struct stat finfo;
if (entry->d_name[0] == '.')
continue;
stat(entry->d_name,&finfo);
if (finfo.st_mode & S_IFDIR)
rd(entry->d_name); // Subdirectory
else
{
// Enable delete of file, then do it
chmod(entry->d_name,S_IWRITE);
if (unlink(entry->d_name))
throw "Error deleting file";
}
}
closedir(dirp);
// Remove the directory from its parent
chdir("..");
if (rmdir(dir))
throw "Error deleting directory";
}
// End of File