sindbad
asked on
Troublein FTPing with CFtpConnection
Hi,
I've developed a class CBatchFtp, as a wrapper for CInternetSession and CFtpConnection. The class has methods to put and get single and multiple files and to check presence of a file in ftp server.
Problem :
A first time call to any of the get/put/seatch methods work fine. Immediately, a call to another method of the CBatchFtp object, fails in CInternetSession->GetFtpCo
Error Description :
CInternetException->GetErr
Given below is the class and its usage.
// CBatchFTP.h
//
#if !defined(CBatchFTP_h)
#define CBatchFTP_h
#include <afxwin.h>
#include <afxinet.h>
class CBatchFTP
{
public:
bool ImportFiles(CString FTPServer, CString UserID, CString Password, CString pszLocalPath, CString pszRemotePath);
bool ExportFiles(CString FTPServer, CString UserID, CString Password, CString pszFileName,CString pszLocalPath,CString pszRemotePath, bool bDeleteFile = true);
bool ExportFiles(CString FTPServer, CString UserID, CString Password, CString pszLocalPath,CString pszRemotePath, bool bDeleteFile = true);
bool SearchFileInServer(CString
CBatchFTP();
CBatchFTP(int PortNumber);
virtual ~CBatchFTP();
protected:
private:
CInternetSession m_InetSession;
CFtpConnection * m_FTPConnection;
int m_PortNumber;
};
#endif /* CBatchFTP_h */
// CBatchFTP.cpp
//
#include "CBatchFTP.h"
bool CBatchFTP::ImportFiles(CString FTPServer, CString UserID, CString Password, CString pszLocalPath, CString pszRemotePath)
{
if (m_FTPConnection)
{
delete m_FTPConnection;
m_FTPConnection = NULL;
}
try
{
m_FTPConnection = m_InetSession.GetFtpConnec
}
catch(CInternetException * eInetException)
{
char ErrMsg[2001];
CString sErrMsg;
memset(ErrMsg,0,2001);
eInetException->GetErrorMe
sErrMsg = ErrMsg;
}
if (m_FTPConnection)
{
CFtpFileFind RemoteFileFind(m_FTPConnec
if (! pszRemotePath.IsEmpty()) // If Remote path is not empty
{
if (FALSE == m_FTPConnection->SetCurren
{
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BU
FORMAT_MESSAGE_FROM_SYSTEM
FORMAT_MESSAGE_IGNORE_INSE
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
CString ErrorMesg;
ErrorMesg.Format("Batch FTP Failed. Error Msg : %s", (LPCTSTR) lpMsgBuf);
//oBatchErr.LogBatchError(
//" ExportFiles");
LocalFree( lpMsgBuf );
return false;
}
}
CString RemoteFileName;
CString LocalFileName;
BOOL bFileFound = RemoteFileFind.FindFile();
while (bFileFound)
{
bFileFound = RemoteFileFind.FindNextFil
if (RemoteFileFind.IsDots() || RemoteFileFind.IsDirectory
continue;
LocalFileName = pszLocalPath;
RemoteFileName = RemoteFileFind.GetFileName
if (LocalFileName[LocalFileNa
{
LocalFileName += "\\";
}
LocalFileName += RemoteFileName;
BOOL bGetDone = m_FTPConnection->GetFile(R
FILE_ATTRIBUTE_ARCHIVE, FTP_TRANSFER_TYPE_ASCII, 1);
if (FALSE == bGetDone)
{
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BU
FORMAT_MESSAGE_FROM_SYSTEM
FORMAT_MESSAGE_IGNORE_INSE
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
CString ErrorMesg;
ErrorMesg.Format("Batch FTP Failed. Error Msg : %s", (LPCTSTR) lpMsgBuf);
//oBatchErr.LogBatchError(
//" ExportFiles");
LocalFree( lpMsgBuf );
}
else
{
BOOL bRemoveDone = m_FTPConnection->Remove(Re
if (FALSE == bRemoveDone)
{
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BU
FORMAT_MESSAGE_FROM_SYSTEM
FORMAT_MESSAGE_IGNORE_INSE
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
CString ErrorMesg;
ErrorMesg.Format("Batch FTP Failed. Error Msg : %s", (LPCTSTR) lpMsgBuf);
//oBatchErr.LogBatchError(
//" ExportFiles");
LocalFree( lpMsgBuf );
}
}
}
}
else
{
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BU
FORMAT_MESSAGE_FROM_SYSTEM
FORMAT_MESSAGE_IGNORE_INSE
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
CString ErrorMesg;
ErrorMesg.Format("Batch FTP Failed. Error Msg : %s", (LPCTSTR) lpMsgBuf);
//oBatchErr.LogBatchError(
//" ExportFiles");
LocalFree( lpMsgBuf );
return false;
}
if (m_FTPConnection)
{
m_FTPConnection->Close();
delete m_FTPConnection;
m_FTPConnection = NULL;
}
m_InetSession.Close();
return true;
}
bool CBatchFTP::ExportFiles(CString FTPServer, CString UserID, CString Password, CString pszFileName, CString pszLocalPath,CString pszRemotePath, bool bDeleteFile)
{
if (m_FTPConnection)
{
delete m_FTPConnection;
m_FTPConnection = NULL;
}
try
{
m_FTPConnection = m_InetSession.GetFtpConnec
LPCTSTR(UserID),
LPCTSTR(Password),
m_PortNumber,1);
}
catch(CInternetException * eInetException)
{
char ErrMsg[2001];
CString sErrMsg;
memset(ErrMsg,0,2001);
eInetException->GetErrorMe
sErrMsg = ErrMsg;
}
if (m_FTPConnection)
{
if (! pszRemotePath.IsEmpty()) // If Remote path is not empty
{
if (FALSE == m_FTPConnection->SetCurren
{
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BU
FORMAT_MESSAGE_FROM_SYSTEM
FORMAT_MESSAGE_IGNORE_INSE
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
CString ErrorMesg;
ErrorMesg.Format("Batch FTP Failed. Error Msg : %s", (LPCTSTR) lpMsgBuf);
//oBatchErr.LogBatchError(
//" ExportFiles");
LocalFree( lpMsgBuf );
return false;
}
}
CString LocalFileName = pszLocalPath;
if (LocalFileName[LocalFileNa
{
LocalFileName += "\\";
}
LocalFileName += pszFileName;
BOOL bGetDone = m_FTPConnection->PutFile(L
if (FALSE == bGetDone)
{
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BU
FORMAT_MESSAGE_FROM_SYSTEM
FORMAT_MESSAGE_IGNORE_INSE
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
CString ErrorMesg;
ErrorMesg.Format("Batch FTP Failed. Error Msg : %s", (LPCTSTR) lpMsgBuf);
//oBatchErr.LogBatchError(
//" ExportFiles");
LocalFree( lpMsgBuf );
return false;
}
else
{
if (bDeleteFile)
{
BOOL bDeleteDone = ::DeleteFile(LocalFileName
if (FALSE == bDeleteDone)
{
CString ErrorMesg;
ErrorMesg.Format("DeleteFi
//oBatchErr.LogBatchError(
//" ExportFiles");
}
}
}
}
else
{
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BU
FORMAT_MESSAGE_FROM_SYSTEM
FORMAT_MESSAGE_IGNORE_INSE
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
CString ErrorMesg;
ErrorMesg.Format("Batch FTP Failed. Error Msg : %s", (LPCTSTR) lpMsgBuf);
//oBatchErr.LogBatchError(
//" ExportFiles");
LocalFree( lpMsgBuf );
return false;
}
if (m_FTPConnection)
{
m_FTPConnection->Close();
delete m_FTPConnection;
m_FTPConnection = NULL;
}
m_InetSession.Close();
return true;
}
bool CBatchFTP::ExportFiles(CString FTPServer, CString UserID, CString Password, CString pszLocalPath,CString pszRemotePath, bool bDeleteFile)
{
if (m_FTPConnection)
{
delete m_FTPConnection;
m_FTPConnection = NULL;
}
try
{
m_FTPConnection = m_InetSession.GetFtpConnec
}
catch(CInternetException * eInetException)
{
char ErrMsg[2001];
CString sErrMsg;
memset(ErrMsg,0,2001);
eInetException->GetErrorMe
sErrMsg = ErrMsg;
}
if (m_FTPConnection)
{
if (! pszRemotePath.IsEmpty()) // If Remote path is not empty
{
if (FALSE == m_FTPConnection->SetCurren
{
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BU
FORMAT_MESSAGE_FROM_SYSTEM
FORMAT_MESSAGE_IGNORE_INSE
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
CString ErrorMesg;
ErrorMesg.Format("Batch FTP Failed. Error Msg : %s", (LPCTSTR) lpMsgBuf);
//oBatchErr.LogBatchError(
//" ExportFiles");
LocalFree( lpMsgBuf );
return false;
}
}
CFileFind LocalFolderSearch;
CString LocalFileName, LocalFolderName = pszLocalPath;
if (LocalFolderName[LocalFold
{
LocalFolderName += "\\";
}
LocalFolderName += "*.*";
BOOL bFileFound = LocalFolderSearch.FindFile
while (bFileFound)
{
bFileFound = LocalFolderSearch.FindNext
LocalFileName = LocalFolderSearch.GetFileP
if (LocalFolderSearch.IsDots(
continue;
BOOL bGetDone = m_FTPConnection->PutFile(L
LocalFolderSearch.GetFileN
FTP_TRANSFER_TYPE_ASCII,1)
if (FALSE == bGetDone)
{
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BU
FORMAT_MESSAGE_FROM_SYSTEM
FORMAT_MESSAGE_IGNORE_INSE
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
CString ErrorMesg;
ErrorMesg.Format("Batch FTP Failed. Error Msg : %s", (LPCTSTR) lpMsgBuf);
//oBatchErr.LogBatchError(
//" ExportFiles");
LocalFree( lpMsgBuf );
return false;
}
else
{
if (bDeleteFile)
{
BOOL bDeleteDone = ::DeleteFile(LocalFileName
if (FALSE == bDeleteDone)
{
CString ErrorMesg;
ErrorMesg.Format("DeleteFi
//oBatchErr.LogBatchError(
//" ExportFiles");
}
}
}
}
}
else
{
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BU
FORMAT_MESSAGE_FROM_SYSTEM
FORMAT_MESSAGE_IGNORE_INSE
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
CString ErrorMesg;
ErrorMesg.Format("Batch FTP Failed. Error Msg : %s", (LPCTSTR) lpMsgBuf);
//oBatchErr.LogBatchError(
//" ExportFiles");
LocalFree( lpMsgBuf );
return false;
}
if (m_FTPConnection)
{
m_FTPConnection->Close();
delete m_FTPConnection;
m_FTPConnection = NULL;
}
m_InetSession.Close();
return true;
}
bool CBatchFTP::SearchFileInServer(CString FTPServer, CString UserID, CString Password,CString pszFileName, CString pszRemotePath)
{
bool bFoundMyFile = false;
if (m_FTPConnection)
{
delete m_FTPConnection;
m_FTPConnection = NULL;
}
try
{
m_FTPConnection = m_InetSession.GetFtpConnec
}
catch(CInternetException * eInetException)
{
char ErrMsg[2001];
CString sErrMsg;
unsigned long ErrCode;
ErrCode = eInetException->m_dwError;
memset(ErrMsg,0,2001);
eInetException->GetErrorMe
sErrMsg = ErrMsg;
}
if (m_FTPConnection)
{
CFtpFileFind RemoteFileFind(m_FTPConnec
if (! pszRemotePath.IsEmpty()) // If Remote path is not empty
{
if (FALSE == m_FTPConnection->SetCurren
{
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BU
FORMAT_MESSAGE_FROM_SYSTEM
FORMAT_MESSAGE_IGNORE_INSE
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
CString ErrorMesg;
ErrorMesg.Format("Batch FTP Failed. Error Msg : %s", (LPCTSTR) lpMsgBuf);
//oBatchErr.LogBatchError(
//" SearchFileInServer");
LocalFree( lpMsgBuf );
return false;
}
}
CString RemoteFileName;
BOOL bAnyFileFound = RemoteFileFind.FindFile();
while (bAnyFileFound)
{
bAnyFileFound = RemoteFileFind.FindNextFil
if (RemoteFileFind.IsDots() || RemoteFileFind.IsDirectory
continue;
RemoteFileName = RemoteFileFind.GetFileName
if (RemoteFileName == pszFileName)
{
bFoundMyFile = true;
break;
}
}
}
else
{
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BU
FORMAT_MESSAGE_FROM_SYSTEM
FORMAT_MESSAGE_IGNORE_INSE
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
CString ErrorMesg;
ErrorMesg.Format("Batch FTP Failed. Error Msg : %s", (LPCTSTR) lpMsgBuf);
//oBatchErr.LogBatchError(
//" SearchFileInServer");
LocalFree( lpMsgBuf );
return false;
}
if (m_FTPConnection)
{
m_FTPConnection->Close();
delete m_FTPConnection;
m_FTPConnection = NULL;
}
m_InetSession.Close();
return bFoundMyFile;
}
CBatchFTP::CBatchFTP() : m_InetSession("My Application",
1,
INTERNET_OPEN_TYPE_PRECONF
NULL,
NULL,
0), m_FTPConnection(NULL),
m_PortNumber(21)
{
}
CBatchFTP::CBatchFTP(int PortNumber) : m_InetSession("My Application",
1,
INTERNET_OPEN_TYPE_PRECONF
NULL,
NULL,
0), m_FTPConnection(NULL),
m_PortNumber(PortNumber)
{
}
CBatchFTP::~CBatchFTP()
{
if (m_FTPConnection)
{
m_FTPConnection->Close();
delete m_FTPConnection;
}
m_InetSession.Close();
}
Call to the CBatchFtp class is as follows.
CBatchFTP MyFtpClient;
bool bWhatHappened = false;
bWhatHappened = batchFTP.ExportFiles(FTPServer, UserID, Pwd, FileName1, LocalPath, RemotePath,false);
if ( bWhatHappened)
{
bWhatHappened = batchFTP.SearchFileInServer(FTPServer, UserID, Pwd, FileName1, RemotePath);
}
Your answer will be of great help.
Sindbad
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yes,
"Not" closing the CInternetSession.Close() at the end of each function, did the trick.
Sindbad
I'm always curious with the criteria people use for grades...why a 'C'?
I will go through ur code & I will try recreate the problem & work that. Pls give me some time.
From
MRN Murthy