1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
| #include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#ifndef WIN32
#include <sys/types.h>
#define CLEAR "clear"
/* system("clear") pour UNIX */
#else
#define CLEAR "cls"
/* system("cls") pour Windows */
#endif
FILE *open_file(char *path)
{
FILE *file = fopen(path, "a+");
if ( file == NULL )
{
fprintf(stderr,"\nEchec ouverture fichier %s", path);
fclose(file);
}
return file;
}
DIR *open_directory(char * path)
{
if ( opendir(path)==NULL )
fprintf(stderr,"\nOuverture dossier echouee : %s", path);
return opendir(path);
}
DIR *is_directory(char * path)
{
return opendir(path);
}
void close_directory(DIR *directory)
{
if ( closedir(directory)==-1 )
fprintf(stderr,"\nFermeture dossier echouee");
closedir(directory);
}
void read_directory(DIR *directory, char *path)
{
directory = open_directory(path);
struct dirent *entity = NULL;
while( (entity = readdir(directory)) != NULL )
{
printf("\n%s", entity->d_name);
}
close_directory(directory);
}
// Fonction qui détermine si un caractère est numérique
unsigned int is_numeric( char c )
{
if ( '0' <= c && c <= '9')
return 1;
else return 0;
}
// Fonction pour compter le nombre de caractères qui ne sont pas numériques dans une chaine
unsigned int str_contains_alpha(char *str)
{
int i, c=0;
for ( i=0 ; i<strlen(str); i++)
{
if ( is_numeric(str[i])==0 )
{
c++;
}
}
return c;
}
// Fonction qui concatène le nom du fichier lu avec le chemin absolue de son repertoire
char *get_current_path( char* path, char *next)
{
char *current_path=(char*)malloc((strlen(path)+strlen(next)+2)*sizeof(char));
int i;
for (i=0; i<strlen(path);i++)
current_path[i]=path[i];
current_path[strlen(path)]='\\';
for (i=strlen(path)+1; i<strlen(path)+strlen(next)+1;i++)
current_path[i]=next[i-strlen(path)-1];
current_path[strlen(path)+strlen(next)+1]='\0';
return current_path;
}
// Fonction qui vérifie si le nom de fichier peut être renommé ou s'il faudra le faire manuellement
void rename_file(char *path, char *current_path, struct dirent *entity)
{
FILE *rename_ok = open_file("rename_ok.txt");
FILE *rename_nok = open_file("rename_nok.txt");
FILE *no_rename = open_file("no_rename.txt");
//Si le nom de fichier possède au moins 4 caractères et pas plus de 7
if ( 4<=entity->d_namlen && entity->d_namlen<8 )
{
char *id=(char*)malloc(5*sizeof(char));
id[4]='\0';
char name[9]="0001XXXX";
int i;
for (i=entity->d_namlen-4 ; i<entity->d_namlen ; i++)
id[i-entity->d_namlen+4]=entity->d_name[i];
//Si l'ID est purement alphanumérique
int count=0;
if ( (count=str_contains_alpha(id))==0 )
{
for (i=4 ; i<8 ; i++)
name[i]=id[i-4];
int status;
if ( (status = rename(current_path, get_current_path( path, name))) == 0 )
fprintf(rename_ok,"%s en %s\n", current_path, get_current_path( path, name));
else fprintf(rename_nok,"%s en %s\n", current_path, get_current_path( path, name));
}
//Le traitement devra être manuel
else fprintf(no_rename,"%s\n", current_path);
free(id);
}
//Le traitement devra être manuel
else fprintf(no_rename,"%s\n", current_path);
fclose(rename_ok);
fclose(rename_nok);
fclose(no_rename);
}
/* Fonction qui vérifie que les noms de fichiers respecte la règle de nommage */
void check_file_name_pattern(char *path, char *current_path, struct dirent *entity)
{
FILE *ok = open_file("file_ok.txt");
FILE *nok = open_file("file_nok.txt");
FILE *other = open_file("file_others.txt");
// Si le nom de fichier ne contient pas de point
if ( strrchr(entity->d_name, '.') == NULL )
{
// Si le nom de fichier ne comporte pas plus de 2 caractères alphabétique
if ( str_contains_alpha(entity->d_name) <= 2)
{
// Si le nom de fichier comporte exactement 8 caractères
if ( entity->d_namlen == 8 )
{
//Si le nom de fichier commence par 0001
if ( entity->d_name[0]=='0' && entity->d_name[1]=='0' && entity->d_name[2]=='0' && entity->d_name[3]=='1')
{
// Si le nom de fichier se termine par des caractères numériques
if ( is_numeric(entity->d_name[4])==1 && is_numeric(entity->d_name[5])==1 && is_numeric(entity->d_name[6])==1 && is_numeric(entity->d_name[7])==1)
{
fprintf(ok,"%s\n", current_path);
}
else
{
fprintf(nok,"!={0-9} ; %s\n", current_path);
rename_file(path, current_path, entity);
}
}
else
{
fprintf(nok,"!=0001 ; %s\n", current_path);
rename_file(path, current_path, entity);
}
}
else
{
fprintf(nok,"!=8 ; %s\n", current_path);
rename_file(path, current_path, entity);
}
}
else fprintf(other,"alpha>2 ; %s\n", current_path);
}
else fprintf(other,"\".\" ; %s\n", current_path);
fclose(ok);
fclose(nok);
fclose(other);
}
void browse_directory(DIR *directory, char *path)
{
directory = open_directory(path); // Ouvrir le répertoire courant
struct dirent *entity=NULL; // Déclarer la structure de repertoire
seekdir(directory,2); // Déplacer le curseur de la structure du repetoire après . & ..
while( (entity=readdir(directory)) != NULL ) // Tant que le repertoire courant n'a pas été entièrement parcouru
{
char *current_path = get_current_path(path, entity->d_name); // On récupère le chemin de l'entité(fichier ou dossier) du repertoire courant
if ( is_directory(current_path) != NULL ) // Si l'entité est un dossier
{
printf("\nDOSSIER %s", current_path);
browse_directory(directory, current_path);
}
else // Sinon c'est un fichier
{
printf("\nFILE %s", current_path);
check_file_name_pattern(path, current_path, entity);
}
}
}
int main (void)
{
//Effacer les anciens fichiers de logs
remove("file_ok.txt");
remove("file_nok.txt");
remove("file_others.txt");
remove("rename_ok.txt");
remove("rename_nok.txt");
remove("no_rename.txt");
//Chemin du dossier à parcourir recursivement
char path[999]="C:\\Documents and Settings\\alv\\Bureau\\programmes";
//Traitement
DIR* directory=NULL;
browse_directory(directory,path);
close_directory(directory);
return 0;
} |
Partager