Jump to content

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

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

"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  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By ozymandius257
      I have written a program to get files from a FTP site using _FTPEx.au3.
      I'm using the _FTP_ListToArray function to get a list of the files, and everything seems OK on 64-bit OS, but it just hangs on a 32-bit OS.
      Specifically, it is working on Windows 10 64-bit, and Windows 7 64-bit - but hanging on Windows 7 32-bit.
      A snippet of the code I'm using is...
      ;read the list of files in the directory Write_To_Log("...getting the list of files on the FTP") Local $returnarray = _FTP_ListToArray($FTPConn, 2) If $returnarray[0] = 0 Then Write_To_Log("...error reading the list of files on the FTP - no files returned") Return False Else ;see if the file was found Local $filefound = False Local $i For $i = 1 To UBound($returnarray) - 1 Write_To_Log("...found the file " & $returnarray[$i]) If $returnarray[$i] = $FTPFileName Then $filefound = True Write_To_Log("...required file found") EndIf Next EndIf Write_To_Log is a function that just writes out to a text file so I can see what's going on - it logs "...getting the list of files on the FTP" and then just hangs.
      Anyone out there can help?
       
    • By Dent
      The following function successfully connects to and uploads a local text file named user.dat
      I have checked the data being written to the text file when it is created locally using a MsgBox and it appears exactly how it should be written to the file. If I comment out the FileDelete and go and open the file locally it is as expected.
      However when I download the file from the FTP server and open it up the text that should be at the end of the file is missing. With each subsequent run more characters are missing.
      I added the Sleep(5000) in case the function was closing the FTP connection too quickly before the file could be fully written but it makes no difference. The user.dat file is (should be) approximately 100 bytes so it is tiny.
      Any idea why this is happening?
      Func UpdateUserData() ; Upload the modified user.dat file Local $hOpen = _FTP_Open("myftp") Local $hConn = _FTP_Connect($hOpen, "my.ftp.server", "user", "pass", 1, 0, 1, 2) If @error Then MsgBox(16, "Error", "Connection failed" & @CRLF & @CRLF & "Please contact support") _FTP_Close($hConn) _FTP_Close($hOpen) Exit EndIf _FTP_FilePut($hConn, @TempDir & "\user.dat", "user.dat") ; Upload the new user.dat file Sleep(5000) If @error <> 0 Then MsgBox(16, "Error", "Couldn't transmit data" & @CRLF & @CRLF & "Please contact support") _FTP_Close($hConn) _FTP_Close($hOpen) Exit EndIf _FTP_Close($hConn) _FTP_Close($hOpen) FileDelete(@TempDir & "\user.dat") EndFunc  
    • By CharlieH
      Hi,
       
      I have written a small script to read a file from an FTP server and check its contents. This can be re-run by the press of a button, however, if the file is deleted from the ftp server between runs (with the .exe still live), the file is still "read" and written locally, which then passes the check.
       
      If I run it initially without the file, it correctly fails and pops up my error message, but if the file is then added, it then seemingly gets cached (or similar) so that the app then always reports a success.
       
      The below code snippet is just the function run when the "test" button is pressed. It includes a load of debug message boxes, and from that I think I've gathered a few (possibly) interesting/relevant things:
      $Open and $Conn are 8 byte values, which increments (not by 1) when the FTP connection is not closed, but if the connection is closed, they stick at the value (presumably windows can re-use that session id if it's been closed e.g:
      1st run (file not present) $Open -> 0x00CC0004 $Conn -> 0x00CC0008
      2nd run (file not present) $Open -> 0x00CC0010 $Conn -> 0x00CC0014
      3rd run (file now present) $Open -> 0x00CC001C $Conn -> 0x00CC0020
      4th run (file still present) $Open -> 0x00CC001C $Conn -> 0x00CC0020
      $Ftp = _FTP_FileGet.... returns a 1 when file not present (in a run after it was present) and "test_transfer.txt" does get created and does contain the correct string This is the key bit I dont understand, I dont know how/where it is getting the data to write this file when it literally no longer exists on the target FTP server...
      resetting every variable used in the function each time it's run does work (in that they become 0), but it doesn't affect the putcome I had thought perhaps some key values were being stored in the variables, but this doesn't seem to be the case
       
       
      Is there any concept of clearing a cache when closing an ftp session? Or deleting any unknown temporary files windows might make?
       
      Thanks all
       
      Func Transfer() Local $connected = 0 $Ftpp = 0 ;Trying to reset these every time function is called $file = 0 $Open = 0 $Conn = 0 $Ftpc = 0 ;Make a new "connecting..." window so that the user has feedback that a transfer is attempting to take place ;Otherwise it just runs in the background and there's no indication its doing anything $connection_window = GUICreate ("Ethernet Switch Test" , 300 , 160 , -1 , -1 , -1 , -1 , 0) GUISetBkColor(0xFFFFFF) GUISetFont(10 * _GDIPlus_GraphicsGetDPIRatio()[0], 400, Default, "Sans Serif") $connecting_label = GUICtrlCreateLabel("Connecting to board...", 0, 25, 300, -1, $SS_Center, "") GUISetState(@SW_SHOW, $connection_window) Sleep(100) ;Connect ;MsgBox(0, "DEBUG", "1" & $Conn) While $connected = 0 $Open = _FTP_Open($count) ;MsgBox(0, "DEBUG", "open " & $Open) $Conn = _FTP_Connect($Open, $server, $username, $password) ;MsgBox(0, "DEBUG", "2" & $Conn) If $Conn = 0 then Local $retry = Msgbox(65, 'FTP Transfer', 'Connection failed' & @CRLF & "Retry?") If $retry = 2 Then MsgBox(0, "FTP Transfer", "Operation aborted") GUISetState(@SW_HIDE, $connection_window) Return EndIf Else $connected_label = GUICtrlCreateLabel("Connected!", 0, 45, 300, -1, $SS_Center, "") $connected = 1 MsgBox(0, "DEBUG", "3" & $connected) Sleep(100) endIf WEnd $transfering_label = GUICtrlCreateLabel("Reading file....", 0, 45, 300, -1, $SS_Center, "") ;Read file from server ;MsgBox(0, "DEBUG", "5" & $Ftpp) ;MsgBox(0, "DEBUG", "flie " & $file) $Ftpp = _FTP_FileGet($Conn, 'test/test.txt', 'test_transfer.txt') ;MsgBox(0, "DEBUG", "6" & $Ftpp) If ($Ftpp) then $transfered_label = GUICtrlCreateLabel("Transfered, checking...", 0, 65, 300, -1, $SS_Center, "") ;MsgBox(0, "DEBUG", "flie " & $file) $file = FileRead("test_transfer.txt") ;MsgBox(0, "DEBUG", "flie " & $file) If Not StringInStr($file, 'this is a test string 12345') Then MsgBox(0, "File check", "Received file incorrect, test failed!") GUISetState(@SW_HIDE, $connection_window) Return Else $tested_label = GUICtrlCreateLabel("Tested and Passed!", 0, 85, 300, -1, $SS_Center, "") $Ftpc = _FTP_Close($Open) ;MsgBox(0, "DEBUG", "close" & $Ftpc) ;$count = $count+1 $ok_button = GUICtrlCreateButton("OK", 125, 105, 50, -1) While 1 Local $pressed = GUIGetMsg() If ($pressed = $ok_button) Then FileDelete("test_transfer.txt") GUISetState(@SW_HIDE, $connection_window) ;$connection_window = 0 Return EndIf WEnd EndIf Else MsgBox(0, "Transfer", "Could not read file " & @error) GUISetState(@SW_HIDE, $connection_window) Return EndIf EndFunc  
    • 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
    • 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
×
×
  • Create New...