Erreur dans une fonction maison :(

Résolu
lamoueste Messages postés 76 Date d'inscription lundi 5 septembre 2005 Statut Membre Dernière intervention 3 juillet 2007 - 30 nov. 2005 à 15:54
BruNews Messages postés 21040 Date d'inscription jeudi 23 janvier 2003 Statut Modérateur Dernière intervention 21 août 2019 - 30 nov. 2005 à 17:54
voila mon petit bout de code qui copie tous les fichiers d'un repertoire(c:\t) vers un autre repertoire (c:\Zipper_Vtfw32) temporaire mais j'ai des erreurs lorsqu'il s'agit de fichier qui ne sont pas d'extension txt ou bin... si quelqu'un a une idee pour modifier cette fonction merci d'avance :)

void CTestDlg::OnOK()
{
// TODO: Add extra validation here
MyCopyDirectoryRec("c:\\t");

CDialog::OnOK();
}

void MyCopyDirectoryRec(CString rep)
{
char NewFileNameDebug[]={"c:\\Zipper_Vtfw32\"};
char FileNameDebug[MAX_PATH];
BOOL bErr = false;
unsigned int cpt = 0;
WIN32_FIND_DATA FindFileData;
strcpy(FileNameDebug,rep);
strcat(FileNameDebug,"\\*.*");
HANDLE hFind = FindFirstFile(FileNameDebug, &FindFileData);
if (hFind==INVALID_HANDLE_VALUE)
return;
DWORD a = 0;
while (cpt<5)//(a != ERROR_NO_MORE_FILES)
{
try
{
FindNextFile(hFind, &FindFileData);
}
catch( DWORD a )
{
a = GetLastError();
bErr = true;
}//end catch

if (bErr == false)
{
if (strcmp(FindFileData.cFileName,".")!=0)
{
strcpy(FileNameDebug,rep);
strcat(FileNameDebug,"\");
strcat(FileNameDebug,FindFileData.cFileName);
char NewFileNameDebug[] ={"c:\\Zipper_Vtfw32\"};
strcat(NewFileNameDebug,FindFileData.cFileName);
CopyFile(FileNameDebug,NewFileNameDebug,0);
}
}
if(a == ERROR_NO_MORE_FILES)
break;
cpt++;
}
FindClose(hFind);
}

1 réponse

BruNews Messages postés 21040 Date d'inscription jeudi 23 janvier 2003 Statut Modérateur Dernière intervention 21 août 2019
30 nov. 2005 à 17:54
Aucun besoin de try catch ici, API retourne toujours un code d'erreur mais n'en déclenche jamais, ça ne fait que grossir inutilement ton exe final et surtout le ralentir.


void MyCopyDirectoryRec(char *repsrc)
{
WIN32_FIND_DATA wfd;
HANDLE hfind;
char szsrc[264], *c;
char szdst[264], *d;
DWORD r;
r = strlen(repsrc);
if(strlen(repsrc) >= MAX_PATH) return: // PAS BON
c = szsrc + r; // c POINTE FIN DE DIR DESTINATION
// copie ton chemin dans szsrc
strcpy(szsrc, repsrc);
// ajoute un '\\' si n'y est pas et place pointer c juste derrière ce '\\' if(*(c-1) !'\\') *c++ '\\';
strcpy(c, "*.*");
hfind = FindFirstFile(buf, &wfd);
if(hfind == INVALID_HANDLE_VALUE) return;
strcpy(szdst, "d:\\mondossier\"); // A CHANGER
d = szdst + 14; // A CHANGER PAR LONGUEUR DOSSIER AU DESSUS

do {
if(wfd.cFileName[0] != '.') {
strcpy(c, wfd.cFileName); // FICHIER SOURCE
strcpy(d, wfd.cFileName); // FICHIER DESTINATION
CopyFile(szsrc, szdst, 0);
}
} while(FindNextFile(hfind, &wfd));
FindClose(hfind);
}

ciao...
http://dev.winsysdev.com
BruNews, MVP VC++
3
Rejoignez-nous