Advertisement
shadeyourself

second itog

May 30th, 2020
893
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.41 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include <dirent.h>
  4. #include <string.h>
  5. #include <sys/stat.h>
  6. #include <stdlib.h>
  7.  
  8. int ifAvi(const char * filePath);
  9. int getSize(const char * filePath);
  10. int ifSame(const char * firsFilePath, const char * secondFilePath);
  11. void  fileCount(  char * dir,   char * filePath, int * count);
  12. void dirwalker(  char * startDir,   char * finishDir);
  13.  
  14.  
  15. //C:/Users/m video/Desktop/videos
  16. int main(){
  17.     char startDir[PATH_MAX+1], finishDir[PATH_MAX+1];
  18.  
  19.     gets(startDir);
  20.     gets(finishDir);
  21.  
  22.     printf("-------------------------\n");
  23.  
  24.     strcat(finishDir, "/");
  25.     strcat(finishDir, "avis");
  26.  
  27.     mkdir(finishDir);
  28.  
  29.     dirwalker(startDir, finishDir);
  30.  
  31.     return 0;
  32. }
  33. //C:/Users/m video/Desktop/videos
  34. int getSize(const char * filePath){
  35.     int size;
  36.  
  37.     FILE * f = fopen(filePath, "r");
  38.     if(!f)return -1;
  39.  
  40.     fseek(f, 0L, SEEK_END);
  41.     size = ftell(f);
  42.  
  43.     fclose(f);
  44.  
  45.     return size;
  46. }
  47. //C:/Users/m video/Desktop/videos
  48. int ifAvi(const char * filePath){
  49.     FILE * f = fopen(filePath, "rb");
  50.     if(!f) return -1;
  51.  
  52.     char buf[13];
  53.     size_t result = fread(buf, 1, 12, f);
  54.  
  55.     if(result < 12) return 0;
  56.  
  57.     if(buf[0] != 'R') return 0;
  58.     if(buf[1] != 'I') return 0;
  59.     if(buf[2] != 'F') return 0;
  60.     if(buf[3] != 'F') return 0;
  61.     if(buf[8] != 'A') return 0;
  62.     if(buf[9] != 'V') return 0;
  63.     if(buf[10] != 'I') return 0;
  64.  
  65.     fclose(f);
  66.     return 1;
  67. }
  68. //C:/Users/m video/Desktop/videos
  69. int ifSame(const char * firsFilePath, const char * secondFilePath){
  70.     FILE * fisrtFile = fopen(firsFilePath, "r");
  71.     if(!fisrtFile) return -1;
  72.  
  73.     FILE * secondFile = fopen(secondFilePath, "r");
  74.     if(!secondFile) return -1;
  75.  
  76.     int firstFileSize = getSize(firsFilePath);
  77.     int secondFileSize = getSize(secondFilePath);
  78.     char first;
  79.     char second;
  80.  
  81.     if(firstFileSize != secondFileSize) return 0;
  82.  
  83.     for(int i; i<firstFileSize; i++){
  84.         first = fgetc(fisrtFile);
  85.         second = fgetc(secondFile);
  86.         if(first!=second) return 0;
  87.     }
  88.     return 1;
  89. }
  90. //C:/Users/m video/Desktop/videos
  91. void fileCount(char * dir, char * filePath, int * count){
  92.     DIR *dp;
  93.     if((dp = opendir(dir)) == NULL) {
  94.         printf("cannot open directory\n");
  95.         return;
  96.     }
  97.  
  98.     char currentPath[PATH_MAX+1];
  99.  
  100.     struct dirent *entry;
  101.     struct stat statbuf;
  102.  
  103.     chdir(dir);
  104.     while((entry = readdir(dp)) != NULL) {
  105.  
  106.         strcpy(currentPath, dir);
  107.         strcat(currentPath, "/");
  108.         strcat(currentPath, entry->d_name);
  109.  
  110.         stat(entry->d_name,&statbuf);
  111.         if(S_ISDIR(statbuf.st_mode)) {
  112.             if(strcmp(".",entry->d_name) == 0 || strcmp("..",entry->d_name) == 0 || strcmp("avis",entry->d_name) == 0)
  113.                 continue;
  114.  
  115.             fileCount(currentPath, filePath, count);
  116.         }
  117.         //printf("FILECOUNT:\ncurrentPath: %s\n", currentPath);
  118.         if(ifSame(filePath, currentPath) == 1) {
  119.             *(count)=*(count) + 1;
  120.  
  121.         }
  122.     }
  123.     chdir("..");
  124.     closedir(dp);
  125. }
  126. //C:/Users/m video/Desktop/videos
  127. void dirwalker( char * startDir,  char * finishDir)
  128. {
  129.     DIR *dp;
  130.     if((dp = opendir(startDir)) == NULL) {
  131.         printf("cannot open directory\n");
  132.         return;
  133.     }
  134.  
  135.     char currentPath[PATH_MAX+1];
  136.    
  137.     struct dirent *entry;
  138.     struct stat statbuf;
  139.  
  140.     chdir(startDir);
  141.     while((entry = readdir(dp)) != NULL) {
  142.         strcpy(currentPath, startDir);
  143.         strcat(currentPath, "/");
  144.         strcat(currentPath, entry->d_name);
  145.        
  146.         stat(entry->d_name,&statbuf);
  147.         if(S_ISDIR(statbuf.st_mode)) {
  148.             if(strcmp(".",entry->d_name) == 0 || strcmp("..",entry->d_name) == 0 || strcmp("avis",entry->d_name) == 0)
  149.                 continue;
  150.             dirwalker(currentPath, finishDir);
  151.         }
  152.  
  153.         //ПЫТАЕМСЯ ПЕРЕНЕСТИ ДУБЛИКАТЫ
  154.         printf("%s\n", currentPath);
  155.  
  156.         if(ifAvi(currentPath) != 1) continue;
  157.  
  158.         int count = 0;
  159.         fileCount(startDir, currentPath, &count);
  160.         if(count <= 1) continue;
  161.        
  162.         char newPath[PATH_MAX+1];
  163.         strcpy(newPath, finishDir);
  164.         strcat(newPath, "/");
  165.         strcat(newPath, entry->d_name);
  166.  
  167.         rename(currentPath, newPath);
  168.     }
  169.     chdir("..");
  170.     closedir(dp);
  171. }
  172.  
  173. //C:/Users/m video/Desktop/videos
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement