Advertisement
PofMagicfingers

remove_all_files.c

Mar 28th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.74 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <dirent.h>
  3.  
  4.  
  5. int main(int argc, char *argv[]) {
  6.     DIR *dir;
  7.     struct dirent *ent;
  8.     long total = 0;
  9.     int starting = 1;
  10.  
  11.     dir = opendir(argv[1]);
  12.  
  13.     long count;
  14.     while(starting == 1 || count > 0) {
  15.       count = 0;
  16.       starting = 0;
  17.       while((ent = readdir(dir))) {
  18.         count++;
  19.         if(DT_REG == ent->d_type) {
  20.           char path[1024];
  21.           sprintf(path, "%s/%s", argv[1], ent->d_name);
  22.           printf("removing %s\n", path);
  23.           if (unlink(path) < 0) {
  24.             exit(-500);
  25.           } else {
  26.             total++;
  27.           }
  28.         }
  29.       }
  30.     }
  31.  
  32.     closedir(dir);
  33.  
  34.     printf("%s: removed %ld files\n", argv[1], total);
  35.  
  36.     return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement