Jump to content
Sign in to follow this  
dedekpredek

reading file from ftp

Recommended Posts

dedekpredek

Hello

I have problem with reading file from ftp. I used loop waiting for input, in this loop is function to process file from ftp. There is only one file at the same moment on ftp. First loop is success, for example file name 123.txt. In second loop is file name 456.txt, but script is trying to open and read 123.txt. Variable $ftpfiles[1] remain the same. Any advice is appreciated.

#include <FTPEx.au3>
func _ftpread()
Local $open = _FTP_Open("shipping")
Local $connect = _FTP_Connect($open, $server, $ftplogin, $ftppswd)
Local $ftpfiles = _FTP_ListToArray($connect, 2)
Local $ftpfileopen = _FTP_FileOpen($connect,$ftpfiles[1])
Local $ftpfile = _FTP_FileRead($ftpfileopen,100000)
Global $convertstring = BinaryToString($ftpfile)
_FTP_FileClose($ftpfileopen)
Local $ftpfiledelete = _FTP_FileDelete($connect,$ftpfiles[1])
_FTP_Close($open)
EndFunc

Thanks

Share this post


Link to post
Share on other sites
kylomas

dedekpredek,

I would add the following lines to your code. The _arraydisplay is just for debugging to verify exactly what is being returned to your array. See comments in code. I would also add error checking where possible, generally after each function call.

#include <FTPEx.au3>
#include <array.au3>
func _ftpread()
Local $open = _FTP_Open("shipping")
Local $connect = _FTP_Connect($open, $server, $ftplogin, $ftppswd)
Local $ftpfiles = _FTP_ListToArray($connect, 2)

_arraydisplay($ftpfiles)                                        ;   <------- add this to see what is in your array

Local $ftpfileopen = _FTP_FileOpen($connect,$ftpfiles[1])
Local $ftpfile = _FTP_FileRead($ftpfileopen,100000)
Global $convertstring = BinaryToString($ftpfile)
_FTP_FileClose($ftpfileopen)
Local $ftpfiledelete = _FTP_FileDelete($connect,$ftpfiles[1])

if $ftpfiledelete = 0 then msgbox($mb_ok,'Delete Error','Delete Failed for file = ' & $ftpfiles[1]) ; <------ and check if the delete failed

_FTP_Close($open)
EndFunc

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
dedekpredek

kylomas,

I posted only short code, already use error check after each function and I also manualy check ftp directory after deleting file. I use msgbox instead of _arraydisplay. After fist loop i get these data - $ftpfiles[0]=1 , $ftpfiles[1]=123.txt ( really on ftp - 123.txt ), after second loop I get this - $ftpfiles[0]=1 , $ftpfiles[1]=123.txt ( really on ftp - 456.txt )

Share this post


Link to post
Share on other sites
kylomas

dedekpredek,

That is my point, I suspect that the delete of the remote file is failing. Putting the _arraydisplay where I have it will show whether or not more than one file exists on the remote site. This is whre I would start debugging.

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
dedekpredek

kylomas

I finished some testing. Deleting of the files work alright. There is problem in using _FTP_ListToArray inside loop. Everything works fine in case of not using loop, var $ftpfiles[1] is null on every new start of script. But in loop remain value of $ftpfiles[1] unchanged from first cycle to next one.

DedekPredek

Share this post


Link to post
Share on other sites
kylomas

dedekpredek,

We cannot see what is going on outside of the function that you posted, most notably the "loop" that you are referring to. You will need to post the COMPLETE code or a problem reproducer.

kylomas

edit: also, what does the _arraydisplay show?

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
dedekpredek

kylomas

I tested it on this code...

#include <Array.au3>
#include <FTPEx.au3>

