1  /* emdir.c v1.0 - 2012-01-22 */
 2  
 3  /*
 4  Update:
 5    http://myc01.free.fr/emdir/
 6  
 7  Emdir empty the contents of one or more specified directories.
 8  Usage:  emdir.exe "Directory"
 9  
10  License:
11    Public Domain Dedication (CC0 1.0)
12    http://creativecommons.org/publicdomain/zero/1.0/
13  */
14  
15  #include <windows.h>
16  #include <stdio.h>
17  
18  #define LEN_PATH    1024
19  
20  int result = 0;
21  
22  
23  /* The EmptyDirectory function deletes all the contents from a specified directory. */
24  void EmptyDirectory (char* folderPath)
25  {
26      char fileFound[LEN_PATH], subFolder[LEN_PATH];
27      WIN32_FIND_DATA info;
28      HANDLE hp;
29  
30      strcpy (fileFound, folderPath);
31      strcat (fileFound, "\\*.*");
32  
33      if ((hp = FindFirstFile (fileFound, &info)) == INVALID_HANDLE_VALUE)
34      {
35          result = 1;
36          return;
37      }
38  
39      do
40      {
41          if (*info.cFileName != '.')
42          {
43              if ((info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY)
44              {
45                  strcpy (subFolder, folderPath);
46                  strcat (subFolder, "\\");
47                  strcat (subFolder, info.cFileName);
48  
49                  EmptyDirectory  (subFolder);
50                  if (RemoveDirectory (subFolder) == FALSE)
51                      result = 1;
52              }
53              else
54              {
55                  strcpy (fileFound, folderPath);
56                  strcat (fileFound, "\\");
57                  strcat (fileFound, info.cFileName);
58                  if (DeleteFile (fileFound) == FALSE)
59                  {
60                      SetFileAttributes (fileFound, FILE_ATTRIBUTE_NORMAL);
61                      if (DeleteFile (fileFound) == FALSE)
62                          result = 1;
63                  }
64              }
65          }
66      } while (FindNextFile (hp, &info));
67  
68      FindClose (hp);
69  }  /* EmptyDirectory */
70  
71  
72  int main (int argc, char *argv[])
73  {
74      if (argc == 1)
75      {
76          puts ("emdir deletes all the contents from a specified directory.");
77          puts ("Usage:  emdir.exe \"Directory\"");
78      }
79      else
80      {
81          while (argc-- > 1)
82          {
83              int n = strlen (argv[argc]);
84              char *text = (char *)calloc (n + 1, 1);
85              CharToOemBuff (argv[argc], text, n);
86              printf ("Empty \"%s\" ...\n", text);
87              free (text);
88  
89              EmptyDirectory (argv[argc]);
90          }
91      }
92  
93      return result;
94  }  /* main */