Bonjour tlm,
J'ai commencé à écrire un programme qui permet simplement de
lister tous les fichiers contenu sur un FTP. J'ai utilisé la classe
CFile pour écrire dans un *.txt les fichiers contenus sur le FTP, et la
DLL Wininet.dll pour accèder au FTP.
J'ai créé une fonction résurisve pour passer tous les
dossiers du FTP en revue, mais je ne comprends pas pourquoi ça me liste
juste les fichiers et les dossiers contenus à la racine (le programme
lance la fonction récursive une deuxième fois pour les sous-dossiers,
mais il ne trouve aucun dossier et fichier, alors qu'il y en a).
Voici mon code
/////////////////////////////////////////////////////////////////////////////
// The one and only application object
//Header pour la connection FTP
#include <windows.h>
#include <Wininet.h>
#include <string.h>
#pragma comment(lib, "Wininet.lib")
CWinApp theApp;
using namespace std;
void ListElement(CFile* fEcriture, HINTERNET HwndFTP);
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
CString strFichier;
CString strFTP;
CString strLogin;
CString strPassword;
//Charge les strings utilent au programme
strFichier.LoadString(IDS_FICHIER);
strFTP.LoadString(IDS_FTP);
strLogin.LoadString(IDS_LOGIN);
strPassword.LoadString(IDS_PASS);
cout << "Connection en
cours sur le FTP (" << (LPCTSTR)strFTP << ")" << endl;
cout << "Initialisation de la connection" << endl;
HINTERNET HwndInternet =
InternetOpen("Listsafe de fichier",INTERNET_OPEN_TYPE_DIRECT,
(LPCSTR)NULL, (LPCSTR)NULL, 0);
cout << "Connection au serveur" << endl;
HINTERNET HwndFTP =
InternetConnect(HwndInternet,(LPCSTR)strFTP,INTERNET_DEFAULT_FTP_PORT,(LPCSTR)strLogin,
(LPCSTR)strPassword, INTERNET_SERVICE_FTP, NULL, NULL);
cout << "Ouverture du fichier d'enregistrement" << endl;
CFile* fEcriture = new CFile;
if(!fEcriture->Open((LPCSTR)strFichier, CFile::modeCreate|CFile::modeWrite, NULL))
{
cout <<
"Echec lors de la creation du fichier source" << endl;
cin.get();
return 0;
}
//Note retour a la ligne => \r\n
cout << "Elaboration de la liste en cours..." << endl;
ListElement(fEcriture, HwndFTP);
cout << "Fermeture des handles de l application" << endl;
delete fEcriture;
InternetCloseHandle(HwndInternet);
InternetCloseHandle(HwndFTP);
int retour;
cin >> retour;
}
return nRetCode;
}
void ListElement(CFile* fEcriture, HINTERNET HwndFTP)
{
//Cherche le chemin actuel
char* CheRel = new char[255];
DWORD tCheRel = 255;
if(!FtpGetCurrentDirectory(HwndFTP, (LPTSTR)CheRel, &tCheRel))
{
cout << "Une erreur s est produite lors de la reception d un chemin" << endl;
exit(0);
}
//Parcours tous les fichier et dossier
WIN32_FIND_DATA fEnum;
HINTERNET premFichier = FtpFindFirstFile(HwndFTP, 0, &fEnum, 0, 0);
//Recupere le premier fichier
//Creation du buffer
char* nomFichier = new char[255];
nomFichier = strcat(fEnum.cFileName, "\r\n");
fEcriture->Write(nomFichier, strlen(nomFichier));
while(InternetFindNextFile(premFichier, &fEnum) != FALSE)
{
//Doit tester et prendre que les dossiers
cout << fEnum.cFileName << endl;
if (!(fEnum.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
//Creation du buffer
char* nomFichier = new char[255];
nomFichier = strcat(fEnum.cFileName, "\r\n");
//strcat(nomFichier, "\r\n");
fEcriture->Write(nomFichier, strlen(nomFichier));
}else{
FtpSetCurrentDirectory(HwndFTP,fEnum.cFileName);
ListElement(fEcriture, HwndFTP);
FtpSetCurrentDirectory(HwndFTP,"..");
}
}
}
Es-ce que quelqu'un aurait une idée pour savoir ou ça marche pas (j'ai créé cette appli avec VC++6, avec la MFC et en console).
Merci d'avance