While 1
$msg = MsgBox(1,"test","Run?") ; <--------- replace input window
if $msg = "2" Then Exit
Local $open = _FTP_Open("shipping")
Local $connect = _FTP_Connect($open, $server, $ftplogin, $ftppswd)
if $connect = 0 Then MsgBox(262192,"Shipping - ERROR","ERROR 1")
Local $ftpfiles = _FTP_ListToArray($connect, 2)
_arraydisplay($ftpfiles)
if $ftpfiles[0] = 0 Then
Sleep(5000)
ContinueLoop
EndIf
Local $ftpfileopen = _FTP_FileOpen($connect,$ftpfiles[1])
if $ftpfileopen = 0 Then MsgBox(262192,"Shipping - ERROR","ERROR 2")
Local $ftpfile = _FTP_FileRead($ftpfileopen,100000)
if $ftpfile = "0" Then MsgBox(262192,"Shipping - ERROR","ERROR 3")
Global $convertstring = BinaryToString($ftpfile)
$ftpfileclose = _FTP_FileClose($ftpfileopen)
if $ftpfileclose = 0 Then MsgBox(262192,"Shipping - ERROR","ERROR 4")
$ftpfiledelete = _FTP_FileDelete($connect,$ftpfiles[1])
if $ftpfiledelete = 0 Then MsgBox(262192,"Shipping - ERROR","ERROR 5")
$ftpclose = _FTP_Close($open)
if $ftpclose = 0 Then MsgBox(262192,"Shipping - ERROR","ERROR 6")
WEnd

_arraydisplay show -

after first loop

[0]|1

[1]|04095013-03_elek_prepravak.lst

after next loop, even though file was deleted successfully at the end of first loop and I get error 2

[0]|1

[1]|04095013-03_elek_prepravak.lst

Share this post


Link to post
Share on other sites
kylomas

dedekpredek,

The code you posted does NOT work on my WEB site either. I looked at _ftp_listtoarray but don't have time to dig through that UDF right now. This is how I manage files from my WEB site.

;
;
;

#cs

Get all KLMS files from samolyk.net

#ce

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <array.au3>
#include <FTPEx.au3>
#include <EditConstants.au3>

If $cmdline[0] <> 0 Then
    If $cmdline[1] = 'quiet' Then
        l(' ')
        l('-----------------------------------------------------------------------------------------------------------------')
        l(' Starting Samolyk.net file download at ' & _now() & ' in batch mode')
        l(' ')
        Local $targ_dir = "k:\KLMS\new\",$file_count = 0, $gui010, $lbl010, $edt010, $lbl015, $edt015, $lbl020, $btn010, $btn015
        get_remote_files()
        exit
    EndIf
endif

Local $targ_dir = "k:\KLMS\OLD\",$file_count = 0

l(' ')
l('-----------------------------------------------------------------------------------------------------------------')
l(' Starting Samolyk.net file download at ' & _now())
l(' ')

Local $gui010 = GUICreate('Samolyk FTP Download for KLMS',1000,700)
                 GUISetFont(10,800,default,'courier new')
Local $lbl010 = GUICtrlCreateLabel('Files Available',50,30,200,20)
Local $edt010 = GUICtrlCreateedit('', 50,50,400,500,$ES_AUTOVSCROLL + $WS_VSCROLL + $ws_hscroll +$es_readonly)
                 GUICtrlSetFont(-1,8.5,800)
Local $lbl015 = GUICtrlCreateLabel('Files Downloaded',550,30,200,20)
Local $edt015 = GUICtrlCreateedit('',550,50,400,500,$ES_AUTOVSCROLL + $WS_VSCROLL + $ws_hscroll +$es_readonly)
                 GUICtrlSetFont(-1,8.5,800)
Local $lbl020 = GUICtrlCreateLabel('',50,650,900,20,$ss_sunken)
Local $btn010 = GUICtrlCreatebutton('Refresh File List',150,570,200,30)
GUICtrlSetFont(-1,16,800,default,"times new roman")
                 GUICtrlSetColor(-1,0x0000ff)
                 GUICtrlSetState($btn010,$gui_disable)
Local $btn015 = GUICtrlCreatebutton('Download Files',650,570,200,30)
GUICtrlSetFont(-1,16,800,default,"times new roman")
                 GUICtrlSetColor(-1,0x0000ff)

                 GUISetState()

