Sign in to follow this  
Followers 0
toofat

FTPEx problem

4 posts in this topic

#1 ·  Posted (edited)

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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

"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

Share this post


Link to post
Share on other sites

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 ...

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  
Followers 0

  • Similar Content

    • DagSa
      By DagSa
      Hi,
      I have problem to have my program executed properly in windows 8 or 10. I develop it in XP and first question is, do I have to develop/compile and then run in same  environment to get it right? Compiled files work perfect in all other platforms but not in Windows 8 or 10..
      I use temporary data in subDir in ProgramData and program is working, but not with FTP, it dont resolve DNS for me when connecting. Extended Error Code 12007.
      My compiled program is installed by a "Install Creater Pro" (clickteam.com) in the targetcomputer.
      Computer I test on with Windows 10  is runing in Developer Mode. I tested Sideload mode as well.
      MS Info about Sideload: https://msdn.microsoft.com/library/windows/apps/xaml/dn706236.aspx
      Any help is appriciated,
      GetFTPfile.au3
    • zelles
      By zelles
      Using the FTP FTPEx.au3 makes communicating with FTP a breeze. With great functions like _FTP_FileGet, _FTP_FilePut, and _FTP_DirPutContents, you can upload and download with ease. But what about the much needed ability to download a folder with a simple _FTP_DirGetContents function? Well I put together a function that will download a folder with ease...
      _FTP_DirGetContents Function:
      Func _FTP_DirGetContents($oConn, $opath, $olocal, $orec) If Not FileExists($olocal) Then DirCreate($olocal) If StringRight($opath, 1) <> "/" Then $opath &= "/" If $orec == 1 Then Local $ocurrent = _FTP_DirGetCurrent($oConn) _FTP_DirSetCurrent($oConn, $opath) Local $afolders = _FTP_ListToArray($oConn, 1) _FTP_DirSetCurrent($oConn, $ocurrent) For $o = 1 To $afolders[0] If $afolders[$o] == "." Or $afolders[$o] == ".." Then ContinueLoop If Not FileExists($olocal & "\" & $afolders[$o]) Then DirCreate($olocal & "\" & $afolders[$o]) _FTP_DirGetContents($oConn, $opath & $afolders[$o], $olocal & "\" & $afolders[$o], $orec) Next EndIf Local $hFTPFind Local $aFile = _FTP_FindFileFirst($oConn, $opath, $hFTPFind) While 1 _FTP_FileGet($oConn, $opath & $aFile[10], @ScriptDir & "\tmpdata\" & $aFile[10]) $aFile = _FTP_FindFileNext($hFTPFind) If @error Then ExitLoop WEnd _FTP_FindFileClose($hFTPFind) EndFunc Example Usage:
      #include <FTPEx.au3> If Not FileExist(@ScriptDir & "\ftptemp") Then DirCreate(@ScriptDir & "\ftptemp") $dir_local = @ScriptDir & "\ftptemp" $dir_remote = "/" Local $iOpen = _FTP_Open('MyFTP Control') Local $iConn = _FTP_Connect($iOpen, "YourFTPServer", "YourUsername", "YourPassword") _FTP_DirGetContents($iConn, $dir_remote, $dir_local, 1) _FTP_Close($iConn) _FTP_Close($iOpen) Function Call Explanation:
      _FTP_DirGetContents($oConn, $opath, $olocal, $orec)
      $oConn: session handle as returned by _FTP_Connect. $opath: The remote folder to download $olocal: The local folder to download to. $orec: set to 1 to download folder and subfolders. Set to 0 for non recursive. Enjoy
    • Yaerox
      By Yaerox
      Hey guys, I got a problem and rly need help.
      I try using FTP to get a file. Then the content of the file on the FTP-Server is getting changed, and I'm downloading this file again. This works, but the content of the file after downloading it the second time is the same like before...doesn't matter if I open and close the ftp session, or using the same. If I'm using manually FileZilla to get the File it works without any probs.
      #include <FTPEx.au3>; This line includes all the FTP functions you need. Open this file to see which functions contains exactly. $server = '' $username = '' $pass = '' $Open = _FTP_Open('MyFTP Control') $Conn = _FTP_Connect($Open, $server, $username, $pass) $Ftpp = _FTP_FileGet($Conn, '/EDI-Archiv-CD/bin/Status.txt', 'C:\Users\mwenzel\Documents\proUSBPacker\Customers\Status.txt', False, 128, $FTP_TRANSFER_TYPE_ASCII) ;$FTP_TRANSFER_TYPE_BINARY If @error Then ConsoleWrite("ERROR - " & @error & " - " & @extended & " - " & _WinAPI_GetLastErrorMessage() & @CR) $Ftpc = _FTP_Close($Open) MsgBox(0, "", "") $Open = _FTP_Open('MyFTP Control') $Conn = _FTP_Connect($Open, $server, $username, $pass) ;~ _FTP_FileGet($l_FTPSession, $s_RemoteFile, $s_LocalFile [, $fFailIfExists = False, [$dwFlagsAndAttributes = 0 [, $l_Flags = 0 [, $l_Context = 0]]]]) $Ftpp = _FTP_FileGet($Conn, '/EDI-Archiv-CD/bin/Status.txt', 'C:\Users\mwenzel\Documents\proUSBPacker\Customers\Status.txt', False, 128, $FTP_TRANSFER_TYPE_ASCII) ;$FTP_TRANSFER_TYPE_BINARY If @error Then ConsoleWrite( Please help me out guys. I need to get this finished :/
    • AndyS01
      By AndyS01
      I have code that uses calls to functions in FTPEx.au3 to copy a file to a local directory, but the copy fails.   The steps I took were: _FTP_Open() _FTP_Connect() _FTP_FileGet()   The _FTP_FileGet() returns a 0 (failed) and the file is not copied.  I've tried passing the full FTP pathname and just the base filename, and I've tried inserting a call to _FTP_DirSetCurrent() just before calling _FTP_FileGet(), but nothing worked.   Here is my test code: #include <FTPex.au3> _Main() Func _Main() $ret = copyFile() $err = @error ConsoleWrite("..." & @CRLF) ConsoleWrite("+++: $err = " & $err & ", $ret = " & $ret & @CRLF) EndFunc ;==>_Main Func copyFile() Local $sIP, $sUsername, $sPassword, $FTPDir, $localDir, $fname, $session Local $err, $hConn, $from, $to, $ret $sIP = "xxxxxx" $sUsername = "xxxxx" $sPassword = "xxxxx" $FTPDir = "/Andy" $localDir = "C:\Temp" $fname = "READ2ME.txt" $from = $FTPDir & "/" & $fname $to = $localDir & "\" & $fname $session = open() If ($session == 0) Then Return (0) $hConn = connect($session, $sIP, $sUsername, $sPassword) If ($hConn == 0) Then Return (0) ;$ret = setcurrent($hConn, $FTPDir) ; <-- returns '1' (not a dir name, like the help doc says) ;If ($ret == 0) Then Return (0) $ret = fileget($hConn, $from, $to) If ($ret == 0) Then Return (0) If (FileExists($to)) Then ConsoleWrite("--> File copied OK" & @CRLF) Return (0) Else ConsoleWrite("--> File wasn't copied!!") Return (1) EndIf EndFunc ;==>copyFile Func open() $session = _FTP_Open('_myFTP Control') $err = @error ConsoleWrite("+++: after _FTP_Open(): @error = " & $err & ", $session = " & Hex($session) & @CRLF) If ($err <> 0) Or ($session == 0) Then ConsoleWrite("+++: _FTP_Open() - FAILED" & @CRLF) Return (0) Else ConsoleWrite("+++: _FTP_Open() - OK" & @CRLF) Return ($session) EndIf EndFunc ;==>open Func connect($session, $ip, $uname, $pwd) $hConn = _FTP_Connect($session, $ip, $uname, $pwd) $err = @error ConsoleWrite("+++: after _FTP_Connect(): @error = " & $err & ", $hConn = " & Hex($hConn) & @CRLF) If ($err <> 0) Or ($hConn == 0) Then ConsoleWrite("+++: _FTP_Connect() - FAILED" & @CRLF) Return (0) Else ConsoleWrite("+++: _FTP_Connect() - OK" & @CRLF) Return ($hConn) EndIf EndFunc ;==>connect Func setcurrent($hConn, $fdir) $ret = _FTP_DirSetCurrent($hConn, $fdir) ; Note: returns '1' instead of the Dir name $err = @error ConsoleWrite("+++: after _FTP_DirSetCurrent(): @error = " & $err & ", $ret = " & $ret & @CRLF) If ($err <> 0) Or ($ret == 0) Or ($ret == "") Then ConsoleWrite("+++: _FTP_DirSetCurrent() - FAILED" & @CRLF) Return (0) Else ConsoleWrite("+++: _FTP_DirSetCurrent() - OK" & @CRLF) Return ($ret) EndIf EndFunc ;==>setcurrent Func fileget($hConn, $from, $to) $ret = _FTP_FileGet($hConn, $from, $to, False) $err = @error ConsoleWrite("+++: after _FTP_FileGet(): $ret = " & $ret & ", @error = " & $err & @CRLF) ConsoleWrite("+++: $from = " & $from & ", $to = " & $to & @CRLF) If ($err <> 0) Or ($ret <> 1) Then ConsoleWrite("+++: _FTP_FileGet() - FAILED " & @CRLF) Return (0) Else ConsoleWrite("+++: _FTP_FileGet() - OK" & @CRLF) EndIf EndFunc ;==>fileget Here is the Console output:
      +++: after _FTP_Open(): @error = 0, $session = 00CC0004 +++: _FTP_Open() - OK +++: after _FTP_Connect(): @error = 0, $hConn = 00CC0008 +++: _FTP_Connect() - OK +++: after _FTP_FileGet(): $ret = 0, @error = -1 +++: $from = /Andy/READ2ME.txt, $to = C:\Temp\READ2ME.txt +++: _FTP_FileGet() - FAILED ... +++: $err = 0, $ret = 0 Any help would be appreciated.
      Andy