Monday, February 28, 2011

A simplae FTP client application



/***********************************************************************
Description:Use the following function to transfer all the files from your local drive to ftp server. This code is compiled using VS 2010, VC++ and MFC.

***********************************************************************/




#include "afxinet.h"
#include <windows.h>
#include <iostream>

CString _strServerName;    // your ftp server
CString _strUserName;    // ftp server user name
CString _strPassword;    // ftp server password
CString _strLocalPath;    // local folder path
CString _strRemotePath;    // remote folder path

void StartTransfer()
{
    CInternetSession inetSession;
    CFtpConnection* pFtp = inetSession.GetFtpConnection(_strServerName,_strUserName,_strPassword);
    if(pFtp != NULL)
    {
        cout << "FTP server opened successfully...\n";
    }
    else
    {
        cout << "FTP server could not open...\n";
        return;
    }

    if(!pFtp->SetCurrentDirectory(_strRemotePath))
    {
        cout << "Remote path doesn't exist\n";
        pFtp->Close();
        return;
    }
    CFileFind fileFind;
    if(!SetCurrentDirectory(_strLocalPath))
    {
        cout << "Local path could not find...\n";
        pFtp->Close();
        return;
    }

    BYTE pBuff[1000];
    CString str;
    if(fileFind.FindFile(NULL))
    {
        while(1)
        {
            BOOL bFound = fileFind.FindNextFile();
            CString strFileName = fileFind.GetFileName();
            CFile file;
            if(file.Open(strFileName,CFile::modeRead))
            {
                CInternetFile* pFtpFile = pFtp->OpenFile(strFileName,GENERIC_WRITE);
                UINT uiLength = file.GetLength();
                int nFrameSize = 1000;
                UINT uiTransferred = 0;
                int nReadBytes = 0;
                wcout << (LPCTSTR)(fileFind.GetFileName() + CString(" transfer started..."));
                CONSOLE_SCREEN_BUFFER_INFO ConsoleCursorInfo;
                GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE),&ConsoleCursorInfo);
                CString strPer = L"%";
                while((nReadBytes = file.Read(pBuff,nFrameSize)) == nFrameSize)
                {
                    pFtpFile->Write(pBuff,nFrameSize);
                    uiTransferred += nFrameSize;
                    str.Format(L"%.1f%s",((double)uiTransferred * 100.0) / (double)uiLength,strPer);
                    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),ConsoleCursorInfo.dwCursorPosition);
                    wcout << (LPCTSTR)str;
                }

                if(nReadBytes > 0)
                {
                    pFtpFile->Write(pBuff,nReadBytes);
                }
                uiTransferred += nReadBytes;
                str.Format(L"%.1f%s",100.0,strPer);
                SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),ConsoleCursorInfo.dwCursorPosition);
                wcout << (LPCTSTR)str;

                if(uiTransferred == uiLength)
                {
                    wcout << (LPCTSTR)(CString("\n") + fileFind.GetFileName() + CString(" transfer success\n"));
                }
                else
                {
                    wcout << (LPCTSTR)(CString("\n") + fileFind.GetFileName() + CString(" transfer fail\n"));
                }
                file.Close();
                pFtpFile->Close();
            }
            if(!bFound) break;
        }
    }
    inetSession.Close();
}