display_remote_files()

                 GUICtrlSetState($btn010,$gui_enable)

Local $msg

While 1

    $msg = GUIGetMsg()

    Switch $msg
        Case $gui_event_close
            Exit
        Case $btn010
            display_remote_files()
        Case $btn015
            get_remote_files()
            GUICtrlSetData($lbl020,'FTP-Samolyk Download Results' & ' - ' & $file_count & ' files were downloaded!!')
    endswitch

wend

Func get_remote_files()

    GUICtrlSetData($lbl020,'Begin file download...')
    GUICtrlSetData($edt015,'')

    $file_count = 0

    Local $server       = '***************'
    Local $username     = '********'
    Local $pass         = '********'

    Local $Open = _FTP_Open('Samolyk FTP')
    If $open = 0 Then
        l(' FTP Open Error - Error code = ' & @error & _WinAPI_GetLastErrorMessage())
        Exit
    endif

    Local $Conn = _FTP_Connect($Open, $server, $username, $pass)
    If $conn = 0 Then
        l(' FTP Connect Error - Error code = ' & @error & _WinAPI_GetLastErrorMessage())
        exit
    endif

    Local $hfl
    Local $a_files = _FTP_findfilefirst($conn,"/www/KLMS/",$hfl)
    If @error = -1 Then Return

    getfile($conn,$a_files[10])

    While 1
        $a_files = _ftp_findfilenext($hfl)
        If @error = -1 Then
            ;MsgBox(0,'End of Files',@error & @LF & _WinAPI_GetLastErrorMessage())
            exitloop
        EndIf
        ;ConsoleWrite('return from findfilenext = ' & @error & @lf)
        getfile($conn, $a_files[10])
    wend

    l(' ')
    l(' ' & $file_count & ' files downloaded to ' & $targ_dir)

    _ftp_findfileclose($hfl)

    GUICtrlSetData($lbl020,'Files downloaded to local PC and deleted from remote server...')


    Local $Ftpc = _FTP_Close($Open)
    If $ftpc = 0 Then
        l(' FTP CLose Error = ' & @error & @LF & _WinAPI_GetLastErrorMessage())
        exit
    EndIf

endfunc

Func getfile($conn,$fl)

    Local $getrc
    $getrc = _ftp_fileget($conn, "www/klms/" & $fl, $targ_dir & $fl)
    If $getrc = 1 Then _ftp_filedelete($conn,"www/klms/" & $fl)
    If @error Then
        l(' File Delete Error -- delete for Remote file failed...file = ' & $fl & ' Error code = ' & @error)
        Exit
    endif
    l(' ' & $fl & ' downloaded to ' & $targ_dir & ' and deleted')

    GUICtrlSetData($edt015,$fl & @crlf,1)
    GUICtrlSetData($edt010,StringReplace(guictrlread($edt010),$fl,""))

    $file_count += 1

endfunc

Func l($str)

    Local $lfn = "k:\KLMS\log.txt"
    local $hlog = FileOpen($lfn, 1)
    If $hlog = -1 Then MsgBox(0, 'Log File Open Error', 'File = ' & $lfn)
    FileWrite($hlog, $str & @crlf)
    FileClose($hlog)
    $hlog = 0

EndFunc

