Salut tout le monde.!!!!!!
je vous propose ici un code source : "recherche un texte spécifié" dans tous les fichiers texte de votre disque dur, l'utilitaire est Trouve-le ! 1.1il est très facile est utile..
trouvé sur:
[ Lien ]Es-ce-que vous pouvez m'aider à modifier le code source pour avoir un programme qui pointe sur une chaîne dans répertoire donné (fichiers .cpp & .hpp)...merci
*************************************
Trouve le.c
*************************************
#include "stdafx.h"
HINSTANCE hInstance=0;
HWND hWndMain=0;
char TexteRecherche[100];
BOOL bIsSearchText=FALSE;
BOOL bIsSearchLog=FALSE;
BOOL CALLBACK MainProc(HWND hwndDlg,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
char strDrivesList[MAX_PATH];
char strDrive[5];
char strVolume[20];
char strDriveName[30];
int nPos=0;
int nIndex=0;
int nCount=0;
int i=0;
switch (uMsg)
{
case WM_INITDIALOG:
hWndMain=hwndDlg;
SendDlgItemMessage (hwndDlg, IDC_LECTEURS, CB_ADDSTRING, (WPARAM) 0, (LPARAM) (LPCTSTR) "Tous les lecteurs");
CheckDlgButton (hwndDlg, IDC_TEXT,TRUE);
CheckDlgButton (hwndDlg, IDC_LOG,TRUE);
// Recherche la liste des lecteurs disponibles
// GetLogicalDriveStrings renvoie une liste du type A:\[NULL]C:\[NULL]D:\[NULL][NULL]
GetLogicalDriveStrings (sizeof(strDrivesList), strDrivesList);
do
{
wsprintf(strDrive, "%c:\\", strDrivesList[nPos]);
// Obtient nom du volume
ZeroMemory (&strVolume, sizeof(strVolume));
GetVolumeInformation (strDrive, strVolume, sizeof(strVolume), (LPDWORD) NULL, (LPDWORD) NULL, (LPDWORD) NULL, (LPTSTR) NULL, (DWORD) NULL);
// Ajoute ce lecteur dans la liste
wsprintf (strDriveName, "%s [%s]", strDrive, strVolume);
SendDlgItemMessage (hwndDlg, IDC_LECTEURS, CB_ADDSTRING, (WPARAM) 0, (LPARAM) (LPCTSTR) strDriveName);
// Se déplace de 4 caractères pour le lecteur suivant
nPos+=4;
} while (strDrivesList[nPos]!='\0' && strDrivesList[nPos+1]!='\0');
SendDlgItemMessage (hwndDlg, IDC_LECTEURS, CB_SETCURSEL, (WPARAM) 0, (LPARAM) 0);
return TRUE;
case WM_COMMAND:
switch (LOWORD (wParam))
{
case IDC_FERMER:
SendMessage (hwndDlg, WM_CLOSE, (WPARAM) 0, (LPARAM) 0);
return TRUE;
case IDC_APROPOS:
DialogBox (hInstance, MAKEINTRESOURCE (IDD_ABOUT), hwndDlg, AboutProc);
return TRUE;
case IDC_RECHERCHER:
// Obtient texte recherché
GetDlgItemText (hwndDlg, IDC_TEXTE, TexteRecherche, sizeof(TexteRecherche));
if (TexteRecherche[0]=='\0')
{
MessageBox (hwndDlg, "Vous n'avez pas spécifié de texte à rechercher !!!","Trouve le !", MB_ICONSTOP);
return FALSE;
}
// Obtient lecteur sélectionné
nIndex=(int) SendDlgItemMessage (hwndDlg, IDC_LECTEURS, CB_GETCURSEL, (WPARAM) 0, (LPARAM) 0);
// Obtient les filtres (*.txt, ...)
bIsSearchText=IsDlgButtonChecked (hwndDlg, IDC_TEXT);
bIsSearchLog=IsDlgButtonChecked (hwndDlg, IDC_LOG);
// Si aucune filtre sélectionne, abandonne
if (!bIsSearchText && !bIsSearchLog)
{
MessageBox (hwndDlg, "Vous n'avez pas sélectionné de filtre !", "Trouve le !", MB_ICONSTOP);
return TRUE;
}
// Ouvre la fenêtre de recherche
DialogBoxParam (hInstance, MAKEINTRESOURCE(IDD_RESULTS), hwndDlg, ResultsProc, (LPARAM) nIndex);
return TRUE;
}
return FALSE;
case WM_CLOSE:
EndDialog (hwndDlg, IDOK);
return TRUE;
}
return FALSE;
}
int APIENTRY WinMain(HINSTANCE hInst,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
hInstance=hInst;
DialogBox (hInstance, MAKEINTRESOURCE (IDD_MAIN), (HWND) NULL, MainProc);
return 0;
}
*************************************
results.c
*************************************
#include "stdafx.h"
HWND hWndResults;
// Définit si le dossier spécifié est valide
// Indispensable, pour éviter d'avoir les dossier '.' et '..'
// qui ne ferrait retourner aux dossiers précédents
BOOL DossierValide (WIN32_FIND_DATA * Find)
{
BOOL bReturn=TRUE;
if (!(Find->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
bReturn=FALSE;
if (lstrcmp (Find->cFileName, ".")==0)
bReturn=FALSE;
if (lstrcmp (Find->cFileName,"..")==0)
bReturn=FALSE;
return bReturn;
}
void RechercherTexte (char strNomFichier[MAX_PATH*2])
{
char *BufferData;
DWORD ByteRead;
char *pDest;
HANDLE hFile;
LPTSTR Retour=NULL;
DWORD FileSize;
char Extension[4];
UINT i;
UINT nCount=0;
// Vérifie que le fichier réponds au critère (du filtre)
ZeroMemory(&Extension, sizeof(Extension));
for (i=lstrlen(strNomFichier);i>=strlen(strNomFichier)-3;i--,nCount++)
Extension[3-nCount]=strNomFichier[i];
CharLower(Extension);
if (!((lstrcmp(Extension,"txt")==0 && bIsSearchText) || (lstrcmp(Extension,"log")==0 && bIsSearchLog)))
return;
hFile=CreateFile (strNomFichier, GENERIC_READ, FILE_SHARE_READ, (LPSECURITY_ATTRIBUTES) NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
// Ouvre fichier et obtient contenu
FileSize=GetFileSize (hFile, (DWORD) NULL);
if (FileSize==0xFFFFFFFF)
{
CloseHandle (hFile);
return;
}
BufferData = (char*) GlobalAlloc (GMEM_FIXED,FileSize);
ReadFile (hFile, (LPVOID) BufferData, FileSize, &ByteRead, (LPOVERLAPPED) NULL);
if (ByteRead!=FileSize && ByteRead!=0)
return;
// Recherche texte
pDest=strstr (BufferData, TexteRecherche);
if (pDest!=NULL)
{
// Texte trouvé
SendDlgItemMessage (hWndResults, IDC_LISTE, LB_ADDSTRING, (WPARAM) 0, (LPARAM) (LPCTSTR) strNomFichier);
UpdateWindow(hWndResults);
}
GlobalFree ((HGLOBAL) BufferData);
CloseHandle (hFile);
}
// Recherche les fichiers textes dans le dossier spécifié
// et recherche le texte ds chaque fichier trouvé
void RechercherFichiers (char NomDossier[MAX_PATH])
{
WIN32_FIND_DATA Find;
HANDLE hFind;
BOOL Finished=FALSE;
char NomFichier[MAX_PATH*2];
if (SetCurrentDirectory (NomDossier)==FALSE)
return;
// Recherche premier fichier
hFind=FindFirstFile ("*.*", &Find);
if (hFind==INVALID_HANDLE_VALUE)
{
// pas de fichier
Finished=TRUE;
}
else
{
// Fichier trouvé, recherche le texte
wsprintf (NomFichier, "%s%s", NomDossier,Find.cFileName);
RechercherTexte(NomFichier);
}
// Les autres fichiers
while (!Finished)
{
if (!FindNextFile (hFind, &Find))
// Plus de fichier
Finished=TRUE;
else
{
// Fichier trouvé, recherche le texte
wsprintf (NomFichier, "%s%s", NomDossier,Find.cFileName);
RechercherTexte(NomFichier);
}
}
FindClose (hFind);
}
// Recherche les fichiers textes dans le dossier spécifié
// Cette fonction est récursive, elle s'apelle à chaque fois
// pour évoluer dans la hiérarchie des dossiers
void RechercherDossiers (char NomDossier[MAX_PATH])
{
WIN32_FIND_DATA Find;
HANDLE hFind;
BOOL Finished=FALSE;
char Buffer[MAX_PATH*2];
if (SetCurrentDirectory (NomDossier)==FALSE)
return;
if (NomDossier[lstrlen(NomDossier)-1]!='\\')
lstrcat (NomDossier, "\\");
// Recherche les fichiers textes du dossier
RechercherFichiers(NomDossier);
// Recherche premier sous dossier
hFind=FindFirstFile ("*.", &Find);
if (hFind==INVALID_HANDLE_VALUE)
// Pas de dossier
Finished=TRUE;
else
{
// Récupère le nom du dossier et lance la recherche si le dossier est valide
// c'est à dire que c'est bien un dossier, autre que '.' et '..'
wsprintf(Buffer,"%s%s", NomDossier, Find.cFileName);
if (DossierValide(&Find))
RechercherDossiers(Buffer);
}
// Recherche les autres dossiers
while (!Finished)
{
if (!FindNextFile (hFind, &Find))
// Plus de dossier
Finished=TRUE;
else
{
// Récupère le nom du dossier et lance la recherche si le dossier est valide
// c'est à dire que c'est bien un dossier, autre que '.' et '..'
wsprintf(Buffer,"%s%s", NomDossier, Find.cFileName);
if (DossierValide (&Find))
RechercherDossiers(Buffer);
}
}
FindClose (hFind);
}
BOOL CALLBACK ResultsProc(HWND hwndDlg,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
char strDriveSelected[30];
char strNomFichier[MAX_PATH];
int nPos=0;
int nIndex;
int nCount=0;
int i=0;
switch (uMsg)
{
case WM_INITDIALOG:
nIndex=(int) lParam;
hWndResults=hwndDlg;
// Affiche la fenêtre
ShowWindow (hwndDlg, SW_SHOW);
UpdateWindow(hwndDlg);
if (nIndex==0)
{
// Recherche dans tous les lecteurs
nCount=(int) SendDlgItemMessage (hWndMain, IDC_LECTEURS, CB_GETCOUNT, (WPARAM) 0, (LPARAM) 0);
for (i=1;i<nCount;i++)
{
SendDlgItemMessage (hWndMain, IDC_LECTEURS, CB_GETLBTEXT, (WPARAM) i, (LPARAM) (LPCSTR) strDriveSelected);
// On veut que par exemple C:\ et pas C:\ [NOM DU VOLUME]
strDriveSelected[3]='\0';
RechercherDossiers (strDriveSelected);
}
}
else
{
// Recherche dans le lecteur spécifié
SendDlgItemMessage (hWndMain, IDC_LECTEURS, CB_GETLBTEXT, (WPARAM) nIndex, (LPARAM) (LPCSTR) strDriveSelected);
// On veut que par exemple C:\ et pas C:\ [NOM DU VOLUME]
strDriveSelected[3]='\0';
RechercherDossiers (strDriveSelected);
}
SetDlgItemText (hwndDlg, IDC_MESSAGEFIN, "Le programme a trouvé le texte spécifié dans le(s) fichier(s) suivant(s).\n\nSélectionnez un fichier et cliquez sur Ouvrir pour visualiser le fichier.");
return TRUE;
case WM_COMMAND:
switch (LOWORD (wParam))
{
case IDC_LISTE:
if (SendDlgItemMessage (hwndDlg, IDC_LISTE,LB_GETCURSEL,(WPARAM) NULL, (LPARAM) NULL)!=LB_ERR)
EnableWindow (GetDlgItem (hwndDlg, IDC_OUVRIR), TRUE);
return TRUE;
case IDC_FERMER:
SendMessage (hwndDlg, WM_CLOSE, (WPARAM) 0, (LPARAM) 0);
return TRUE;
case IDC_OUVRIR:
nIndex=(int) SendDlgItemMessage (hwndDlg, IDC_LISTE, LB_GETCURSEL, (WPARAM) 0, (LPARAM) 0);
SendDlgItemMessage (hwndDlg, IDC_LISTE, LB_GETTEXT, (WPARAM) nIndex, (LPARAM) (LPCTSTR) strNomFichier);
ShellExecute (hwndDlg, "open", strNomFichier, (LPCTSTR) NULL, (LPCTSTR) NULL, SW_SHOWNORMAL );
return TRUE;
}
return FALSE;
case WM_CLOSE:
EndDialog (hwndDlg, IDOK);
return TRUE;
}
return FALSE;
}