Jump to content

toofat
 Share

Go to solution Solved by toofat,

Recommended Posts

If somebody could please explain to me why is function "_FTP_FindFileFirst" returning over and over again the same files that I have allready downloaded and deleted, and why does the function "_FTP_FileGet" reports "Succeeded" on downloading non-existing file? Only the function "_FTP_FileDelete" works OK (it reports Failed on all passes except the first).

$iStatus = 0
$sServer = 'ftp.test.com'
$sUsername = 'userftp'
$sPass = 'passftp'
$iInterval = 10

ConsoleWrite ('Opening a new FTP session ... ')
$hOpen = _FTP_Open ('FTP Control ' & @AutoItPID)
If @error Then
 ConsoleWrite ('Failed' & @CRLF)
 Sleep (5000)
 Exit
EndIf
ConsoleWrite ('Succeeded' & @CRLF)

Dim $FTPTime = 1000*$iInterval, $FTPStart = TimerInit (), $FTPEnd = TimerDiff ($FTPStart)

While 'FTP Connection'
 $FTPEnd = TimerDiff ($FTPStart)

 Select
  Case $FTPEnd > $FTPTime
   If NOT $iStatus Then
    ConsoleWrite ('Connecting to FTP server: ftp://' & $sUsername & ':' & $sPass & '@' & $sServer & ' ... ')
    $hConnect = _FTP_Connect ($hOpen, $sServer, $sUsername, $sPass, 1)
    If @error Then
     ConsoleWrite ('Failed' & @CRLF)
     $FTPStart = TimerInit ()
     ContinueLoop
    EndIf
    ConsoleWrite ('Succeeded' & @CRLF)
    ConsoleWrite ('--------------------------------------------------' & @CRLF & @CRLF)
   EndIf
   $iStatus = _FTP_Command ($hConnect, 'STAT')
   If NOT $iStatus Then
    ConsoleWrite ('Connection to FTP server was lost' & @CRLF)
    $FTPStart = TimerInit ()
    ContinueLoop
   EndIf
   Dim $h_Handle
   $aFile = _FTP_FindFileFirst ($hConnect, '/Dir1/Dir2/Dir3/', $h_Handle)
   If NOT @error Then
    While 'FTP Download/Delete'
     Sleep (10)
     ConsoleWrite ('File found: ' & $aFile[10] & @CRLF)
     ConsoleWrite ('Downloading file ... ')
     _FTP_FileGet ($hConnect, '/Dir1/Dir2/Dir3/' & $aFile[10], 'D:\TargetDir\' & $aFile[10], False)
     If @error Then
      ConsoleWrite ('Failed' & @CRLF)
     Else
      ConsoleWrite ('Succeeded' & @CRLF)
      ConsoleWrite ('Deleting file ... ')
      _FTP_FileDelete ($hConnect, '/Dir1/Dir2/Dir3/' & $aFile[10])
      If @error Then
       ConsoleWrite ('Failed' & @CRLF)
      Else
       ConsoleWrite ('Succeeded' & @CRLF)
      EndIf
     EndIf
     ConsoleWrite ('--------------------------------------------------' & @CRLF)
     $aFile = _FTP_FindFileNext ($h_Handle)
     If @error Then ExitLoop
    WEnd
   EndIf
   $aClose = _FTP_FindFileClose ($h_Handle)
   ConsoleWrite ('No new files' & @CRLF)
   $FTPStart = TimerInit ()

 EndSelect
WEnd

$hDisconnect = _FTP_Close ($hConnect)
$hClose = _FTP_Close ($hOpen)

Exit
Edited by toofat
Link to comment
Share on other sites

Hi toofat,

  IMHO  (in my humble opinion) you have a lot going on in this long list of Functions and arguments.

My suggestion would be to clean it up by putting each individual "Job" in a Function and call it accordingly.

This will make it much easier to troubleshoot / error check and even read and use your script.

See the Help File for   Language Reference / User Functions.

Bill

Link to comment
Share on other sites

"I3ill" is this any better ... script is compiled as console app

#include <FTPEx.au3>

$sServer = 'ftp.myfirm.com'
$sUsername = 'UserLogin'
$sPass = 'UserPass'

$hOpen = _FTP_Open ('FTP Control')
$hConnect = _FTP_Connect ($hOpen, $sServer, $sUsername, $sPass, 1)
_FTP_DirSetCurrent ($hConnect, '/Dir1/Dir2')

While 'FTP Connection'
   Dim $h_Handle
   $aFile = _FTP_FindFileFirst ($hConnect, '/Dir1/Dir2/', $h_Handle)
   If NOT @error Then
    While 'FTP Download/Delete'
     Sleep (10)
     ConsoleWrite ('File found: ' & $aFile[10] & @CRLF)
     _FTP_FileGet ($hConnect, '/Dir1/Dir2/' & $aFile[10], 'D:\FTPtarget\' & $aFile[10], False)
     If NOT @error Then
      ConsoleWrite ('Downloaded file ' & $aFile[10] & @CRLF)
      _FTP_FileDelete ($hConnect, '/Dir1/Dir2/' & $aFile[10])
      If NOT @error Then ConsoleWrite ('Deleted file' & @CRLF)
     EndIf
     $aFile = _FTP_FindFileNext ($h_Handle)
     If @error Then ExitLoop
    WEnd
   EndIf
   $aClose = _FTP_FindFileClose ($h_Handle)
   ConsoleWrite ('Waiting 10s' & @CRLF & @CRLF)
   Sleep (10000)
WEnd

$hDisconnect = _FTP_Close ($hConnect)
$hClose = _FTP_Close ($hOpen)

Exit

One more thing. If I delete downloaded file from the Target dir, even thou the file no longer exists on the FTP server (deleted in the first pass), it is successfully downoloaded once again, and again, and again ... I supose from the cache, or ???

And it doesn't help if I do it like this

#include <FTPEx.au3>

$sServer = 'ftp.myfirm.com'
$sUsername = 'UserLogin'
$sPass = 'UserPass'

While 'FTP Connection'
   $hOpen = _FTP_Open ('FTP Control')
   $hConnect = _FTP_Connect ($hOpen, $sServer, $sUsername, $sPass, 1)
   _FTP_DirSetCurrent ($hConnect, '/Dir1/Dir2')
   Dim $h_Handle
   $aFile = _FTP_FindFileFirst ($hConnect, '/Dir1/Dir2/', $h_Handle)
   If NOT @error Then
    While 'FTP Download/Delete'
     Sleep (10)
     ConsoleWrite ('File found: ' & $aFile[10] & @CRLF)
     _FTP_FileGet ($hConnect, '/Dir1/Dir2/' & $aFile[10], 'D:\FTPtarget\' & $aFile[10], False)
     If NOT @error Then
      ConsoleWrite ('Downloaded file ' & $aFile[10] & @CRLF)
      _FTP_FileDelete ($hConnect, '/Dir1/Dir2/' & $aFile[10])
      If NOT @error Then ConsoleWrite ('Deleted file' & @CRLF)
     EndIf
     $aFile = _FTP_FindFileNext ($h_Handle)
     If @error Then ExitLoop
    WEnd
   EndIf
   $aClose = _FTP_FindFileClose ($h_Handle)
   $hDisconnect = _FTP_Close ($hConnect)
   $hClose = _FTP_Close ($hOpen)
   ConsoleWrite ('Waiting 10s' & @CRLF & @CRLF)
   Sleep (10000)
WEnd

Exit
Edited by toofat
Link to comment
Share on other sites

  • Solution

OK got it :thumbsup:

#include <FTPEx.au3>

$sServer = 'ftp.myfirm.com'
$sUsername = 'UserLogin'
$sPass = 'UserPass'

$hOpen = _FTP_Open ('FTP Control')
$hConnect = _FTP_Connect ($hOpen, $sServer, $sUsername, $sPass, 1)
_FTP_DirSetCurrent ($hConnect, '/Dir1/Dir2')

While 'FTP Connection'
   Dim $h_Handle
   $aFile = _FTP_FindFileFirst ($hConnect, '/Dir1/Dir2/', $h_Handle, $INTERNET_FLAG_NO_CACHE_WRITE)
   If NOT @error Then
    While 'FTP Download/Delete'
     Sleep (10)
     ConsoleWrite ('File found: ' & $aFile[10] & @CRLF)
     _FTP_FileGet ($hConnect, '/Dir1/Dir2/' & $aFile[10], 'D:\FTPtarget\' & $aFile[10], False, 0, $INTERNET_FLAG_RELOAD)
     If NOT @error Then
      ConsoleWrite ('Downloaded file ' & $aFile[10] & @CRLF)
      _FTP_FileDelete ($hConnect, '/Dir1/Dir2/' & $aFile[10])
      If NOT @error Then ConsoleWrite ('Deleted file' & @CRLF)
     EndIf
     $aFile = _FTP_FindFileNext ($h_Handle)
     If @error Then ExitLoop
    WEnd
   EndIf
   $aClose = _FTP_FindFileClose ($h_Handle)
   ConsoleWrite ('Waiting 10s' & @CRLF & @CRLF)
   Sleep (10000)
WEnd

$hDisconnect = _FTP_Close ($hConnect)
$hClose = _FTP_Close ($hOpen)

Exit

Added two flags:

1. $INTERNET_FLAG_NO_CACHE_WRITE to "_FTP_FindFileFirst"

2. $INTERNET_FLAG_RELOAD to "_FTP_FileGet"

I should have read the help file better ...

Link to comment
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
 Share

×
×
  • Create New...