Func display_remote_files()

    $file_count = 0

    GUICtrlSetData($lbl020,'Finding remote files...please wait...')
    GUICtrlSetData($edt010,'')

    Local $server       = '*******************'
    Local $username     = '*********'
    Local $pass         = '*********'

    Local $Open = _FTP_Open('Samolyk FTP')
    If $open = 0 Then
        l(' FTP Open Error - Error code = ' & @error & _WinAPI_GetLastErrorMessage())
        Exit
    endif

    Local $Conn = _FTP_Connect($Open, $server, $username, $pass)
    If $conn = 0 Then
        l(' FTP Connect Error - Error code = ' & @error & _WinAPI_GetLastErrorMessage())
        exit
    endif

    Local $hfl
    Local $a_files = _FTP_findfilefirst($conn,"/www/KLMS/",$hfl)
    If @error = -1 Then
        GUICtrlSetData($lbl020,$file_count & ' files available for download...')
        Return
    endif

    GUICtrlSetData($edt010,$a_files[10] & @crlf, 1)

    $file_count += 1

    While 1
        $a_files = _ftp_findfilenext($hfl)
        If @error = -1 Then
            ;MsgBox(0,'End of Files',@error & @LF & _WinAPI_GetLastErrorMessage())
            exitloop
        EndIf
        ;ConsoleWrite('return from findfilenext = ' & @error & @lf)
        GUICtrlSetData($edt010,$a_files[10] & @crlf, 1)
        $file_count += 1
    wend

    GUICtrlSetData($lbl020,$file_count & ' file(s) available for download...hit "DOWNLOAD FILES" to start download...')

    _ftp_findfileclose($hfl)

    Local $Ftpc = _FTP_Close($Open)
    If $ftpc = 0 Then
        l(' FTP CLose Error = ' & @error & @LF & _WinAPI_GetLastErrorMessage())
        exit
    EndIf

endfunc

Hope this helps,

kylomas

edit: note - I do not read the file remotely, I process them locally.

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
kylomas

dedekpredek,

This code works ( added _ftp_dirsetcurrent() ) on my WEB site.

#include <Array.au3>
#include <FTPEx.au3>

While 1
    $msg = MsgBox(1, "test", "Run?") ; <--------- replace input window
    If $msg = "2" Then Exit
    Local $open = _FTP_Open("shipping")
    Local $connect = _FTP_Connect($open, '********', '******', '********')
    If $connect = 0 Then MsgBox(262192, "Shipping - ERROR", "ERROR 1")
    _ftp_dirsetcurrent($connect,"/www/KLMS/")                           ;  <-----  You need to set the current dir
    Local $ftpfiles = _FTP_ListToArray($connect, 0)
    _ArrayDisplay($ftpfiles)
    If $ftpfiles[0] = 0 Then
        Sleep(5000)
        ContinueLoop
    EndIf
    Local $ftpfileopen = _FTP_FileOpen($connect, $ftpfiles[1])
    If $ftpfileopen = 0 Then MsgBox(262192, "Shipping - ERROR", "ERROR 2")
    Local $ftpfile = _FTP_FileRead($ftpfileopen, 100000)
    If $ftpfile = "0" Then MsgBox(262192, "Shipping - ERROR", "ERROR 3")
    Global $convertstring = BinaryToString($ftpfile)
    $ftpfileclose = _FTP_FileClose($ftpfileopen)
    If $ftpfileclose = 0 Then MsgBox(262192, "Shipping - ERROR", "ERROR 4")
    $ftpfiledelete = _FTP_FileDelete($connect, $ftpfiles[1])
    If $ftpfiledelete = 0 Then MsgBox(262192, "Shipping - ERROR", "ERROR 5")
    $ftpclose = _FTP_Close($open)
    If $ftpclose = 0 Then MsgBox(262192, "Shipping - ERROR", "ERROR 6")
WEnd

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
kylomas

dedekpredek,

I was trying to post an update and lost power (we are in the middle of the mother of all snow storms).

The code that I posted lists the correct files but as per your OP there is an issue on successive iterations of the loop. Goofing around with it to see what I can come up with.

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
kylomas

dedekpredek,

Solved the listarray problem. You need to use a flag like this

$ftpfiles = _FTP_ListToArray($connect, 0, $INTERNET_FLAG_RELOAD)  ; <--- need this flag

However, the fileread is failing. You were initially testing the return against a string "0". I changed it to 0 and it is failing. It is returning a -1 (I think that means it is at EOF). Do some testing to see what you come up with.

kylomas

  • Like 1

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
dedekpredek

kylomas,

many thanks to you. I use the ListToArray solution, now it works and does exactly what I want. Also I change error check after reading file to "if @error ....". Now I will test findfilefirst / next, it might help later. This script needs improvement, as well as my scriptwriting skill.

DedekPredek

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×