IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

 C Discussion :

Fonction rename qui renomme pas toujours


Sujet :

C

  1. #1
    Membre confirmé
    Homme Profil pro
    amateur
    Inscrit en
    Octobre 2007
    Messages
    731
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : amateur

    Informations forums :
    Inscription : Octobre 2007
    Messages : 731
    Points : 460
    Points
    460
    Par défaut Fonction rename qui renomme pas toujours
    Bonjour,

    J'essaye de renommer des fichiers mais parfois cela ne marche pas alors je ne comprends pas bien pourquoi.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    void check_file_name(char *path, char *current_path, struct dirent *entity)
    {
        if ( 4<=entity->d_namlen && entity->d_namlen<8 )
        {
            char id[5]="XXXX";
            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];
     
            for (i=4 ; i<8 ; i++)
                name[i]=id[i-4];
     
            int result=rename(current_path, get_current_path( path, name));
     
            FILE *log = NULL;
     
            if ( (log = fopen("log.txt","a+")) != NULL  )
                fprintf(log,"\n--rename %s in %s | %d", current_path, get_current_path( path, name), result);
     
            else fprintf(stderr,"\n --erreur ouverture fichier log.txt");
     
            fclose(log);
        }
    }
    J'ai un log qui donne ca :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
     
    --rename C:\Documents and Settings\alv\Bureau\programmes\103\0019982 in C:\Documents and Settings\alv\Bureau\programmes\103\00019982 | -1
    --rename C:\Documents and Settings\alv\Bureau\programmes\103\O009853 in C:\Documents and Settings\alv\Bureau\programmes\103\00019853 | -1
    --rename C:\Documents and Settings\alv\Bureau\programmes\103\O009960 in C:\Documents and Settings\alv\Bureau\programmes\103\00019960 | -1
    --rename C:\Documents and Settings\alv\Bureau\programmes\103\O9960 in C:\Documents and Settings\alv\Bureau\programmes\103\00019960 | -1
    --rename C:\Documents and Settings\alv\Bureau\programmes\106\1026 in C:\Documents and Settings\alv\Bureau\programmes\106\00011026 | -1
    --rename C:\Documents and Settings\alv\Bureau\programmes\106\1045 in C:\Documents and Settings\alv\Bureau\programmes\106\00011045 | -1
    --rename C:\Documents and Settings\alv\Bureau\programmes\106\1100 in C:\Documents and Settings\alv\Bureau\programmes\106\00011100 | -1
    --rename C:\Documents and Settings\alv\Bureau\programmes\106\1114 in C:\Documents and Settings\alv\Bureau\programmes\106\00011114 | -1
    --rename C:\Documents and Settings\alv\Bureau\programmes\106\1137 in C:\Documents and Settings\alv\Bureau\programmes\106\00011137 | -1
    --rename C:\Documents and Settings\alv\Bureau\programmes\106\1163 in C:\Documents and Settings\alv\Bureau\programmes\106\00011163 | -1
    --rename C:\Documents and Settings\alv\Bureau\programmes\106\1175 in C:\Documents and Settings\alv\Bureau\programmes\106\00011175 | -1
    --rename C:\Documents and Settings\alv\Bureau\programmes\106\O1014 in C:\Documents and Settings\alv\Bureau\programmes\106\00011014 | -1
    --rename C:\Documents and Settings\alv\Bureau\programmes\107\204113 in C:\Documents and Settings\alv\Bureau\programmes\107\00014113 | -1
    --rename C:\Documents and Settings\alv\Bureau\programmes\107\244113 in C:\Documents and Settings\alv\Bureau\programmes\107\00014113 | -1
    --rename C:\Documents and Settings\alv\Bureau\programmes\107\244235 in C:\Documents and Settings\alv\Bureau\programmes\107\00014235 | -1
    --rename C:\Documents and Settings\alv\Bureau\programmes\107\244276 in C:\Documents and Settings\alv\Bureau\programmes\107\00014276 | -1
    --rename C:\Documents and Settings\alv\Bureau\programmes\107\284272 in C:\Documents and Settings\alv\Bureau\programmes\107\00014272 | -1
    --rename C:\Documents and Settings\alv\Bureau\programmes\107\284387 in C:\Documents and Settings\alv\Bureau\programmes\107\00014387 | -1
    --rename C:\Documents and Settings\alv\Bureau\programmes\107\284443 in C:\Documents and Settings\alv\Bureau\programmes\107\00014443 | -1
    --rename C:\Documents and Settings\alv\Bureau\programmes\107\284773 in C:\Documents and Settings\alv\Bureau\programmes\107\00014773 | -1
    --rename C:\Documents and Settings\alv\Bureau\programmes\107\286203 in C:\Documents and Settings\alv\Bureau\programmes\107\00016203 | -1
    --rename C:\Documents and Settings\alv\Bureau\programmes\107\288001 in C:\Documents and Settings\alv\Bureau\programmes\107\00018001 | -1
    --rename C:\Documents and Settings\alv\Bureau\programmes\107\324386 in C:\Documents and Settings\alv\Bureau\programmes\107\00014386 | -1
    --rename C:\Documents and Settings\alv\Bureau\programmes\107\324442 in C:\Documents and Settings\alv\Bureau\programmes\107\00014442 | -1
    --rename C:\Documents and Settings\alv\Bureau\programmes\107\328002 in C:\Documents and Settings\alv\Bureau\programmes\107\00018002 | -1
    --rename C:\Documents and Settings\alv\Bureau\programmes\107\328007 in C:\Documents and Settings\alv\Bureau\programmes\107\00018007 | -1
    --rename C:\Documents and Settings\alv\Bureau\programmes\107\328008 in C:\Documents and Settings\alv\Bureau\programmes\107\00018008 | -1
    --rename C:\Documents and Settings\alv\Bureau\programmes\107\384383 in C:\Documents and Settings\alv\Bureau\programmes\107\00014383 | -1
    --rename C:\Documents and Settings\alv\Bureau\programmes\107\384385 in C:\Documents and Settings\alv\Bureau\programmes\107\00014385 | -1
    --rename C:\Documents and Settings\alv\Bureau\programmes\107\384386 in C:\Documents and Settings\alv\Bureau\programmes\107\00014386 | -1
    --rename C:\Documents and Settings\alv\Bureau\programmes\107\384387 in C:\Documents and Settings\alv\Bureau\programmes\107\00014387 | -1
    --rename C:\Documents and Settings\alv\Bureau\programmes\107\388001 in C:\Documents and Settings\alv\Bureau\programmes\107\00018001 | -1
    --rename C:\Documents and Settings\alv\Bureau\programmes\107\444383 in C:\Documents and Settings\alv\Bureau\programmes\107\00014383 | -1
    --rename C:\Documents and Settings\alv\Bureau\programmes\107\444384 in C:\Documents and Settings\alv\Bureau\programmes\107\00014384 | -1
    --rename C:\Documents and Settings\alv\Bureau\programmes\107\448001 in C:\Documents and Settings\alv\Bureau\programmes\107\00018001 | -1
    --rename C:\Documents and Settings\alv\Bureau\programmes\107\60341 in C:\Documents and Settings\alv\Bureau\programmes\107\00010341 | -1
    --rename C:\Documents and Settings\alv\Bureau\programmes\107\68485 in C:\Documents and Settings\alv\Bureau\programmes\107\00018485 | -1
    --rename C:\Documents and Settings\alv\Bureau\programmes\108\1006 in C:\Documents and Settings\alv\Bureau\programmes\108\00011006 | -1
    --rename C:\Documents and Settings\alv\Bureau\programmes\108\1027 in C:\Documents and Settings\alv\Bureau\programmes\108\00011027 | -1
    --rename C:\Documents and Settings\alv\Bureau\programmes\108\1187 in C:\Documents and Settings\alv\Bureau\programmes\108\00011187 | -1
    --rename C:\Documents and Settings\alv\Bureau\programmes\108\1233 in C:\Documents and Settings\alv\Bureau\programmes\108\00011233 | -1
    --rename C:\Documents and Settings\alv\Bureau\programmes\108\1276 in C:\Documents and Settings\alv\Bureau\programmes\108\00011276 | -1
    --rename C:\Documents and Settings\alv\Bureau\programmes\109\01113 in C:\Documents and Settings\alv\Bureau\programmes\109\00011113 | -1
    --rename C:\Documents and Settings\alv\Bureau\programmes\109\1028 in C:\Documents and Settings\alv\Bureau\programmes\109\00011028 | -1
    --rename C:\Documents and Settings\alv\Bureau\programmes\109\1232 in C:\Documents and Settings\alv\Bureau\programmes\109\00011232 | -1
    --rename C:\Documents and Settings\alv\Bureau\programmes\110\06546 in C:\Documents and Settings\alv\Bureau\programmes\110\00016546 | -1
    --rename C:\Documents and Settings\alv\Bureau\programmes\110\06558 in C:\Documents and Settings\alv\Bureau\programmes\110\00016558 | -1
    --rename C:\Documents and Settings\alv\Bureau\programmes\110\6400 in C:\Documents and Settings\alv\Bureau\programmes\110\00016400 | -1
    --rename C:\Documents and Settings\alv\Bureau\programmes\110\6496 in C:\Documents and Settings\alv\Bureau\programmes\110\00016496 | -1
    --rename C:\Documents and Settings\alv\Bureau\programmes\110\6499 in C:\Documents and Settings\alv\Bureau\programmes\110\00016499 | -1
    --rename C:\Documents and Settings\alv\Bureau\programmes\110\6503 in C:\Documents and Settings\alv\Bureau\programmes\110\00016503 | -1
    --rename C:\Documents and Settings\alv\Bureau\programmes\110\6504 in C:\Documents and Settings\alv\Bureau\programmes\110\00016504 | -1
    --rename C:\Documents and Settings\alv\Bureau\programmes\110\6505 in C:\Documents and Settings\alv\Bureau\programmes\110\00016505 | -1
    --rename C:\Documents and Settings\alv\Bureau\programmes\110\6506 in C:\Documents and Settings\alv\Bureau\programmes\110\00016506 | -1
    --rename C:\Documents and Settings\alv\Bureau\programmes\110\6507 in C:\Documents and Settings\alv\Bureau\programmes\110\00016507 | -1
    --rename C:\Documents and Settings\alv\Bureau\programmes\110\O6549 in C:\Documents and Settings\alv\Bureau\programmes\110\00016549 | -1
    --rename C:\Documents and Settings\alv\Bureau\programmes\110\O6589 in C:\Documents and Settings\alv\Bureau\programmes\110\00016589 | -1
    --rename C:\Documents and Settings\alv\Bureau\programmes\122\O1108 in C:\Documents and Settings\alv\Bureau\programmes\122\00011108 | -1
    --rename C:\Documents and Settings\alv\Bureau\programmes\122\O1109 in C:\Documents and Settings\alv\Bureau\programmes\122\00011109 | -1
    --rename C:\Documents and Settings\alv\Bureau\programmes\122\O3001 in C:\Documents and Settings\alv\Bureau\programmes\122\00013001 | -1
    --rename C:\Documents and Settings\alv\Bureau\programmes\122\O3005 in C:\Documents and Settings\alv\Bureau\programmes\122\00013005 | -1
    --rename C:\Documents and Settings\alv\Bureau\programmes\129\Archive 02-10-2012\0001 in C:\Documents and Settings\alv\Bureau\programmes\129\Archive 02-10-2012\00010001 | -1
    --rename C:\Documents and Settings\alv\Bureau\programmes\retour\0000 in C:\Documents and Settings\alv\Bureau\programmes\retour\00010000 | -1
    On voit les -1 du retour de la fonction rename qui montre que les fichiers n'ont pas été renommés. J'ai pourtant tous les droits. Auriez-vous une explication à cet étrange phénomène paranormal?

    Merci d'avance
    UNE REPONSE UTILE : &|| UN PROBLEME RESOLU :

  2. #2
    Modérateur
    Avatar de gangsoleil
    Homme Profil pro
    Manager / Cyber Sécurité
    Inscrit en
    Mai 2004
    Messages
    10 150
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Savoie (Rhône Alpes)

    Informations professionnelles :
    Activité : Manager / Cyber Sécurité

    Informations forums :
    Inscription : Mai 2004
    Messages : 10 150
    Points : 28 119
    Points
    28 119
    Par défaut
    Bonjour,

    Les 3 premiers fonctionnent, et aucun des suivant.

    Pourquoi est-ce que tu utilises namelen -4 ? Pour l'extension que tu n'as pas ? Je pense que tu devrais commenter ton code, je pense qu'il y a des erreurs dedans.
    "La route est longue, mais le chemin est libre" -- https://framasoft.org/
    Les règles du forum

  3. #3
    Membre confirmé
    Homme Profil pro
    amateur
    Inscrit en
    Octobre 2007
    Messages
    731
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : amateur

    Informations forums :
    Inscription : Octobre 2007
    Messages : 731
    Points : 460
    Points
    460
    Par défaut
    En fait les noms de programmes doivent être de la forme 0001XXXX. Malheuresement cela n'a pas été respecté et c'est le gros bordel.

    Donc 0001 invariant et XXXX le numéro de programme que j'ai appelé ID.

    Pour les fichiers qui font entre 4 et 7 caractères
    4 pour les 4 chiffres d'ID
    et 7 caractères max car si 8 le numéro de programme est déjà bon.

    Je récupère dans ID le numéro de programme, je le concatène à 0001, je l'ajoute au path pour avoir le chemin absolu et je demande de renommer.

    Mon log fait 180ko pour 9ko d'erreur. Ce qui donne environ 4% des fichiers qui ne sont pas renommés.
    UNE REPONSE UTILE : &|| UN PROBLEME RESOLU :

  4. #4
    Expert éminent sénior

    Femme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Juin 2007
    Messages
    5 189
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Juin 2007
    Messages : 5 189
    Points : 17 141
    Points
    17 141
    Par défaut
    Tu as un besoin spécifique, qui me semble pouvoir être couvert par le programme linux "rename". Tu dois pouvoir en trouver une variante pour windows
    Mes principes de bases du codeur qui veut pouvoir dormir:
    • Une variable de moins est une source d'erreur en moins.
    • Un pointeur de moins est une montagne d'erreurs en moins.
    • Un copier-coller, ça doit se justifier... Deux, c'est un de trop.
    • jamais signifie "sauf si j'ai passé trois jours à prouver que je peux".
    • La plus sotte des questions est celle qu'on ne pose pas.
    Pour faire des graphes, essayez yEd.
    le ter nel est le titre porté par un de mes personnages de jeu de rôle

  5. #5
    Membre confirmé
    Homme Profil pro
    amateur
    Inscrit en
    Octobre 2007
    Messages
    731
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : amateur

    Informations forums :
    Inscription : Octobre 2007
    Messages : 731
    Points : 460
    Points
    460
    Par défaut
    J'utilise dirent.h qui si j'ai bien compris viens d'abord d'Unix et a été arrangé pour windows. J'utilise donc déjà la fonction rename en question si c'est bien de ça dont tu parlais.
    J'hésite à utiliser "ren" par system mais je préférai éviter et plutôt comprendre l'origine de ces erreurs.
    UNE REPONSE UTILE : &|| UN PROBLEME RESOLU :

  6. #6
    Modérateur
    Avatar de gangsoleil
    Homme Profil pro
    Manager / Cyber Sécurité
    Inscrit en
    Mai 2004
    Messages
    10 150
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Savoie (Rhône Alpes)

    Informations professionnelles :
    Activité : Manager / Cyber Sécurité

    Informations forums :
    Inscription : Mai 2004
    Messages : 10 150
    Points : 28 119
    Points
    28 119
    Par défaut
    Citation Envoyé par leternel Voir le message
    Tu as un besoin spécifique, qui me semble pouvoir être couvert par le programme linux "rename". Tu dois pouvoir en trouver une variante pour windows
    Il ne s'agit pas du programme linux rename, mais de la fonction C rename, qui est (sauf erreur) C89, donc implementee par la tres grande majorite des compilateurs sous quel qu'OS que ce soit.



    Sinon, si j'ai bien compris, tu veux :
    Si 4 <= longueur < 8
    recuperer les 4 derniers caracteres du nom de fichier
    renommer le fichier en 0001XXXX avec XXXX les 4 char

    Et pourquoi pas un simple memcpy ? Un truc du genre :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
      memcpy (tmp_name, entity->d_name[entity->d_namlen -4], 4);
    "La route est longue, mais le chemin est libre" -- https://framasoft.org/
    Les règles du forum

  7. #7
    Membre confirmé
    Homme Profil pro
    amateur
    Inscrit en
    Octobre 2007
    Messages
    731
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : amateur

    Informations forums :
    Inscription : Octobre 2007
    Messages : 731
    Points : 460
    Points
    460
    Par défaut
    Ah oui oui oui, j'achète. Je ne connais pas toutes ces fonctions par cœur, malheureusement =D. Surtout que je mettrais au moins autant de temps sinon plus à chercher le nom de la fonction que de la refaire (en moins bien).

    Merci pour ça.
    UNE REPONSE UTILE : &|| UN PROBLEME RESOLU :

  8. #8
    Modérateur
    Avatar de gangsoleil
    Homme Profil pro
    Manager / Cyber Sécurité
    Inscrit en
    Mai 2004
    Messages
    10 150
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Savoie (Rhône Alpes)

    Informations professionnelles :
    Activité : Manager / Cyber Sécurité

    Informations forums :
    Inscription : Mai 2004
    Messages : 10 150
    Points : 28 119
    Points
    28 119
    Par défaut
    pas de quoi.

    Si tu as des soucis, n'hesite pas a reposter ton nouveau code ici
    "La route est longue, mais le chemin est libre" -- https://framasoft.org/
    Les règles du forum

  9. #9
    Membre confirmé
    Homme Profil pro
    amateur
    Inscrit en
    Octobre 2007
    Messages
    731
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : amateur

    Informations forums :
    Inscription : Octobre 2007
    Messages : 731
    Points : 460
    Points
    460
    Par défaut
    Ben pour le moment ça va, si ce n'est que j'ai toujours des fichiers qui ne veulent pas se renommer pour je ne sais quelle raison !

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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;
    }
    Selon mon log rename_nok, voici les fichiers qui aurait du être renommés ( est affiché le nom modifié pour le renommage )

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    C:\Documents and Settings\alv\Bureau\programmes\103\0019982 en C:\Documents and Settings\alv\Bureau\programmes\103\00019982
    C:\Documents and Settings\alv\Bureau\programmes\103\O009853 en C:\Documents and Settings\alv\Bureau\programmes\103\00019853
    C:\Documents and Settings\alv\Bureau\programmes\103\O009960 en C:\Documents and Settings\alv\Bureau\programmes\103\00019960
    C:\Documents and Settings\alv\Bureau\programmes\103\O9960 en C:\Documents and Settings\alv\Bureau\programmes\103\00019960
    C:\Documents and Settings\alv\Bureau\programmes\106\1026 en C:\Documents and Settings\alv\Bureau\programmes\106\00011026
    C:\Documents and Settings\alv\Bureau\programmes\106\1045 en C:\Documents and Settings\alv\Bureau\programmes\106\00011045
    C:\Documents and Settings\alv\Bureau\programmes\106\1100 en C:\Documents and Settings\alv\Bureau\programmes\106\00011100
    C:\Documents and Settings\alv\Bureau\programmes\106\1114 en C:\Documents and Settings\alv\Bureau\programmes\106\00011114
    C:\Documents and Settings\alv\Bureau\programmes\106\1137 en C:\Documents and Settings\alv\Bureau\programmes\106\00011137
    C:\Documents and Settings\alv\Bureau\programmes\106\1163 en C:\Documents and Settings\alv\Bureau\programmes\106\00011163
    C:\Documents and Settings\alv\Bureau\programmes\106\1175 en C:\Documents and Settings\alv\Bureau\programmes\106\00011175
    C:\Documents and Settings\alv\Bureau\programmes\106\O1014 en C:\Documents and Settings\alv\Bureau\programmes\106\00011014
    C:\Documents and Settings\alv\Bureau\programmes\107\204113 en C:\Documents and Settings\alv\Bureau\programmes\107\00014113
    C:\Documents and Settings\alv\Bureau\programmes\107\244113 en C:\Documents and Settings\alv\Bureau\programmes\107\00014113
    C:\Documents and Settings\alv\Bureau\programmes\107\244235 en C:\Documents and Settings\alv\Bureau\programmes\107\00014235
    C:\Documents and Settings\alv\Bureau\programmes\107\244276 en C:\Documents and Settings\alv\Bureau\programmes\107\00014276
    C:\Documents and Settings\alv\Bureau\programmes\107\284272 en C:\Documents and Settings\alv\Bureau\programmes\107\00014272
    C:\Documents and Settings\alv\Bureau\programmes\107\284387 en C:\Documents and Settings\alv\Bureau\programmes\107\00014387
    C:\Documents and Settings\alv\Bureau\programmes\107\284443 en C:\Documents and Settings\alv\Bureau\programmes\107\00014443
    C:\Documents and Settings\alv\Bureau\programmes\107\284773 en C:\Documents and Settings\alv\Bureau\programmes\107\00014773
    C:\Documents and Settings\alv\Bureau\programmes\107\286203 en C:\Documents and Settings\alv\Bureau\programmes\107\00016203
    C:\Documents and Settings\alv\Bureau\programmes\107\288001 en C:\Documents and Settings\alv\Bureau\programmes\107\00018001
    C:\Documents and Settings\alv\Bureau\programmes\107\324386 en C:\Documents and Settings\alv\Bureau\programmes\107\00014386
    C:\Documents and Settings\alv\Bureau\programmes\107\324442 en C:\Documents and Settings\alv\Bureau\programmes\107\00014442
    C:\Documents and Settings\alv\Bureau\programmes\107\328002 en C:\Documents and Settings\alv\Bureau\programmes\107\00018002
    C:\Documents and Settings\alv\Bureau\programmes\107\328007 en C:\Documents and Settings\alv\Bureau\programmes\107\00018007
    C:\Documents and Settings\alv\Bureau\programmes\107\328008 en C:\Documents and Settings\alv\Bureau\programmes\107\00018008
    C:\Documents and Settings\alv\Bureau\programmes\107\384383 en C:\Documents and Settings\alv\Bureau\programmes\107\00014383
    C:\Documents and Settings\alv\Bureau\programmes\107\384385 en C:\Documents and Settings\alv\Bureau\programmes\107\00014385
    C:\Documents and Settings\alv\Bureau\programmes\107\384386 en C:\Documents and Settings\alv\Bureau\programmes\107\00014386
    C:\Documents and Settings\alv\Bureau\programmes\107\384387 en C:\Documents and Settings\alv\Bureau\programmes\107\00014387
    C:\Documents and Settings\alv\Bureau\programmes\107\388001 en C:\Documents and Settings\alv\Bureau\programmes\107\00018001
    C:\Documents and Settings\alv\Bureau\programmes\107\444383 en C:\Documents and Settings\alv\Bureau\programmes\107\00014383
    C:\Documents and Settings\alv\Bureau\programmes\107\444384 en C:\Documents and Settings\alv\Bureau\programmes\107\00014384
    C:\Documents and Settings\alv\Bureau\programmes\107\448001 en C:\Documents and Settings\alv\Bureau\programmes\107\00018001
    C:\Documents and Settings\alv\Bureau\programmes\107\60341 en C:\Documents and Settings\alv\Bureau\programmes\107\00010341
    C:\Documents and Settings\alv\Bureau\programmes\107\68485 en C:\Documents and Settings\alv\Bureau\programmes\107\00018485
    C:\Documents and Settings\alv\Bureau\programmes\108\1006 en C:\Documents and Settings\alv\Bureau\programmes\108\00011006
    C:\Documents and Settings\alv\Bureau\programmes\108\1027 en C:\Documents and Settings\alv\Bureau\programmes\108\00011027
    C:\Documents and Settings\alv\Bureau\programmes\108\1187 en C:\Documents and Settings\alv\Bureau\programmes\108\00011187
    C:\Documents and Settings\alv\Bureau\programmes\108\1233 en C:\Documents and Settings\alv\Bureau\programmes\108\00011233
    C:\Documents and Settings\alv\Bureau\programmes\108\1276 en C:\Documents and Settings\alv\Bureau\programmes\108\00011276
    C:\Documents and Settings\alv\Bureau\programmes\109\01113 en C:\Documents and Settings\alv\Bureau\programmes\109\00011113
    C:\Documents and Settings\alv\Bureau\programmes\109\1028 en C:\Documents and Settings\alv\Bureau\programmes\109\00011028
    C:\Documents and Settings\alv\Bureau\programmes\109\1232 en C:\Documents and Settings\alv\Bureau\programmes\109\00011232
    C:\Documents and Settings\alv\Bureau\programmes\110\0000 en C:\Documents and Settings\alv\Bureau\programmes\110\00010000
    C:\Documents and Settings\alv\Bureau\programmes\110\06546 en C:\Documents and Settings\alv\Bureau\programmes\110\00016546
    C:\Documents and Settings\alv\Bureau\programmes\110\06558 en C:\Documents and Settings\alv\Bureau\programmes\110\00016558
    C:\Documents and Settings\alv\Bureau\programmes\110\6400 en C:\Documents and Settings\alv\Bureau\programmes\110\00016400
    C:\Documents and Settings\alv\Bureau\programmes\110\6496 en C:\Documents and Settings\alv\Bureau\programmes\110\00016496
    C:\Documents and Settings\alv\Bureau\programmes\110\6499 en C:\Documents and Settings\alv\Bureau\programmes\110\00016499
    C:\Documents and Settings\alv\Bureau\programmes\110\6503 en C:\Documents and Settings\alv\Bureau\programmes\110\00016503
    C:\Documents and Settings\alv\Bureau\programmes\110\6504 en C:\Documents and Settings\alv\Bureau\programmes\110\00016504
    C:\Documents and Settings\alv\Bureau\programmes\110\6505 en C:\Documents and Settings\alv\Bureau\programmes\110\00016505
    C:\Documents and Settings\alv\Bureau\programmes\110\6506 en C:\Documents and Settings\alv\Bureau\programmes\110\00016506
    C:\Documents and Settings\alv\Bureau\programmes\110\6507 en C:\Documents and Settings\alv\Bureau\programmes\110\00016507
    C:\Documents and Settings\alv\Bureau\programmes\110\O6549 en C:\Documents and Settings\alv\Bureau\programmes\110\00016549
    C:\Documents and Settings\alv\Bureau\programmes\110\O6589 en C:\Documents and Settings\alv\Bureau\programmes\110\00016589
    C:\Documents and Settings\alv\Bureau\programmes\122\O1108 en C:\Documents and Settings\alv\Bureau\programmes\122\00011108
    C:\Documents and Settings\alv\Bureau\programmes\122\O1109 en C:\Documents and Settings\alv\Bureau\programmes\122\00011109
    C:\Documents and Settings\alv\Bureau\programmes\122\O3001 en C:\Documents and Settings\alv\Bureau\programmes\122\00013001
    C:\Documents and Settings\alv\Bureau\programmes\122\O3005 en C:\Documents and Settings\alv\Bureau\programmes\122\00013005
    C:\Documents and Settings\alv\Bureau\programmes\129\Archive 02-10-2012\0001 en C:\Documents and Settings\alv\Bureau\programmes\129\Archive 02-10-2012\00010001
    UNE REPONSE UTILE : &|| UN PROBLEME RESOLU :

  10. #10
    Expert éminent

    Inscrit en
    Novembre 2005
    Messages
    5 145
    Détails du profil
    Informations forums :
    Inscription : Novembre 2005
    Messages : 5 145
    Points : 6 911
    Points
    6 911
    Par défaut
    1/ Une cause d'erreur evidente est la tentative de renommage vers un nom qui existe deja.

    2/ Unix definit errno quand rename echoue. Regarde si c'est le cas de Windows, ca donnera une indication. (Utilise perror ou strerror pour avoir un message plus comprehensible qu'un nombre).
    Les MP ne sont pas là pour les questions techniques, les forums sont là pour ça.

  11. #11
    Expert éminent sénior

    Femme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Juin 2007
    Messages
    5 189
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Juin 2007
    Messages : 5 189
    Points : 17 141
    Points
    17 141
    Par défaut
    En fait, je pensais qu'il n'y a pas un besoin de coder, mais juste d'utiliser le programme/script rename, qui contient l'appel à la fonction C.
    Mes principes de bases du codeur qui veut pouvoir dormir:
    • Une variable de moins est une source d'erreur en moins.
    • Un pointeur de moins est une montagne d'erreurs en moins.
    • Un copier-coller, ça doit se justifier... Deux, c'est un de trop.
    • jamais signifie "sauf si j'ai passé trois jours à prouver que je peux".
    • La plus sotte des questions est celle qu'on ne pose pas.
    Pour faire des graphes, essayez yEd.
    le ter nel est le titre porté par un de mes personnages de jeu de rôle

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. fonction intval ne fonctionne pas toujours ?
    Par ninikkhuet dans le forum Langage
    Réponses: 2
    Dernier message: 24/12/2009, 10h23
  2. fonction javascript qui marche pas
    Par iam dans le forum Général JavaScript
    Réponses: 1
    Dernier message: 25/02/2008, 14h46
  3. [Mail] fonction mail qui fonctionne pas
    Par digger dans le forum Langage
    Réponses: 3
    Dernier message: 31/08/2006, 22h28
  4. fonction ridicule qui fonctionne pas sous firefox
    Par Isses dans le forum Général JavaScript
    Réponses: 6
    Dernier message: 27/06/2006, 12h08
  5. fonction plpgsql qui fonctionne pas...
    Par Empty_body dans le forum PostgreSQL
    Réponses: 3
    Dernier message: 15/01/2006, 18h22

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo