Guest User

Untitled

a guest
Oct 26th, 2014
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.23 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <dirent.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include <sys/stat.h>
  6.  
  7. void deleteEmptyFileInDirectory(const char* directory);
  8. void deleteEmptyFileInDirectory(DIR* directory);
  9.  
  10. int main(int argc, char** argv) {
  11.     if(argc < 2) {
  12.         fprintf(stderr, "Error argc! No file path to delete empty file in directory!\n");
  13.     }
  14.  
  15.     char* directory = argv[1];
  16.     deleteEmptyFileInDirectory(directory);
  17.  
  18.     return 0;
  19. }
  20.  
  21. void deleteEmptyFileInDirectory(const char* pathToDirectory) {
  22.     char* runDirectory = getcwd(NULL, 0);
  23.  
  24.     DIR* directory = opendir(pathToDirectory);
  25.     deleteEmptyFileInDirectory(directory);
  26.     closedir(directory);
  27.  
  28.     chdir(runDirectory);
  29. }
  30.  
  31. void deleteEmptyFileInDirectory(DIR* directory) {
  32.     struct dirent* fileDirent = readdir(directory);
  33.  
  34.     while(fileDirent != NULL) {
  35.         char *fileName = fileDirent->d_name;
  36.         if(strcmp(fileName, ".") != 0 && strcmp(fileName, "..") != 0) {
  37.             struct stat stateFile;
  38.             stat(fileName, &stateFile);
  39.             if(S_ISREG(stateFile.st_mode) != 0 && stateFile.st_size == 0) {
  40.                 remove(fileName);
  41.             }
  42.         }
  43.         fileDirent = readdir(directory);
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment