Jump to content

Recommended Posts



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


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

    ;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)
            $connected_label = GUICtrlCreateLabel("Connected!", 0, 45, 300, -1, $SS_Center, "")
            $connected = 1
            MsgBox(0, "DEBUG", "3" & $connected)

    $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)
            $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
                    GUISetState(@SW_HIDE, $connection_window)
                    ;$connection_window = 0
        MsgBox(0, "Transfer", "Could not read file " & @error)
        GUISetState(@SW_HIDE, $connection_window)



Link to post
Share on other sites
45 minutes ago, Andreik said:

You can use  $INTERNET_FLAG_RELOAD in _FTP_Open() to force the download from the origin server instead from cache.

Many thanks for the suggestion, I've now edited the _FTP_Open call:


However, this hasn't solved the issue I was having... :/

Is the above syntax correct? Can I avoid the extra overflow parameters or do I need all to send the iFlags?

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
  • 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 wysocki
      I have a smartphone and I use it to access my email. However, when composing an email on it I have a problem. My list of phone contacts on the phone is very different from my list of email contacts in my Thunderbird desktop app.  I use my Gmail address book to store primarily phone contacts, and I use Thunderbird for my list of email contacts. I wanted a way to get my Thunderbird contact list onto my smartphone to be able to compose emails to addresses in that list. Here's my solution.
      I wrote a script to export my Thunderbird Personal Address Book to a csv file. It then reads that file and re-writes it with html wrappers around the data to make it into a nicely formatted web page. It then uploads the htm file to my website. On my smartphone, I created a shortcut to the file's URL and whenever I click it, I get the list displayed. Each contact shows name and email address along with a COPY button that will put the address into the clipboard. Then in my email client, I can easily paste that address into it. Alternatively, clicking on the actual email link will open a new message dialog in your email client with that address already entered.
      To use the app, all you need to do is use Thunderbird and have a webserver available. You'll need to download the FTPEX.AU3 file from this website and make a few changes to some constants around line 17 for FTP login info, etc.
    • 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 Raywando
      This is my first post. So I’ve worked on a script for a while and I’m planning to publish it but the problem is that it connects to an FTP server at some point, and as you probably know FTP credentials are easily captured by a MITM attack or Wireshark (not sure if Wireshark does). So I thought if i can detect data capturing in the user’s network the script would stop. Any idea?.
      If there’s another workaround I’m happy to hear it. 
    • By Carm01
      I am attempting to pull a list of the directory structure from a public FTP where no username or password is required i.e:
      Now I have looked all over the place and have failed find anything to accomplish, and if I found some, and the documentation is rather bleak for example;
      it does show something I am looking for, but there is no ftp.au3, and the usage and examples of what i want to do seems to elude me on this and it may not even apply to what I am trying to accomplish?
      I want to avoid using things with Internet explorer , and I have done some google searches. However nothing seems to help
      the documentation surrounding : _FTP_DirGetCurrent  references _FTP_Connect , and then references _FTP_Open , and regardless what I try I cannot get it to pull a list of directorys of files as a list.
      Any help is appreciated
  • Create New...