ftp创建并查找文件和文件夹
我想在上传的时候判断是否可以上传到对应的文件夹,我知道 api FtpCreateDirectory,FtpFindFirstFile,InternetFindNextFile。但是没有例子 自己写的也不能创建,我想写win32程序,不要mfc,谢谢
[解决办法]
#pragma once
#include <atlstr.h>
#include <atltime.h>
#include <WinInet.h>
class CFtpFindFile;
class IFtpFileFindCallBack
{
public:
virtual void OnFindFile(const CFtpFindFile &findFile, WPARAM wParam, LPARAM lParam) = 0;
};
//返回TRUE停止, 否则继续
typedef BOOL (WINAPI *FtpEnumCallBack)(const CFtpFindFile&, WPARAM,LPARAM);
class CFtpFindFile
{
public:
CFtpFindFile();
virtual ~CFtpFindFile();
// Attributes
public:
void setConnectHandle(HINTERNET hConnect);
//获取文件长度
ULONGLONG GetLength() const;
//获取文件名; xx.exe
virtual CString GetFileName() const;
//路径 C:\xxxx\xx.exe
virtual CString GetFilePath() const;
//名字 xx 无路径和后缀
virtual CString GetFileTitle() const;
//URL路径 file:///C:\xxx\xx.exe
virtual CString GetFileURL() const;
//文件所在目录 带\;
virtual CString GetRoot() const;
//如果有扩展名, 返回扩展名
virtual CString GetFileExt() const;
virtual BOOL GetLastWriteTime(FILETIME* pTimeStamp) const;
virtual BOOL GetLastAccessTime(FILETIME* pTimeStamp) const;
virtual BOOL GetCreationTime(FILETIME* pTimeStamp) const;
virtual BOOL GetLastWriteTime(CTime& refTime) const;
virtual BOOL GetLastAccessTime(CTime& refTime) const;
virtual BOOL GetCreationTime(CTime& refTime) const;
virtual BOOL MatchesMask(DWORD dwMask) const;
virtual BOOL IsDots() const;
// these aren't virtual because they all use MatchesMask(), which is
BOOL IsReadOnly() const;
BOOL IsDirectory() const;
BOOL IsCompressed() const;
BOOL IsSystem() const;
BOOL IsHidden() const;
BOOL IsTemporary() const;
BOOL IsNormal() const;
BOOL IsArchived() const;
// Operations
void Close();
virtual BOOL FindFile(LPCTSTR pstrName, DWORD dwUnused = 0);
virtual BOOL FindNextFile();
void SetCallback(IFtpFileFindCallBack *pCallback, WPARAM wParam, LPARAM lParam)
{
m_pCallback = pCallback;
m_callBackWParam = wParam;
m_callbackLParam = lParam;
}
//获取指定目录下文件个数. 不包括目录, 只计算文件.
UINT GetFileCount(LPCTSTR strPath, LPCTSTR strFilter = NULL, BOOL bCountSub = FALSE);
BOOL enumFile(FtpEnumCallBack fun, LPCTSTR strPath, BOOL skipDir = TRUE, LPCTSTR strFilter = NULL,
WPARAM wParam =0, LPARAM lParam = 0, BOOL bEnumSub = FALSE);
//递归删除目录, 绝对路径
BOOL delDirectory(LPCTSTR strDir, FtpEnumCallBack callBackFun = NULL, WPARAM wParam = 0, LPARAM lParam = 0);
//删除指定文件, 绝对路径
BOOL delFile(LPCTSTR strFilePath, FtpEnumCallBack callBackFun = NULL, WPARAM wParam = 0, LPARAM lParam = 0);
//只删除当前指定的目录, 如果此目录中有文件或者目录, 会删除失败.
BOOL delDir(LPCTSTR strDir,FtpEnumCallBack callBackFun = NULL, WPARAM wParam = 0, LPARAM lParam = 0);
protected:
WIN32_FIND_DATA m_findData;
HINTERNET m_hFile;
CString m_strPath;
IFtpFileFindCallBack *m_pCallback;
WPARAM m_callBackWParam;
LPARAM m_callbackLParam;
HINTERNET m_hConnect;
};
#include "StdAfx.h"
#include "FtpFindFile.h"
CFtpFindFile::CFtpFindFile(void)
{
memset(&m_findData, 0, sizeof(WIN32_FIND_DATA));
m_hFile = INVALID_HANDLE_VALUE;
m_callbackLParam = NULL;
m_callBackWParam = NULL;
m_pCallback = NULL;
m_hConnect = NULL;
}
CFtpFindFile::~CFtpFindFile(void)
{
Close();
m_hConnect = NULL;
}
ULONGLONG CFtpFindFile::GetLength() const
{
union
{
ULONGLONG ulong;
DWORD dw[2];
} len ;
len.ulong = 0;
if(m_hFile != INVALID_HANDLE_VALUE )
{
len.dw[1] = m_findData.nFileSizeHigh;
len.dw[0] = m_findData.nFileSizeLow;
}
return len.ulong;
}
CString CFtpFindFile::GetFileName() const
{
if(m_hFile != INVALID_HANDLE_VALUE )
{
return CString(m_findData.cFileName);
}
return CString(TEXT(""));
}
CString CFtpFindFile::GetFileExt() const
{
CString Str;
if(m_hFile != INVALID_HANDLE_VALUE )
{
Str = m_findData.cFileName;
int Index = Str.Find('.');
if(Index > 0)
{
Str = Str.Right(Str.GetLength()-Index-1);
Str.MakeUpper();
}
}
return Str;
}
CString CFtpFindFile::GetFilePath() const
{
if(m_hFile != INVALID_HANDLE_VALUE )
{
return m_strPath + m_findData.cFileName;
}
return CString(TEXT(""));
}
CString CFtpFindFile::GetFileTitle() const
{
CString Str;
if(m_hFile != INVALID_HANDLE_VALUE )
{
Str = m_findData.cFileName;
int Index = Str.Find('.');
if(Index > 0)
{
Str = Str.Left(Index);
}
}
return Str;
}
CString CFtpFindFile::GetFileURL() const
{
return TEXT("file://") + GetFilePath();
}
CString CFtpFindFile::GetRoot() const
{
CString Str;
if(m_hFile != INVALID_HANDLE_VALUE )
{
return m_strPath;
}
return Str;
}
BOOL CFtpFindFile::GetLastWriteTime( FILETIME* pTimeStamp ) const
{
if(m_hFile != INVALID_HANDLE_VALUE && pTimeStamp)
{
*pTimeStamp = m_findData.ftLastWriteTime;
return TRUE;
}
return FALSE;
}
BOOL CFtpFindFile::GetLastWriteTime( CTime& refTime ) const
{
if(m_hFile != INVALID_HANDLE_VALUE )
{
refTime = CTime(m_findData.ftLastWriteTime);
return TRUE;
}
return FALSE;
}
BOOL CFtpFindFile::GetLastAccessTime( FILETIME* pTimeStamp ) const
{
if(m_hFile != INVALID_HANDLE_VALUE && pTimeStamp)
{
*pTimeStamp = m_findData.ftLastAccessTime;
return TRUE;
}
return FALSE;
}
BOOL CFtpFindFile::GetLastAccessTime( CTime& refTime ) const
{
if(m_hFile != INVALID_HANDLE_VALUE )
{
refTime = CTime(m_findData.ftLastAccessTime);
return TRUE;
}
return FALSE;
}
BOOL CFtpFindFile::GetCreationTime( FILETIME* pTimeStamp ) const
{
if(m_hFile != INVALID_HANDLE_VALUE && pTimeStamp)
{
*pTimeStamp = m_findData.ftCreationTime;
return TRUE;
}
return FALSE;
}
BOOL CFtpFindFile::GetCreationTime( CTime& refTime ) const
{
if(m_hFile != INVALID_HANDLE_VALUE )
{
refTime = CTime(m_findData.ftCreationTime);
return TRUE;
}
return FALSE;
}
BOOL CFtpFindFile::MatchesMask( DWORD dwMask ) const
{
//DUITRACE(TEXT("FileAttribute = %08X"), m_findData.dwFileAttributes);
return (dwMask & m_findData.dwFileAttributes) == dwMask ;
}
BOOL CFtpFindFile::IsDots() const
{
return _tcscmp(m_findData.cFileName, TEXT("."))==0
[解决办法]
_tcscmp(m_findData.cFileName, TEXT(".."))==0;
}
BOOL CFtpFindFile::IsReadOnly() const
{
return MatchesMask(FILE_ATTRIBUTE_READONLY);
}
BOOL CFtpFindFile::IsDirectory() const
{
return MatchesMask(FILE_ATTRIBUTE_DIRECTORY);
}
BOOL CFtpFindFile::IsCompressed() const
{
return MatchesMask(FILE_ATTRIBUTE_COMPRESSED);
}
BOOL CFtpFindFile::IsSystem() const
{
return MatchesMask(FILE_ATTRIBUTE_SYSTEM);
}
BOOL CFtpFindFile::IsHidden() const
{
return MatchesMask(FILE_ATTRIBUTE_HIDDEN);
}
BOOL CFtpFindFile::IsTemporary() const
{
return MatchesMask(FILE_ATTRIBUTE_TEMPORARY);
}
BOOL CFtpFindFile::IsNormal() const
{
return MatchesMask(FILE_ATTRIBUTE_NORMAL);
}
BOOL CFtpFindFile::IsArchived() const
{
return MatchesMask(FILE_ATTRIBUTE_ARCHIVE);
}
void CFtpFindFile::Close()
{
if(m_hFile)
{
InternetCloseHandle(m_hFile);
}
m_hFile = NULL;
}
BOOL CFtpFindFile::FindFile( LPCTSTR pstrName , DWORD dwUnused /*= 0*/ )
{
if(pstrName == NULL
[解决办法]
m_hConnect == NULL)
{
return FALSE;
}
m_strPath = pstrName;
int Index = m_strPath.ReverseFind('/');
if(Index >= 0)
{
m_strPath = m_strPath.Left(Index+1);
}
m_hFile = FtpFindFirstFile(m_hConnect, pstrName, &m_findData, 0, 0);
if(m_hFile == NULL)
{
DWORD dwErroCode = GetLastError();
DWORD dwInErro = 0;
TCHAR buf[1024] = {0};
DWORD dwLen = 1024;
InternetGetLastResponseInfo(&dwInErro, buf, &dwLen );
CString Str;
Str.Format(TEXT("erroCode = %d inEoor=%d "), dwErroCode, dwInErro);
Str += buf;
Str += TEXT("\r\n");
OutputDebugString(Str);
}
return m_hFile != NULL;
}
BOOL CFtpFindFile::FindNextFile()
{
if(m_hFile == NULL)
{
return FALSE;
}
return ::InternetFindNextFile(m_hFile, &m_findData);
}
UINT CFtpFindFile::GetFileCount( LPCTSTR strPath, LPCTSTR strFilter /*= NULL*/, BOOL bCountSub /*= FALSE*/ )
{
TCHAR filter[0x100] = {0};
if(strFilter)
{
_tcscpy_s(filter, 0x100, strFilter);
_tcsupr_s(filter, 0x100);
}
UINT nCount = 0;
CString path = strPath;
if(path.GetLength() > 0 && path.GetAt(path.GetLength()-1) != '//')
{
path += TEXT("//");
}
if(nCount == 0)
{
path += TEXT("*.*");
}
DWORD fileCount = 0;
HANDLE hOldFile = NULL;
CString strOldPath;
OutputDebugString(path);
if(FindFile(path))
{
while(FindNextFile())
{
if(IsDots())
{
continue;
}
else if(IsDirectory() )
{
if(bCountSub)
{
hOldFile = m_hFile;
strOldPath = m_strPath;
fileCount += GetFileCount(GetFilePath(), strFilter, bCountSub);
m_hFile = hOldFile;
m_strPath = strOldPath;
}
}
else
{
if(strFilter != NULL)//有多个扩展名过虑时, 要判断扩展名, 再统计
{
if(NULL != _tcsstr(filter, GetFileExt()))
{
//OutputDebugString(GetFileName());
++fileCount;
}
}
else //否则全部文件统计.
{
++fileCount;
//OutputDebugString(GetFileName());
}
}
}
}
Close();
return fileCount;
}
BOOL CFtpFindFile::enumFile( FtpEnumCallBack fun, LPCTSTR strPath, BOOL skipDir /*= TRUE*/, LPCTSTR strFilter /*= NULL*/,
WPARAM wParam /*=0*/, LPARAM lParam /*= 0*/, BOOL bEnumSub /*= FALSE*/ )
{
if(fun == NULL
[解决办法]
strPath == NULL)
{
return FALSE;
}
BOOL bBreak = FALSE;
TCHAR filter[0x100] = {0};
if(strFilter)
{
_tcscpy_s(filter, 0x100, strFilter);
_tcsupr_s(filter, 0x100);
}
UINT nCount = 0;
CString path = strPath;
if(path.GetLength() > 0 && path.GetAt(path.GetLength()-1) != '\\')
{
path += TEXT("\");
}
if(nCount == 0)
{
path += TEXT("*.*");
}
DWORD fileCount = 0;
HANDLE hOldFile = NULL;
CString strOldPath;
OutputDebugString(path);
if(FindFile(path))
{
while(FindNextFile())
{
if(IsDots())
{
continue;
}
else if(IsDirectory() )
{
if(skipDir == FALSE)
{
if(TRUE == fun(*this, wParam, lParam))
{
bBreak = 2;
break;
}
}
if(bEnumSub)
{
hOldFile = m_hFile;
strOldPath = m_strPath;
bBreak = enumFile(fun, GetFilePath(), skipDir, strFilter, wParam, lParam, bEnumSub);
m_hFile = hOldFile;
m_strPath = strOldPath;
if(bBreak == 2)
{
break;
}
}
}
else
{
if(strFilter != NULL)//有多个扩展名过虑时, 要判断扩展名, 再统计
{
if(NULL != _tcsstr(filter, GetFileExt()))
{
if(TRUE == fun(*this, wParam, lParam))
{
bBreak = 2;
break;
}
//OutputDebugString(GetFileName());
}
}
else //否则全部文件统计.
{
if(TRUE == fun(*this, wParam,lParam))
{
bBreak = 2;
break;
}
}
}
//Sleep(200);
}
}
Close();
if(bBreak)
{
return bBreak;
}
return TRUE;
}
void CFtpFindFile::setConnectHandle(HINTERNET hConnect)
{
m_hConnect = hConnect;
}
BOOL CFtpFindFile::delDirectory( LPCTSTR strDir, FtpEnumCallBack callBackFun /*= NULL*/,
WPARAM wParam /*= 0*/, LPARAM lParam /*= 0*/ )
{
if(m_hConnect == NULL
[解决办法]
strDir == NULL)
{
return FALSE;
}
BOOL bBreak = FALSE;
UINT nCount = 0;
CString path = strDir;
if(path.GetLength() > 0 && path.GetAt(path.GetLength()-1) != '/')
{
path += TEXT("/");
}
if(nCount == 0)
{
path += TEXT("*.*");
}
DWORD fileCount = 0;
CString strOldPath;
CString strFilePath;
OutputDebugString(path);
if(FindFile(path))
{
while(FindNextFile())
{
if(IsDots())
{
continue;
}
else if(IsDirectory() )
{
strOldPath = m_strPath;
strFilePath = GetFilePath();
//尝试直接删除, 如果删除成功, 则继续, 否则递归.
if(delDir(strFilePath, callBackFun, wParam, lParam))
{
continue;
}
Close();//因为FTP不是同时有两个查找 所以需要关闭后,再递归调用.
bBreak = delDirectory(strFilePath, callBackFun, wParam, lParam);
m_strPath = strOldPath;
if(bBreak == 2)
{
break;
}
//再重新打开当前级目录继续查找.
path = strOldPath;
if(path.GetLength() > 0 && path.GetAt(path.GetLength()-1) != '/')
{
path += TEXT("/");
}
if(nCount == 0)
{
path += TEXT("*.*");
}
//注意, 这里删除文件后查找, 肯定已经没有文件了, 所以查找会失败.
if( FALSE == FindFile(path))
{
break;
}
}
else
{
if(FALSE == delFile(GetFilePath(), callBackFun, wParam, lParam))
{
bBreak = 2;
break;
}
}
//Sleep(200);
}
}
if(bBreak != 2)
{
bBreak = delDir(strDir, callBackFun, wParam, lParam);
}
Close();
return bBreak;
}
BOOL CFtpFindFile::delFile( LPCTSTR strFilePath, FtpEnumCallBack callBackFun /*= NULL*/,
WPARAM wParam /*= 0*/, LPARAM lParam /*= 0*/ )
{
if(m_hConnect)
{
DWORD dwContext = 0;
CString Str = TEXT("DELE ");
Str += strFilePath;
Str += TEXT(" \r\n");
OutputDebugString(Str);
BOOL bOk = ::FtpCommand(m_hConnect, FALSE, FTP_TRANSFER_TYPE_ASCII, Str, dwContext, NULL);
TCHAR buf[1024] = {0};
DWORD bufLen = 1024;
InternetGetLastResponseInfo(&dwContext, buf, &bufLen);
OutputDebugString(buf);
if(bOk)
{
if(m_pCallback)
{
m_pCallback->OnFindFile(*this, m_callBackWParam, m_callbackLParam);
}
if(callBackFun)
{
callBackFun(*this, wParam, lParam);
}
}
return bOk;
}
return FALSE;
}
BOOL CFtpFindFile::delDir( LPCTSTR strDir,FtpEnumCallBack callBackFun /*= NULL*/,
WPARAM wParam /*= 0*/, LPARAM lParam /*= 0*/ )
{
if(m_hConnect)
{
DWORD dwContext = 0;
CString Str = TEXT("RMD ");
Str += strDir;
Str += TEXT(" \r\n");
OutputDebugString(Str);
BOOL bOk = ::FtpCommand(m_hConnect, FALSE, FTP_TRANSFER_TYPE_ASCII, Str, dwContext, NULL);
if(bOk)
{
if(callBackFun)
{
callBackFun(*this, wParam, lParam);
}
if(m_pCallback)
{
m_pCallback->OnFindFile(*this, m_callBackWParam, m_callbackLParam);
}
}
TCHAR buf[1024] = {0};
DWORD bufLen = 1024;
InternetGetLastResponseInfo(&dwContext, buf, &bufLen);
OutputDebugString(buf);
return bOk;
}
return FALSE;
}
// HKFTP.cpp: implementation of the CHKFTP class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "HKFTP.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CHKFTP::CHKFTP()
{
mp_FTPConnect = NULL ;
mp_FTPSession = NULL ;
mp_InetFile = NULL ;
mp_FileFind = NULL;
FTPURL = _T("");
isAnonymous = true;
TRACE0("------------------ HOWARD.KANG FTP Object Created !\n");
}
//////////////////////////////////////////////////////////////////////
CHKFTP::~CHKFTP()
{
CloseFile();
CloseSite();
TRACE0("xxxxxxxxxxxxxxxxxx HOWARD.KANG FTP Object Destroyed !\n");
}
//////////////////////////////////////////////////////////////////////
bool CHKFTP::OpenSite( LPCTSTR pFTPURL , LPCTSTR UserName, LPCTSTR PassWord, unsigned short nPort , BOOL bPassive , LPCTSTR pstrAgent , DWORD dwContext , DWORD dwAccessType , LPCTSTR pstrProxyName , LPCTSTR pstrProxyBypass, DWORD dwFlags )
{
bool x;
unsigned short FtpPort;
DWORD ServerType;
CString FtpSite;
CString FtpObject;
CWaitCursor wcr;
FTPURL = pFTPURL;
OpenSession ( pstrAgent , dwContext , dwAccessType , pstrProxyName , pstrProxyBypass, dwFlags );
x = false;
if ( mp_FTPSession == NULL
[解决办法]
FTPURL.IsEmpty() ) return x;
if (!AfxParseURL(pFTPURL, ServerType, FtpSite , FtpObject, FtpPort))
{
FTPURL = _T("ftp://");
FTPURL += pFTPURL;
if (!AfxParseURL(FTPURL, ServerType, FtpSite, FtpObject, FtpPort)) return x;
}
if ((ServerType == INTERNET_SERVICE_FTP) && !FtpSite.IsEmpty())
{
x = true;
try
{
if ( FtpPort == 0 ) FtpPort = nPort;
mp_FTPConnect = ( isAnonymous ) ? ( mp_FTPSession->GetFtpConnection( LPCTSTR(FtpSite)) ) :
( mp_FTPConnect = mp_FTPSession->GetFtpConnection( LPCTSTR(FtpSite), UserName, PassWord , FtpPort , bPassive ) );
}
catch (CInternetException* pEx)
{
pEx->Delete();
mp_FTPConnect = NULL;
x = false;
}
}
if ( mp_FTPConnect != NULL )
{
mp_FileFind = new CFtpFileFind ( mp_FTPConnect );
if ( !FtpObject.IsEmpty() )
cd ( LPCTSTR ( FtpObject ) );
}
TRACE("FTP Info -> \n\tFTP Site = %s\n\tService Type = %d\n\tPort = %d\n",FtpSite,ServerType,FtpPort);
return x;
}
//////////////////////////////////////////////////////////////////////
void CHKFTP::CloseSite()
{
CWaitCursor wcr;
CloseFile();
if ( mp_FileFind != NULL )
{
delete mp_FileFind;
mp_FileFind = NULL;
}
if ( mp_FTPConnect != NULL )
{
mp_FTPConnect->Close();
delete mp_FTPConnect;
mp_FTPConnect = NULL;
}
if ( mp_FTPSession != NULL )
{
mp_FTPSession->Close();
delete mp_FTPSession;
mp_FTPSession = NULL;
}
::Sleep ( Howard_Delay_Parameter );
}
//////////////////////////////////////////////////////////////////////
bool CHKFTP::OpenFile( LPCTSTR fnm , DWORD dwAccess , DWORD dwFlags , DWORD dwContext )
{
if ( mp_InetFile != NULL ) return false;
CWaitCursor wcr;
mp_InetFile = mp_FTPConnect->OpenFile( fnm , dwAccess , dwFlags , dwContext);
return true;
}
//////////////////////////////////////////////////////////////////////
bool CHKFTP::CloseFile()
{
bool x;
if ( mp_InetFile == NULL ) return true;
x = true;
try
{
mp_InetFile->Flush();
}
catch ( CInternetException * ier )
{
ier->Delete();
x = false;
mp_InetFile->Abort();
}
if ( x ) mp_InetFile->Close();
delete mp_InetFile;
mp_InetFile = NULL;
return x;
}
//////////////////////////////////////////////////////////////////////
bool CHKFTP::FileIsOpen()
{
return (mp_InetFile != NULL);
}
//////////////////////////////////////////////////////////////////////
bool CHKFTP::OpenSession(LPCTSTR pstrAgent, DWORD dwContext , DWORD dwAccessType, LPCTSTR pstrProxyName, LPCTSTR pstrProxyBypass , DWORD dwFlags )
{
bool x;
if ( mp_FTPSession != NULL ) return false;
x = true;
try
{
mp_FTPSession = new CInternetSession ( pstrAgent , dwContext , dwAccessType , pstrProxyName , pstrProxyBypass , dwFlags );
}
catch ( CInternetException * ier )
{
ier->Delete();
mp_FTPSession = NULL;
x = false;
}
return x;
}
//////////////////////////////////////////////////////////////////////
CString CHKFTP::GetDirectory()
{
CString x;
ASSERT ( mp_FTPConnect != NULL );
if ( ! mp_FTPConnect->GetCurrentDirectory ( x ) )
x = _T("");
return x;
}
//////////////////////////////////////////////////////////////////////
bool CHKFTP::cd(LPCTSTR x)
{
bool rv;
ASSERT ( mp_FTPConnect != NULL );
TRACE1("------ FTP ------ 進入遠端目錄 %s\n",x);
rv = ( mp_FTPConnect->SetCurrentDirectory( x ) != 0 );
return rv;
}
//////////////////////////////////////////////////////////////////////
bool CHKFTP::md(LPCTSTR x)
{
bool rv;
ASSERT ( mp_FTPConnect != NULL );
TRACE1("------ FTP ------ 製出遠端目錄 %s\n",x);
rv = ( mp_FTPConnect->CreateDirectory( x ) != 0 );
return rv;
}
//////////////////////////////////////////////////////////////////////
bool CHKFTP::rd(LPCTSTR x)
{
bool rv;
ASSERT ( mp_FTPConnect != NULL );
TRACE1("------ FTP ------ 刪除遠端目錄 %s\n",x);
rv = ( mp_FTPConnect->RemoveDirectory( x ) != 0 );
return rv;
}
//////////////////////////////////////////////////////////////////////
bool CHKFTP::ren(LPCTSTR pold, LPCTSTR pnew)
{
bool rv;
ASSERT ( mp_FTPConnect != NULL );
TRACE2("------ FTP ------ 更換遠端目錄之名稱 [ %s ] 為 [ %s ]\n",pold,pnew);
rv = ( mp_FTPConnect->Rename( pold , pnew ) != 0);
return rv;
}
//////////////////////////////////////////////////////////////////////
bool CHKFTP::del(LPCTSTR x)
{
bool rv;
ASSERT ( mp_FTPConnect != NULL );
TRACE1("------ FTP ------ 刪除遠端檔案 %s\n",x);
rv = ( mp_FTPConnect->Remove( x ) != 0 );
return rv;
}
//////////////////////////////////////////////////////////////////////
bool CHKFTP::Upload(LPCTSTR LocalFile, LPCTSTR RemoteFile, DWORD dwFlags, DWORD dwContext)
{
bool rv;
ASSERT ( mp_FTPConnect != NULL );
CWaitCursor wcr;
TRACE2("------ FTP ------ 上傳檔案 %s 成為遠端 %s\n",LocalFile,RemoteFile);
if ( Howard_Delay_Parameter > 0 ) ::Sleep ( Howard_Delay_Parameter / 2 );
rv = ( mp_FTPConnect->PutFile( LocalFile, RemoteFile, dwFlags , dwContext ) != 0 );
if ( Howard_Delay_Parameter > 0 ) ::Sleep ( Howard_Delay_Parameter / 2 );
return rv;
}
//////////////////////////////////////////////////////////////////////
bool CHKFTP::Download(LPCTSTR RemoteFile, LPCTSTR LocalFile, BOOL bFailIfExists, DWORD dwAttributes, DWORD dwFlags, DWORD dwContext)
{
bool rv;
ASSERT ( mp_FTPConnect != NULL );
CWaitCursor wcr;
TRACE2("------ FTP ------ 下傳遠端檔案 %s 成為 %s\n",RemoteFile,LocalFile);
rv = ( mp_FTPConnect->GetFile( RemoteFile, LocalFile, bFailIfExists, dwAttributes , dwFlags , dwContext ) != 0 );
if ( Howard_Delay_Parameter > 0 ) ::Sleep ( Howard_Delay_Parameter );
return rv;
}
//////////////////////////////////////////////////////////////////////
bool CHKFTP::SetRBufSize(UINT rbsz)
{
ASSERT ( mp_InetFile != NULL );
TRACE1("------ FTP ------ 設定讀取緩衝區大小成為 %d\n",rbsz);
return ( mp_InetFile->SetReadBufferSize( rbsz ) != 0 );
}
//////////////////////////////////////////////////////////////////////
bool CHKFTP::SetWBufSize(UINT wbsz)
{
ASSERT ( mp_InetFile != NULL );
TRACE1("------ FTP ------ 設定寫出緩衝區大小成為 %d\n",wbsz);
return ( mp_InetFile->SetWriteBufferSize( wbsz ) != 0 );
}
//////////////////////////////////////////////////////////////////////
bool CHKFTP::ReadTXT(CString & x)
{
bool rv;
ASSERT ( mp_InetFile != NULL );
CWaitCursor wcr;
rv = true;
try
{
rv = ( mp_InetFile->ReadString( x ) != 0 );
}
catch ( CInternetException * ier )
{
ier->Delete();
mp_InetFile->Abort();
x = _T("");
rv = false;
}
TRACE1("------ FTP ------ 從遠端檔案讀取字串 %s\n",x);
return rv;
}
//////////////////////////////////////////////////////////////////////
bool CHKFTP::WriteTXT(LPCTSTR x)
{
bool rv;
ASSERT ( mp_InetFile != NULL );
CWaitCursor wcr;
rv = true;
try
{
mp_InetFile->WriteString( x );
}
catch ( CInternetException * ier )
{
ier->Delete();
mp_InetFile->Abort();
rv = false;
}
TRACE1("------ FTP ------ 寫出字串到遠端檔案 %s\n",x);
return rv;
}
//////////////////////////////////////////////////////////////////////
UINT CHKFTP::ReadBin(BYTE * buf, UINT bs)
{
UINT rl;
ASSERT ( mp_InetFile != NULL );
CWaitCursor wcr;
try
{
rl = mp_InetFile->Read( (void*) buf, bs );
}
catch ( CInternetException * ier )
{
ier->Delete();
mp_InetFile->Abort();
rl = 0;
}
TRACE0("------ FTP ------ 從遠端檔案讀取2進位檔\n");
return rl;
}
//////////////////////////////////////////////////////////////////////
bool CHKFTP::WriteBIN(BYTE * buf, UINT sz)
{
bool rv;
ASSERT ( mp_InetFile != NULL );
CWaitCursor wcr;
rv = true;
try
{
mp_InetFile->Write( (void*) buf, sz );
}
catch ( CInternetException * ier )
{
ier->Delete();
mp_InetFile->Abort();
rv = false;
}
TRACE0("------ FTP ------ 寫出2進位資料到遠端檔案\n");
return rv;
}
//////////////////////////////////////////////////////////////////////
bool CHKFTP::InitFindFile(LPCTSTR x , DWORD dwFlags )
{
ASSERT ( mp_FileFind != NULL );
CWaitCursor wcr;
TRACE1("------ FTP ------ 在遠端尋找 [ %s ]\n",x);
return ( mp_FileFind->FindFile(x,dwFlags) != 0 );
}
//////////////////////////////////////////////////////////////////////
bool CHKFTP::FindNext( CString & x , bool fnmode )
{
bool rv;
CString y;
ASSERT ( mp_FileFind != NULL );
y = _T("");
CWaitCursor wcr;
rv = ( mp_FileFind->FindNextFile() != 0 );
y = ( fnmode ) ? ( mp_FileFind->GetFileName() ) : ( mp_FileFind->GetFileURL() ) ;
if ( x != y )
x = y;
else
x = _T("");
TRACE1("------ FTP ------ 在遠端尋找符合先前設定條件之檔案 , 找到 [ %s ]\n",x);
return rv;
}
//////////////////////////////////////////////////////////////////////
CFtpFileFind * CHKFTP::TheFind()
{
return mp_FileFind;
}
//////////////////////////////////////////////////////////////////////
CInternetFile * CHKFTP::TheFile()
{
return mp_InetFile;
}
//////////////////////////////////////////////////////////////////////
LPTSTR CHKFTP::ReadTXT(LPTSTR pstr, UINT nMax)
{
ASSERT ( mp_InetFile != NULL );
CWaitCursor wcr;
try
{
mp_InetFile->ReadString( pstr , nMax );
}
catch ( CInternetException * ier )
{
ier->Delete();
mp_InetFile->Abort();
pstr = NULL;
}
TRACE1("------ FTP ------ 從遠端檔案讀取字串 %s\n",pstr);
return pstr;
}
//////////////////////////////////////////////////////////////////////
CString CHKFTP::GetServerName()
{
return mp_FTPConnect->GetServerName( );
}