Click to See Complete Forum and Search --> : trying to get all files in ftp directory


ih8c
June 20th, 2004, 12:31 PM
allright after a ton of work and a ton of searching on this forum and many others i cant seem to get all of the files in a directory. no matter what i do, i get either JUST the first file found, or i get all files and then when it hits the last one it repeats it infinatly.. im doing something wrong but i dont know what:cry: #include <iostream>
using namespace std;
#include <windows.h>
#include <URLMON.h>//for the urldownload thing
#pragma comment (lib,"URLMON.lib")
#include <WININET.h>
#pragma comment (lib,"WININET.lib")

HINTERNET interneth,ftph,findh;

WIN32_FIND_DATA finddata;

BOOL ismore=TRUE ;

double FtpInit(PCHAR server,PCHAR username,PCHAR password)
{


interneth=InternetOpen("hello this is ftp dir's",INTERNET_OPEN_TYPE_DIRECT ,NULL,NULL,INTERNET_FLAG_ASYNC);

ftph=InternetConnect(interneth,server,INTERNET_DEFAULT_FTP_PORT ,username,password,INTERNET_SERVICE_FTP,0,NULL);
if(!interneth)
{
MessageBox(0,"failed to open internet handle","error",0);
return -1;
}
if(!ftph)
{
MessageBox(0,"failed to open ftp handle","error",0);
return -1;
}

return 0;
}


double FtpDeInit()
{
InternetCloseHandle(interneth);
InternetCloseHandle(ftph);
return 0;
}

/*char next[260]="n";
char last[260]="l" ;*/

/*bool checkvalid()
{
for(int i;i<=260;++i)
{
if(next[i]==last[i])
continue;
else
return false;
}
return true;
}*/

//BOOL ismore=TRUE;

int main()
{
//InternetAttemptConnect(0);
FtpInit(/*ftpserver*/,/*username*/,/*password*/);
/*NOTE THAT YOU HAVE TO USE YOUR OWN FTP SERVER INFO! im not giving out my password/username.*/

findh=FtpFindFirstFile(ftph,"*.html"/*empty string finds first file*/,&finddata,NULL,NULL);
if(!findh)
MessageBox(0,"failed to open search handle","error",0);

while(ismore)
{
ismore=InternetFindNextFile(findh,&finddata);
cout<<ismore<<" "<<finddata.cFileName;
}
/*
LPTSTR next="next";
LPTSTR last="last";
while(GetLastError()!=ERROR_NO_MORE_FILES )
{
//next=last;

//next=NULL;
ismore=InternetFindNextFile(findh,&finddata);
next=finddata.cFileName;
cout<<ismore<<" "<<finddata.cFileName<<" "<<next<<" "<<last<<"\n";

last=next;//get previous value and store it in last
next="zero";
cout<<ismore<<" "<<finddata.cFileName<<" "<<next<<" "<<last<<"\n\n";



Sleep(100);
}
if(finddata.dwFileAttributes==FILE_ATTRIBUTE_DIRECTORY )//if the file is a directory
cout<<"/"<<finddata.cFileName<<"\n";
else // else its a regular file
cout<<finddata.cFileName<<"\n";*/
/*


while(GetLastError()!=ERROR_NO_MORE_FILES)
{

//cout<<finddata.cFileName<<"..1..\n";
if(!ismore)//make sure we found a file otherwise duplicate filenames possible
{
if(finddata.dwFileAttributes!=FILE_ATTRIBUTE_DIRECTORY )//we dont want any directory listings do we?
cout<<finddata.cFileName<<"\n";
else if(finddata.dwFileAttributes==FILE_ATTRIBUTE_DIRECTORY )
cout<<"/"<<finddata.cFileName<<"\n";
}
ismore=InternetFindNextFile(findh,&finddata);
Sleep(100);


}
*/

cout<<"\n\n\n no more files found.\n";

FtpDeInit();
system("pause");
return 0;
}


anyway im kind of out of ideas and i cant see whats different in examples that ive seen.

Yves M
June 20th, 2004, 08:16 PM
[moved thread to appropriate forum]

Yves M
June 20th, 2004, 08:21 PM
Your while condition is a bit iffy. GetLastError may be set for other types of errors than just running out of files to list. I would use the bool return value of InternetFindNextFile to check whether you need to go on or not.

By the way, when you post code, it would be better to leave out the bits that are commented out, unless they are important to understand the problem at hand.

ih8c
June 20th, 2004, 11:18 PM
the reason i posted the code with comments was so everyone could see all i tryed...... the return value of InternetFindNextFile is BOOL , not bool (theres a bit of difference there.....) and you can see that i tryed to use the BOOL return value of the InternetFindNextFile function also, but for some reason its returning FALSE when theres still more files which i dont understand:(