Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <unistd.h>
- #include <stdio.h>
- #include <dirent.h>
- #include <string.h>
- #include <sys/stat.h>
- #include <stdlib.h>
- int ifAvi(const char * filePath);
- int getSize(const char * filePath);
- int ifSame(const char * firsFilePath, const char * secondFilePath);
- void fileCount( char * dir, char * filePath, int * count);
- void dirwalker( char * startDir, char * finishDir);
- //C:/Users/m video/Desktop/videos
- int main(){
- char startDir[PATH_MAX+1], finishDir[PATH_MAX+1];
- gets(startDir);
- gets(finishDir);
- printf("-------------------------\n");
- strcat(finishDir, "/");
- strcat(finishDir, "avis");
- mkdir(finishDir);
- dirwalker(startDir, finishDir);
- return 0;
- }
- //C:/Users/m video/Desktop/videos
- int getSize(const char * filePath){
- int size;
- FILE * f = fopen(filePath, "r");
- if(!f)return -1;
- fseek(f, 0L, SEEK_END);
- size = ftell(f);
- fclose(f);
- return size;
- }
- //C:/Users/m video/Desktop/videos
- int ifAvi(const char * filePath){
- FILE * f = fopen(filePath, "rb");
- if(!f) return -1;
- char buf[13];
- size_t result = fread(buf, 1, 12, f);
- if(result < 12) return 0;
- if(buf[0] != 'R') return 0;
- if(buf[1] != 'I') return 0;
- if(buf[2] != 'F') return 0;
- if(buf[3] != 'F') return 0;
- if(buf[8] != 'A') return 0;
- if(buf[9] != 'V') return 0;
- if(buf[10] != 'I') return 0;
- fclose(f);
- return 1;
- }
- //C:/Users/m video/Desktop/videos
- int ifSame(const char * firsFilePath, const char * secondFilePath){
- FILE * fisrtFile = fopen(firsFilePath, "r");
- if(!fisrtFile) return -1;
- FILE * secondFile = fopen(secondFilePath, "r");
- if(!secondFile) return -1;
- int firstFileSize = getSize(firsFilePath);
- int secondFileSize = getSize(secondFilePath);
- char first;
- char second;
- if(firstFileSize != secondFileSize) return 0;
- for(int i; i<firstFileSize; i++){
- first = fgetc(fisrtFile);
- second = fgetc(secondFile);
- if(first!=second) return 0;
- }
- return 1;
- }
- //C:/Users/m video/Desktop/videos
- void fileCount(char * dir, char * filePath, int * count){
- DIR *dp;
- if((dp = opendir(dir)) == NULL) {
- printf("cannot open directory\n");
- return;
- }
- char currentPath[PATH_MAX+1];
- struct dirent *entry;
- struct stat statbuf;
- chdir(dir);
- while((entry = readdir(dp)) != NULL) {
- strcpy(currentPath, dir);
- strcat(currentPath, "/");
- strcat(currentPath, entry->d_name);
- stat(entry->d_name,&statbuf);
- if(S_ISDIR(statbuf.st_mode)) {
- if(strcmp(".",entry->d_name) == 0 || strcmp("..",entry->d_name) == 0 || strcmp("avis",entry->d_name) == 0)
- continue;
- fileCount(currentPath, filePath, count);
- }
- //printf("FILECOUNT:\ncurrentPath: %s\n", currentPath);
- if(ifSame(filePath, currentPath) == 1) {
- *(count)=*(count) + 1;
- }
- }
- chdir("..");
- closedir(dp);
- }
- //C:/Users/m video/Desktop/videos
- void dirwalker( char * startDir, char * finishDir)
- {
- DIR *dp;
- if((dp = opendir(startDir)) == NULL) {
- printf("cannot open directory\n");
- return;
- }
- char currentPath[PATH_MAX+1];
- struct dirent *entry;
- struct stat statbuf;
- chdir(startDir);
- while((entry = readdir(dp)) != NULL) {
- strcpy(currentPath, startDir);
- strcat(currentPath, "/");
- strcat(currentPath, entry->d_name);
- stat(entry->d_name,&statbuf);
- if(S_ISDIR(statbuf.st_mode)) {
- if(strcmp(".",entry->d_name) == 0 || strcmp("..",entry->d_name) == 0 || strcmp("avis",entry->d_name) == 0)
- continue;
- dirwalker(currentPath, finishDir);
- }
- //ПЫТАЕМСЯ ПЕРЕНЕСТИ ДУБЛИКАТЫ
- printf("%s\n", currentPath);
- if(ifAvi(currentPath) != 1) continue;
- int count = 0;
- fileCount(startDir, currentPath, &count);
- if(count <= 1) continue;
- char newPath[PATH_MAX+1];
- strcpy(newPath, finishDir);
- strcat(newPath, "/");
- strcat(newPath, entry->d_name);
- rename(currentPath, newPath);
- }
- chdir("..");
- closedir(dp);
- }
- //C:/Users/m video/Desktop/videos
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement