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 :

Détection d'un modification dans un dossier


Sujet :

C++

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Modérateur
    Avatar de toopac
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Juin 2009
    Messages
    940
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : France, Ille et Vilaine (Bretagne)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2009
    Messages : 940
    Par défaut Détection d'un modification dans un dossier
    Bonjour,

    Mes connaissances en C++ étant assez limitées, je me permet de demander votre aide.

    Je souhaite détecter les changements dans un dossier (création, renommage, suppression d'un fichier ou d'un sous dossier).

    Pour cela j'ai trouvé un exemple partiel qui correspond à ce que je veux faire ici : http://msdn.microsoft.com/en-us/libr...8VS.85%29.aspx

    Le code est le suivant :
    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
    #include <windows.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <tchar.h>
     
    void RefreshDirectory(LPTSTR);
    void RefreshTree(LPTSTR);
    void WatchDirectory(LPTSTR);
     
    void _tmain(int argc, TCHAR *argv[])
    {
        if(argc != 2)
        {
            _tprintf(TEXT("Usage: %s <dir>\n"), argv[0]);
            return;
        }
     
        WatchDirectory(argv[1]);
    }
     
    void WatchDirectory(LPTSTR lpDir)
    {
       DWORD dwWaitStatus; 
       HANDLE dwChangeHandles[2]; 
       TCHAR lpDrive[4];
       TCHAR lpFile[_MAX_FNAME];
       TCHAR lpExt[_MAX_EXT];
     
       _tsplitpath_s(lpDir, lpDrive, 4, NULL, 0, lpFile, _MAX_FNAME, lpExt, _MAX_EXT);
     
       lpDrive[2] = (TCHAR)'\\';
       lpDrive[3] = (TCHAR)'\0';
     
    // Watch the directory for file creation and deletion. 
     
       dwChangeHandles[0] = FindFirstChangeNotification( 
          lpDir,                         // directory to watch 
          TRUE,                         // watch subtree 
          FILE_NOTIFY_CHANGE_FILE_NAME); // watch file name changes 
     
       if (dwChangeHandles[0] == INVALID_HANDLE_VALUE) 
       {
         printf("\n ERROR: FindFirstChangeNotification function failed.\n");
         ExitProcess(GetLastError()); 
       }
     
    // Watch the subtree for directory creation and deletion. 
     
       dwChangeHandles[1] = FindFirstChangeNotification( 
          lpDrive,                       // directory to watch 
          TRUE,                          // watch the subtree 
          FILE_NOTIFY_CHANGE_DIR_NAME);  // watch dir name changes 
     
       if (dwChangeHandles[1] == INVALID_HANDLE_VALUE) 
       {
         printf("\n ERROR: FindFirstChangeNotification function failed.\n");
         ExitProcess(GetLastError()); 
       }
     
     
    // Make a final validation check on our handles.
     
       if ((dwChangeHandles[0] == NULL) || (dwChangeHandles[1] == NULL))
       {
         printf("\n ERROR: Unexpected NULL from FindFirstChangeNotification.\n");
         ExitProcess(GetLastError()); 
       }
     
    // Change notification is set. Now wait on both notification 
    // handles and refresh accordingly. 
     
       while (TRUE) 
       { 
       // Wait for notification.
     
          printf("\nWaiting for notification...\n");
     
          dwWaitStatus = WaitForMultipleObjects(2, dwChangeHandles, 
             FALSE, INFINITE); 
     
          switch (dwWaitStatus) 
          { 
             case WAIT_OBJECT_0: 
     
             // A file was created, renamed, or deleted in the directory.
             // Refresh this directory and restart the notification.
     
                 RefreshDirectory(lpDir); 
                 if ( FindNextChangeNotification(dwChangeHandles[0]) == FALSE )
                 {
                   printf("\n ERROR: FindNextChangeNotification function failed.\n");
                   ExitProcess(GetLastError()); 
                 }
                 break; 
     
             case WAIT_OBJECT_0 + 1: 
     
             // A directory was created, renamed, or deleted.
             // Refresh the tree and restart the notification.
     
                 RefreshTree(lpDrive); 
                 if (FindNextChangeNotification(dwChangeHandles[1]) == FALSE )
                 {
                   printf("\n ERROR: FindNextChangeNotification function failed.\n");
                   ExitProcess(GetLastError()); 
                 }
                 break; 
     
             case WAIT_TIMEOUT:
     
             // A timeout occurred, this would happen if some value other 
             // than INFINITE is used in the Wait call and no changes occur.
             // In a single-threaded environment you might not want an
             // INFINITE wait.
     
                printf("\nNo changes in the timeout period.\n");
                break;
     
             default: 
                printf("\n ERROR: Unhandled dwWaitStatus.\n");
                ExitProcess(GetLastError());
                break;
          }
       }
    }
     
    void RefreshDirectory(LPTSTR lpDir)
    {
       // This is where you might place code to refresh your
       // directory listing, but not the subtree because it
       // would not be necessary.
     
       _tprintf(TEXT("Directory (%s) changed.\n"), lpDir);
    }
     
    void RefreshTree(LPTSTR lpDrive)
    {
       // This is where you might place code to refresh your
       // directory listing, including the subtree.
     
       _tprintf(TEXT("Directory tree (%s) changed.\n"), lpDrive);
    }
    Tout fonctionne très bien, je détecte bien les changements.

    Mon soucis, c'est que je ne sais pas de quel changement il s'agit (création? renommage? suppression?) ni sur quel fichier cette modification porte.

    Avez vous-une idée pour récupérer ces informations?

    Merci!

  2. #2
    Modérateur
    Avatar de bruno_pages
    Homme Profil pro
    ingénieur informaticien à la retraite
    Inscrit en
    Juin 2005
    Messages
    3 545
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 65
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : ingénieur informaticien à la retraite
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Juin 2005
    Messages : 3 545
    Par défaut
    Bonjour,

    je ne connais pas les services que vous utilisez et s'il offre le moyen de savoir si vous pouvez différentier les différents cas, mais au pire il vous suffit de mémoriser le contenu du sous arbre de répertoires/fichiers à surveiller puis lorsque quelque chose change de regarder ce qui à bouger, et bien-sûr de mettre à jour votre arbre mémorisé pour la prochaine détection de modification
    Bruno Pagès, auteur de Bouml (freeware), mes tutoriels sur DVP (vieux, non à jour )

    N'oubliez pas de consulter les FAQ UML et les cours et tutoriels UML

  3. #3
    Modérateur
    Avatar de toopac
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Juin 2009
    Messages
    940
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : France, Ille et Vilaine (Bretagne)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2009
    Messages : 940
    Par défaut
    Bonjour, au final j'ai utilisé la classe FileSystemWatcher, et je récupère toutes les infos qui me faut.

    Merci quand même pour votre aide.

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

Discussions similaires

  1. [SP-2010] les modification dans le dossier layout non pris en compte
    Par roubi dans le forum SharePoint
    Réponses: 4
    Dernier message: 31/01/2014, 10h31
  2. Réponses: 16
    Dernier message: 19/12/2011, 17h32
  3. [AIR] Détection de changements dans un dossier
    Par toopac dans le forum Flex
    Réponses: 2
    Dernier message: 21/04/2010, 16h54
  4. Réponses: 5
    Dernier message: 25/02/2008, 17h58

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