C语言自动下载ftp文件程序
2010-01-13 16:22
477 查看
由于经常要到一个固定ftp下载几个文件,不想每次都重复登陆选择,因此用C写了一个简单的ftp自动下载程序。
从FTP 134.101.50.23/Mapper/CDMA/ 目录下下载命名以CDMA开始的文件到本地地址。
源代码如下:
#include <afx.h>
#include <afxwin.h>
#include <afxinet.h>
#include <stdio.h>
CWinApp theApp;
void main()
{
int iFileCnt =0;
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
return;
}
// create a session object to initialize WININET library
CInternetSession sess(_T("MyProgram/1.0"));
CFtpConnection* pConnect = NULL;
try
{
// Request a connection to ftp Default with username = ANONYMOUS
// and password set to the machine name @ domain name
pConnect = sess.GetFtpConnection(_T("134.101.50.23"));
// use a file find object to find files
CFtpFileFind finder(pConnect);
//Set FTP directory
pConnect->SetCurrentDirectory(_T("/Mapper/CDMA"));
CString strDirName;
pConnect->GetCurrentDirectoryAsURL(strDirName);
// printf("GetCurrentDirectoryAsURL is : %s/n",strDirName);
CString strFileName,strObjFile;
//Set the directory where object file is put
CString strObjDir(_T("C:/Documents and Settings/Han Teng/桌面/CDMA map/"));
// Find files start with name CDMA
BOOL bWorking = finder.FindFile(_T("CDMA*"));
BOOL bGetFile;
while (bWorking)
{
bWorking = finder.FindNextFile();
// printf("GetFileURL: %s/n", (LPCTSTR) finder.GetFileURL());
strFileName = finder.GetFileURL();
strFileName = strFileName.Right(strFileName.GetLength()-strDirName.GetLength()-1);
// printf("FileName : %s/n", strFileName);
strObjFile = strObjDir + strFileName;
// printf("ObjFile : %s/n", strObjFile);
//Get file
bGetFile = pConnect->GetFile(strFileName,strObjFile,FALSE,
FILE_ATTRIBUTE_COMPRESSED,FTP_TRANSFER_TYPE_BINARY,1);
if(bGetFile == 0)
{
printf("Not get file! %s/n", strFileName);
}
else
{
printf("Get file done! %s/n",strFileName);
iFileCnt++;
}
}
}
catch (CInternetException* pEx)
{
TCHAR sz[1024];
pEx->GetErrorMessage(sz, 1024);
printf("ERROR! %s/n", sz);
pEx->Delete();
}
printf("%d files is download to local directory!/n",iFileCnt);
// if the connection is open, close it
if (pConnect != NULL)
pConnect->Close();
delete pConnect;
return;
}
从FTP 134.101.50.23/Mapper/CDMA/ 目录下下载命名以CDMA开始的文件到本地地址。
源代码如下:
#include <afx.h>
#include <afxwin.h>
#include <afxinet.h>
#include <stdio.h>
CWinApp theApp;
void main()
{
int iFileCnt =0;
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
return;
}
// create a session object to initialize WININET library
CInternetSession sess(_T("MyProgram/1.0"));
CFtpConnection* pConnect = NULL;
try
{
// Request a connection to ftp Default with username = ANONYMOUS
// and password set to the machine name @ domain name
pConnect = sess.GetFtpConnection(_T("134.101.50.23"));
// use a file find object to find files
CFtpFileFind finder(pConnect);
//Set FTP directory
pConnect->SetCurrentDirectory(_T("/Mapper/CDMA"));
CString strDirName;
pConnect->GetCurrentDirectoryAsURL(strDirName);
// printf("GetCurrentDirectoryAsURL is : %s/n",strDirName);
CString strFileName,strObjFile;
//Set the directory where object file is put
CString strObjDir(_T("C:/Documents and Settings/Han Teng/桌面/CDMA map/"));
// Find files start with name CDMA
BOOL bWorking = finder.FindFile(_T("CDMA*"));
BOOL bGetFile;
while (bWorking)
{
bWorking = finder.FindNextFile();
// printf("GetFileURL: %s/n", (LPCTSTR) finder.GetFileURL());
strFileName = finder.GetFileURL();
strFileName = strFileName.Right(strFileName.GetLength()-strDirName.GetLength()-1);
// printf("FileName : %s/n", strFileName);
strObjFile = strObjDir + strFileName;
// printf("ObjFile : %s/n", strObjFile);
//Get file
bGetFile = pConnect->GetFile(strFileName,strObjFile,FALSE,
FILE_ATTRIBUTE_COMPRESSED,FTP_TRANSFER_TYPE_BINARY,1);
if(bGetFile == 0)
{
printf("Not get file! %s/n", strFileName);
}
else
{
printf("Get file done! %s/n",strFileName);
iFileCnt++;
}
}
}
catch (CInternetException* pEx)
{
TCHAR sz[1024];
pEx->GetErrorMessage(sz, 1024);
printf("ERROR! %s/n", sz);
pEx->Delete();
}
printf("%d files is download to local directory!/n",iFileCnt);
// if the connection is open, close it
if (pConnect != NULL)
pConnect->Close();
delete pConnect;
return;
}
相关文章推荐
- Linux 利用shell中使用ftp自动下载上传文件
- 自动到ftp下载并替换文件内容
- ftp自动上传下载文件脚本
- php实现从ftp服务器上下载文件树到本地电脑的程序
- 从ftp服务器上下载文件树到本地电脑的php程序
- shell脚本,自动登陆ftp,下载文件
- 使用VBS自动删除已经从FTP下载下来的文件
- 利用脚本自动下载ftp列表中的文件
- 使用ftp自动下载上传文件
- 基于TCP网络通信的自动升级程序源码分析-启动升级文件下载程序
- ftp、sftp利用bat脚本自动下载以及上传文件
- 织梦、phpcms程序添加ftp://、thunder://、flashget:文件下载时,出错的修复方法
- Linux FTP自动登陆下载备份,上传文件脚本
- 使用VBS自动删除已经从FTP下载下来的文件
- 用java编写多线程ftp断点下载文件程序
- Linux综合辅导:LinuxShell实现ftp自动上传下载文件
- 使用FTP命令自动完成文件的上传和下载
- linux shell 脚本自动从配置远程ftp服务上下载更新文件
- linux shell实现FTP自动登录批量下载文件
- php实现从ftp服务器上下载文件树到本地电脑的程序