Manage many files in FTP ...

Post Reply
User avatar
Rimantas
Posts: 437
Joined: Fri Oct 07, 2005 12:56 pm
Location: Utena , Lithuania

Manage many files in FTP ...

Post by Rimantas »

Hi !

Maybe you can share with some source code , how to manage with some files in FTP site ...

I can connect to needful FTP direcoty :

Code: Select all

hLib := LOADLIB32( "WinInet.dll" )
hInternet := InetOpen( cFsrv, 1, 0, 0, 0 )
hConnect := InetConnect( hInternet, cFsrv, 0, cFusr, cFpsw, 1, 0, 0 )
How to get an array of files in FTP directory ? It's needful to get them to PC and then delete from FTP .

As I can understand , with FTPGetFile( hConnect, cRemote , cLocal , lOvwr, n2 ?, n3 ?, n4 ? ) ( from WinApi/wininet.prg ) I can get file from ftp to local PC . With FTPDeleteFile() , after check in local pc , will delete from FTP ...

But how I can to know how many files are in FTP site ?

Many thanks in advance ! With best regards ! Rimantas
Rimantas U.
User avatar
Enrico Maria Giordano
Posts: 7355
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia
Contact:

Re: Manage many files in FTP ...

Post by Enrico Maria Giordano »

Code: Select all

#include "Fivewin.ch"
#include "Directry.ch"


FUNCTION MAIN()

    LOCAL aDir := FTPDIRECTORY( "/emagsoftware.it/*.asp", "D", "ftp.emagsoftware.it", "userid", "password" )

    LOCAL aNam := {}

    LOCAL i

    SET CENTURY ON
    SET DATE BRITISH

    FOR i = 1 TO LEN( aDir )
        AADD( aNam, aDir[ i, F_NAME ] + CHR( 9 ) + LTRIM( STR( aDir[ i, F_SIZE ] ) ) + CHR( 9 ) + DTOC( aDir[ i, F_DATE ] ) + CHR( 9 ) + aDir[ i, F_TIME ] + CHR( 9 ) + aDir[ i, F_ATTR ] )
    NEXT

    WQOUT( aNam )

    RETURN NIL


// File attributes

#define FILE_ATTRIBUTE_READONLY  1
#define FILE_ATTRIBUTE_HIDDEN    2
#define FILE_ATTRIBUTE_SYSTEM    4
#define FILE_ATTRIBUTE_DIRECTORY 16
#define FILE_ATTRIBUTE_ARCHIVE   32
#define FILE_ATTRIBUTE_NORMAL    128
#define FILE_ATTRIBUTE_TEMPORARY 256


//
// access types for InternetOpen()
//

#define INTERNET_OPEN_TYPE_PRECONFIG                    0   // use registry configuration
#define INTERNET_OPEN_TYPE_DIRECT                       1   // direct to net
#define INTERNET_OPEN_TYPE_PROXY                        3   // via named proxy
#define INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY  4   // prevent using java/script/INS


//
// manifests
//

#define INTERNET_INVALID_PORT_NUMBER    0           // use the protocol-specific default

#define INTERNET_DEFAULT_FTP_PORT       21          // default for FTP servers
#define INTERNET_DEFAULT_GOPHER_PORT    70          //    "     " gopher "
#define INTERNET_DEFAULT_HTTP_PORT      80          //    "     " HTTP   "
#define INTERNET_DEFAULT_HTTPS_PORT     443         //    "     " HTTPS  "
#define INTERNET_DEFAULT_SOCKS_PORT     1080        // default for SOCKS firewall servers.


//
// service types for InternetConnect()
//

#define INTERNET_SERVICE_FTP     1
#define INTERNET_SERVICE_GOPHER  2
#define INTERNET_SERVICE_HTTP    3


//
// flags for FTP
//

#define INTERNET_FLAG_TRANSFER_ASCII  1
#define INTERNET_FLAG_TRANSFER_BINARY 2


#include "Struct.ch"


#define MAX_PATH 260

#define ZTRIM( cString ) Left( cString, At( Chr( 0 ), cString ) - 1 )


