Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <dirent.h>
- #include <string.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- int move(char* loc,char* dest);
- int CodToInt(char* cod);
- char* substring(char* input,int a,int b);
- char* getMoveRegister(char* career, char* year, char* input);
- char* getYear(char* codYear);
- char* getCareer(char* codCareer);
- int
- main (void)
- {
- DIR *dp;
- FILE *file;
- struct dirent *ep;
- //descomponer nombre
- char* year;
- char* career;
- char* codYear;
- char* codCareer;
- //registro log.txt
- char* moveRegister;
- //crear directorio
- char* directory;
- //mover archivos
- char* input;
- char* output;
- int length;
- file = fopen("./tarea/log.txt","a+");
- dp = opendir ("./tarea");
- directory = (char*)malloc(50*sizeof(char));
- input = (char*)malloc(50*sizeof(char));
- output = (char*)malloc(50*sizeof(char));
- if (dp != NULL)
- {
- while ((ep = readdir (dp)))
- {
- /*filtra archivos que no comportan formato*/
- length = strlen(ep->d_name);
- if((length == 14) || (length == 16))
- {
- year = substring(ep->d_name,0,4);
- if((strcmp(year,"2011") == 0) || (strcmp(year,"2010") == 0))
- {
- /*saca codigo de carrera*/
- codCareer = substring(ep->d_name,4,6);
- career = getCareer(codCareer);
- }
- else
- {
- codYear = substring(ep->d_name,0,2);
- /*recalcula year*/
- free(year);
- year = getYear(codYear);
- /*saca codigo de carrera*/
- codCareer = substring(ep->d_name,2,4);
- career = getCareer(codCareer);
- }
- if((career != NULL) && (year != NULL))
- {
- //crea directorio
- sprintf(directory,"tarea/%s",year);
- mkdir(directory, S_IRWXU | S_IRWXG | S_IRWXO);
- sprintf(directory,"%s/%s",directory,career);
- mkdir(directory, S_IRWXU | S_IRWXG | S_IRWXO);
- //mueve archivos
- sprintf(input,"tarea/%s",ep->d_name);
- sprintf(output,"%s/%s",directory,ep->d_name);
- printf("de %s\n", input);
- printf("a %s\n", output);
- if(move(input,output) != 0)
- perror("error mover\n");
- moveRegister = getMoveRegister(career,year,ep->d_name);
- fputs (moveRegister, file);
- free(moveRegister);
- moveRegister = NULL;
- }
- }/*endif*/
- }/*endWhiel*/
- fclose(file);
- (void) closedir (dp);
- }/*endif*/
- else
- perror ("No se puede crear el directorio");
- printf("libera espacio\n");
- free(year);
- year = NULL;
- free(codYear);
- codYear = NULL;
- free(career);
- career = NULL;
- free(codCareer);
- codCareer = NULL;
- free(directory);
- directory = NULL;
- free(input);
- input = NULL;
- free(output);
- output = NULL;
- return 0;
- }
- /*obtiene subcadena de input(desde "a" a "b")*/
- char* substring(char* input,int a,int b)
- {
- char* output;
- if(a>b) output = NULL;
- else
- output = (char*)malloc((b-a + 1)*sizeof(char));
- strncpy(output, input+a, (b-a));
- output[b-a] = '\0';
- return output;
- }
- /*transforma el codigo(año o carrera) en entero*/
- int CodToInt(char* cod)
- {
- int n,m;
- n = (int)cod[0] - 48;
- m = (int)cod[1] - 48;
- return (10*n+m);
- }
- /*funcion retorna el string del registro*/
- char* getMoveRegister(char* career, char* year, char* input)
- {
- char* output;
- output = (char*)malloc(100*sizeof(char));
- sprintf(output, "El archivo '%s' fue movido a la carpeta 'tareas/%s/%s' [ x kb]\n", input, year,career);
- return(output);
- }
- /*funcion transforma codigo de año en el año (20-29)*/
- char* getYear(char* codYear)
- {
- int iCod;
- char* output;
- output = (char*)malloc(5*sizeof(char));
- /*revisa que se valido el codigo*/
- iCod = CodToInt(codYear);
- if((iCod >= 20) && (iCod <= 29))
- {
- output[0] = codYear[0];
- output[1] = '0';
- output[2] = '0';
- output[3] = codYear[1];
- output[4] = '\0';
- }
- else
- output = NULL;
- return output;
- }
- /*funcion que retorna string carrera segun el codigo*/
- char* getCareer(char* codCareer)
- {
- char* output;
- int iCod;
- output = (char*)malloc(30*sizeof(char));
- iCod = CodToInt(codCareer);
- switch(iCod)
- {
- case 4:
- output = "PlanComun";
- break;
- case 3:
- output = "PPI";
- break;
- case 30:
- output = "Telematica";
- break;
- case 21:
- output = "Electronica";
- break;
- case 23:
- output = "Electrica";
- break;
- case 90:
- output = "Especial";
- break;
- case 73:
- output = "Informatica";
- break;
- case 51:
- output = "Quimica";
- break;
- case 84:
- output = "Ambiental";
- break;
- case 41:
- output = "Mecanica";
- break;
- case 60:
- output = "Industrial";
- break;
- case 66:
- output = "Comercial";
- break;
- case 11:
- output = "ObrasCiviles";
- break;
- case 02:
- output = "Especial";
- break;
- case 44:
- output = "IDP";
- break;
- case 43:
- output = "MecanicaIndustrial";
- break;
- case 13:
- output = "Arquitectura";
- break;
- case 12:
- output = "ConstruccionCivil";
- break;
- default:
- output = NULL;
- }
- return output;
- }
- //funcion para mover archivos
- int move(char* loc,char* dest)
- {
- FILE *source,*target;
- int byte;
- source = fopen(loc,"rb+");
- if(source==NULL)
- {
- printf("problema directorio fuente!!.");
- exit(0);
- }
- target = fopen(dest,"wb+");
- if(target==NULL)
- {
- printf("problema directorio objetivo!!!.");
- fclose(source);
- exit(0);
- }
- while(1)
- {
- byte=fgetc(source);
- if(byte==EOF)
- {
- break;
- }
- else
- fputc(byte,target);
- }
- fclose(source);
- fclose(target);
- if(unlink (loc) < 0)
- perror("error borrar\n");
- }
Add Comment
Please, Sign In to add comment