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
| void __fastcall TForm1::DirSearch (char * path_name)
{ //fonction recherche d'un fichier en réseau
WIN32_FIND_DATA FN;//La structure WIN32_FIND_DATA décrit un fichier que
//l'FindFirstFile, FindFirstFileEx, ou les fonctions FindNextFile doivent le trouver.
//l'attribut FN.cFileName (TCHAR cFileName[MAX_PATH];)indique le nom du fichier
HANDLE hFind;
char search_arg[MAX_PATH], new_file_path[MAX_PATH];
sprintf(search_arg, "%s\\*.*", path_name);//Écrit la sortie formatée pour une chaîne.
//search_arg[20]="Fichier.txt";
hFind = FindFirstFile(search_arg, &FN); /*La fonction FindFirstFile cherche
un répertoire pour un fichier ou un répertoire dont en fournit le nom en argument*/
if (hFind != INVALID_HANDLE_VALUE)
{
do {
// Make sure that we don't process . or .. in FN.cFileName here.
if (strcmp(FN.cFileName, ".") != 0 && strcmp(FN.cFileName, "..") != 0)
{
sprintf(new_file_path, "%s\\%s", path_name, FN.cFileName);
// If this is a directory then recurse into the directory
if (FN.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
DirSearch(new_file_path);
else
{
ShowMessage(FN.cFileName);
}
// Do something here with the file
// new_file_path contains the filename with complete path.
}
}
while (FindNextFile(hFind, &FN) != 0);
if (GetLastError() == ERROR_NO_MORE_FILES)
FindClose(&FN);
else
ShowMessage("FindNextFile() crapped on us!");
}
} |
Partager