FUNCTION FTPDIRECTORY( cFileSpec, cAttrib, cFtp, cUsr, cPsw )

    LOCAL oWin32FindData := OWIN32FINDDATA()

    LOCAL cBuffer

    LOCAL hInternet, hConnect, hFile

    LOCAL aDir := {}

    LOCAL cName, nSize, dDate, cTime, cAttr

    DEFAULT cFileSpec := "*.*"
    DEFAULT cAttrib   := ""

    cBuffer = oWin32FindData:cBuffer

    hInternet = INTERNETOPEN( "Anystring", INTERNET_OPEN_TYPE_DIRECT, 0, 0, 0 )

    hConnect = INTERNETCONNECT( hInternet, cFtp, INTERNET_INVALID_PORT_NUMBER, cUsr, cPsw, INTERNET_SERVICE_FTP, 0, 0 )

    hFile = FTPFINDFIRSTFILE( hConnect, cFileSpec, @cBuffer, 0, 0 )

    oWin32FindData:cBuffer = cBuffer

    IF hFile != 0
        WHILE .T.
            DO CASE
                CASE !( "D" $ cAttrib ) .AND. NAND( oWin32FindData:nFileAttr, FILE_ATTRIBUTE_DIRECTORY ) != 0
                CASE !( "H" $ cAttrib ) .AND. NAND( oWin32FindData:nFileAttr, FILE_ATTRIBUTE_HIDDEN ) != 0
                CASE !( "S" $ cAttrib ) .AND. NAND( oWin32FindData:nFileAttr, FILE_ATTRIBUTE_SYSTEM ) != 0
                OTHERWISE
                    cName = ZTRIM( oWin32FindData:cFileName )
                    nSize = 4294967296 * oWin32FindData:nFileSizeH + oWin32FindData:nFileSizeL
                    dDate = FILEDATE( oWin32FindData:cLastWTime )
                    cTime = FILETIME( oWin32FindData:cLastWTime )
                    cAttr = FILEATTR( oWin32FindData:nFileAttr )

                    AADD( aDir, { cName, nSize, dDate, cTime, cAttr } )
            ENDCASE

            cBuffer = oWin32FindData:cBuffer

            IF !INTERNETFINDNEXTFILE( hFile, cBuffer ); EXIT; ENDIF

            oWin32FindData:cBuffer = cBuffer
        ENDDO

        INTERNETCLOSEHANDLE( hFile )
    ENDIF

    INTERNETCLOSEHANDLE( hConnect )
    INTERNETCLOSEHANDLE( hInternet )

    RETURN aDir


FUNCTION FILEDATE( cFileDtTm )

    LOCAL oSystemTime := OSYSTEMTIME()

    LOCAL cBuffer := oSystemTime:cBuffer

    FTTOSYSTIME( cFileDtTm, @cBuffer )

    oSystemTime:cBuffer = cBuffer

    RETURN STOD( STR( oSystemTime:nYear, 4 ) + STRZERO( oSystemTime:nMonth, 2 ) + STRZERO( oSystemTime:nDay, 2 ) )


FUNCTION FILETIME( cFileDtTm )

    LOCAL oSystemTime := OSYSTEMTIME()

    LOCAL cBuffer := oSystemTime:cBuffer

    FTTOSYSTIME( cFileDtTm, @cBuffer )

    oSystemTime:cBuffer = cBuffer

    RETURN STRZERO( oSystemTime:nHour, 2 ) + ":" + STRZERO( oSystemTime:nMinute, 2 ) + ":" + STRZERO( oSystemTime:nSecond, 2 )


FUNCTION FILEATTR( nAttrib )

    LOCAL cAttrib := ""

    IF NAND( nAttrib, FILE_ATTRIBUTE_ARCHIVE ) != 0; cAttrib += "A"; ENDIF
    IF NAND( nAttrib, FILE_ATTRIBUTE_DIRECTORY ) != 0; cAttrib += "D"; ENDIF
    IF NAND( nAttrib, FILE_ATTRIBUTE_HIDDEN ) != 0; cAttrib += "H"; ENDIF
    IF NAND( nAttrib, FILE_ATTRIBUTE_READONLY ) != 0; cAttrib += "R"; ENDIF
    IF NAND( nAttrib, FILE_ATTRIBUTE_SYSTEM ) != 0; cAttrib += "S"; ENDIF

    RETURN cAttrib


FUNCTION OSYSTEMTIME()

    LOCAL oSystemTime

    STRUCT oSystemTime
        MEMBER nYear      AS WORD
        MEMBER nMonth     AS WORD
        MEMBER nDayOfWeek AS WORD
        MEMBER nDay       AS WORD
        MEMBER nHour      AS WORD
        MEMBER nMinute    AS WORD
        MEMBER nSecond    AS WORD
        MEMBER nMilliSecs AS WORD
    ENDSTRUCT

    RETURN oSystemTime


FUNCTION OWIN32FINDDATA()

    LOCAL oWin32FindData

    STRUCT oWin32FindData
        MEMBER nFileAttr  AS DWORD
        MEMBER cCreatTime AS STRING LEN 8
        MEMBER cLastATime AS STRING LEN 8
        MEMBER cLastWTime AS STRING LEN 8
        MEMBER nFileSizeH AS DWORD
        MEMBER nFileSizeL AS DWORD
        MEMBER nReserved0 AS DWORD
        MEMBER nReserved1 AS DWORD
        MEMBER cFileName  AS STRING LEN MAX_PATH
        MEMBER cShortName AS STRING LEN 14
    ENDSTRUCT

    RETURN oWin32FindData


DLL32 FUNCTION FTTOSYSTIME( cFileTime AS LPSTR, @cSystemTime AS LPSTR ) AS BOOL;
      PASCAL FROM "FileTimeToSystemTime" LIB "kernel32.dll"


#pragma BEGINDUMP

#include "windows.h"
#include "wininet.h"
#include "hbapi.h"


HB_FUNC( INTERNETOPEN )
{
    hb_retnl( ( LONG ) InternetOpen( hb_parc( 1 ), hb_parnl( 2 ), hb_parc( 3 ), hb_parc( 4 ), hb_parnl( 5 ) ) );
}


HB_FUNC( INTERNETCLOSEHANDLE )
{
    hb_retl( InternetCloseHandle( ( HINTERNET ) hb_parnl( 1 ) ) );
}


HB_FUNC( INTERNETCONNECT )
{
    hb_retnl( ( LONG ) InternetConnect( ( HINTERNET ) hb_parnl( 1 ), hb_parc( 2 ), ( INTERNET_PORT ) hb_parnl( 3 ), hb_parc( 4 ), hb_parc( 5 ), hb_parnl( 6 ), hb_parnl( 7 ), hb_parnl( 8 ) ) );
}


HB_FUNC( FTPGETFILE )
{
    hb_retl( FtpGetFile( ( HINTERNET ) hb_parnl( 1 ), hb_parc( 2 ), hb_parc( 3 ), hb_parl( 4 ), hb_parnl( 5 ), hb_parnl( 6 ), hb_parnl( 7 ) ) );
}


HB_FUNC( FTPPUTFILE )
{
    hb_retl( FtpPutFile( ( HINTERNET ) hb_parnl( 1 ), hb_parc( 2 ), hb_parc( 3 ), hb_parnl( 4 ), hb_parnl( 5 ) ) );
}


HB_FUNC( FTPDELETEFILE )
{
    hb_retl( FtpDeleteFile( ( HINTERNET ) hb_parnl( 1 ), hb_parc( 2 ) ) );
}


HB_FUNC( FTPFINDFIRSTFILE )
{
    hb_retnl( ( LONG ) FtpFindFirstFile( ( HINTERNET ) hb_parnl( 1 ), hb_parc( 2 ), ( WIN32_FIND_DATA * ) hb_parc( 3 ), hb_parnl( 4 ), hb_parnl( 5 ) ) );
}


HB_FUNC( INTERNETFINDNEXTFILE )
{
    hb_retl( InternetFindNextFile( ( HINTERNET ) hb_parnl( 1 ), hb_parc( 2 ) ) );
}

#pragma ENDDUMP
EMG
User avatar
Rimantas
Posts: 437
Joined: Fri Oct 07, 2005 12:56 pm
Location: Utena , Lithuania

Re: Manage many files in FTP ...

Post by Rimantas »

Enrico Maria Giordano wrote:

Code: Select all

#include "Fivewin.ch"
#include "Directry.ch"


FUNCTION MAIN()

    LOCAL aDir := FTPDIRECTORY( "/emagsoftware.it/*.asp", "D", "ftp.emagsoftware.it", "userid", "password" )
EMG

Wowww ... !!! :) All in one place that I need ! Many thanks , Enrico !

With best regards ! Rimantas
Rimantas U.
Post Reply