Jump to content
akira2891

FTP get data in GUI list

Recommended Posts

akira2891

Hi, i have problem to get all files from server in list, it say in help file that u can get data from current session but it wont work

 

#RequireAdmin
#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GUIListBox.au3>
#include <GUIListView.au3>
#include <FTPEx.au3>
#include <Array.au3>

#Region ### START Koda GUI section ###
$GUI = GUICreate("FTP tool", 754, 613, 691, 157)
GUICtrlCreateLabel("Host", 16, 16, 26, 17)
GUICtrlCreateLabel("Port", 192, 16, 23, 17)
GUICtrlCreateLabel("Username", 16, 56, 52, 17)
GUICtrlCreateLabel("Password", 16, 88, 50, 17)
GUICtrlCreateLabel("Status : ", 8, 152, 43, 17)
$status = GUICtrlCreateLabel("Not connected", 52, 152, 221, 17)
$host = GUICtrlCreateInput("", 48, 12, 121, 21)
$port = GUICtrlCreateCombo("", 224, 12, 65, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL))
GUICtrlSetData(-1, "21|80")
$username = GUICtrlCreateInput("", 79, 52, 121, 21)
$password = GUICtrlCreateInput("", 79, 81, 121, 21, BitOR($GUI_SS_DEFAULT_INPUT,$ES_PASSWORD))
$connectBtn = GUICtrlCreateButton("CONNECT", 56, 192, 75, 25)
$disconnectBtn = GUICtrlCreateButton("DISCONNECT", 143, 192, 107, 25)
$List = GUICtrlCreateList("", 16, 240, 721, 340, 0)

GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

If GUICtrlRead($status) = 'Not connected' Then GUICtrlSetState($disconnectBtn, $GUI_DISABLE) ; disable disconnect button if not connected
GUICtrlSetData($port, '21') ; default port

Global $status, $host, $port, $username, $password, $FTPopen, $FTPConnect, $List

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $connectBtn
            _connectFTP()
        Case $disconnectBtn
            _disconnectFTP()

    EndSwitch
WEnd

; connection to FTP server
Func _connectFTP()

    Local $_host = GUICtrlRead($host) ; host name
    Local $_username = GUICtrlRead($username) ; username
    Local $_password = GUICtrlRead($password) ; password
    Local $_port = GUICtrlRead($port) ; port

    Local $FTPopen = _FTP_Open('FTP connection test') ; name of connection
    Local $FTPConnect = _FTP_Connect($FTPopen, $_host, $_username, $_password, '', $_port) ; connect

        ; try to connect to server
        If Not @error Then ; if not error
            GUICtrlSetData($status, 'Connected') ; set status to connected
            GUICtrlSetState($connectBtn, $GUI_DISABLE) ; disable connect button
            GUICtrlSetState($disconnectBtn, $GUI_ENABLE) ; enable disconnect button
            _listData()
        Else
            MsgBox(48, 'FTP connection error', "Can't connect to server, error = " & @error) ; if there is connection error
        EndIf

EndFunc

; disconnect from FTP server
Func _disconnectFTP()
    
    _FTP_Close($FTPopen)
    _FTP_Close($FTPConnect)
    GUICtrlSetState($disconnectBtn, $GUI_DISABLE)
    GUICtrlSetState($connectBtn, $GUI_ENABLE)
    GUICtrlSetData($status, 'Not connected')
    _GUICtrlListBox_ResetContent($List)
    
EndFunc

; list all files on FTP server
Func _listData()

    ; get current dir on server
    $FTPdirData = _FTP_DirGetCurrent($FTPopen)

    ; Add files in list box
    _GUICtrlListBox_BeginUpdate($List)
    _GUICtrlListBox_ResetContent($List)
    _GUICtrlListBox_Dir($List, "", $FTPdirData, False)
    _GUICtrlListBox_EndUpdate($List)


EndFunc

 

Edited by akira2891

Share this post


Link to post
Share on other sites
kylomas

akira2891,

1 - Where is it failing?

2 - What is the value of $FTPdirData

3 - See _FTP_ListToArray in Help file.  You are not getting a list of files, just a directory name.

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
akira2891
#RequireAdmin
#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GUIListBox.au3>
#include <GUIListView.au3>
#include <FTPEx.au3>
#include <Array.au3>

#Region ### START Koda GUI section ###
$GUI = GUICreate("FTP tool", 754, 613, 691, 157)
GUICtrlCreateLabel("Host", 16, 16, 26, 17)
GUICtrlCreateLabel("Port", 192, 16, 23, 17)
GUICtrlCreateLabel("Username", 16, 56, 52, 17)
GUICtrlCreateLabel("Password", 16, 88, 50, 17)
GUICtrlCreateLabel("Status : ", 8, 152, 43, 17)
$status = GUICtrlCreateLabel("Not connected", 52, 152, 221, 17)
$host = GUICtrlCreateInput("", 48, 12, 121, 21)
$port = GUICtrlCreateCombo("", 224, 12, 65, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL))
GUICtrlSetData(-1, "21|80")
$username = GUICtrlCreateInput("", 79, 52, 121, 21)
$password = GUICtrlCreateInput("", 79, 81, 121, 21, BitOR($GUI_SS_DEFAULT_INPUT,$ES_PASSWORD))
$connectBtn = GUICtrlCreateButton("CONNECT", 56, 192, 75, 25)
$disconnectBtn = GUICtrlCreateButton("DISCONNECT", 143, 192, 107, 25)
$List = GUICtrlCreateList("", 16, 240, 721, 340, 0)

GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

If GUICtrlRead($status) = 'Not connected' Then GUICtrlSetState($disconnectBtn, $GUI_DISABLE) ; disable disconnect button if not connected
GUICtrlSetData($port, '21') ; default port

Global $status, $host, $port, $username, $password, $FTPopen, $FTPConnect, $List

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $connectBtn
            _connectFTP()
        Case $disconnectBtn
            _disconnectFTP()

    EndSwitch
WEnd

; connection to FTP server
Func _connectFTP()

    Local $_host = GUICtrlRead($host) ; host name
    Local $_username = GUICtrlRead($username) ; username
    Local $_password = GUICtrlRead($password) ; password
    Local $_port = GUICtrlRead($port) ; port

    $FTPopen = _FTP_Open('FTP connection test',0) ; name of connection
    $FTPConnect = _FTP_Connect($FTPopen, $_host, $_username, $_password, '', $_port) ; connect

        ; try to connect to server
        If Not @error Then ; if not error
            GUICtrlSetData($status, 'Connected') ; set status to connected
            GUICtrlSetState($connectBtn, $GUI_DISABLE) ; disable connect button
            GUICtrlSetState($disconnectBtn, $GUI_ENABLE) ; enable disconnect button
            _listData()
        Else
            MsgBox(48, 'FTP connection error', "Can't connect to server, @error = " & @error & ", @extended = " & @extended) ; if there is connection error
        EndIf

EndFunc

; disconnect from FTP server
Func _disconnectFTP()

    _FTP_Close($FTPopen)
    _FTP_Close($FTPConnect)
    GUICtrlSetState($disconnectBtn, $GUI_DISABLE)
    GUICtrlSetState($connectBtn, $GUI_ENABLE)
    GUICtrlSetData($status, 'Not connected')
    _GUICtrlListBox_ResetContent($List)

EndFunc

; list all files on FTP server
Func _listData()

    ; get current dir on server
    $FTPdirData = _FTP_DirGetCurrent($FTPConnect)

;~  MsgBox(0, "", $FTPdirData & ', @error = ' & @error & ' , @extended = ' & @extended)

    ; Add files in list box
    _GUICtrlListBox_BeginUpdate($List)
    _GUICtrlListBox_ResetContent($List)
    _GUICtrlListBox_Dir($List, "", $FTPdirData, False)
    _GUICtrlListBox_EndUpdate($List)

EndFunc

 

Here is the updated script but now when i connect im getting list of my drives on computer ?


yVTsdMU.png

Share this post


Link to post
Share on other sites
kylomas

akira,

Probably because of this

_GUICtrlListBox_Dir($List, "", $FTPdirData, False)

There is nothing in your code that gets a list of remote files, again, see _FTP_ListToArray in Help file.


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

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

  • Similar Content

    • CharlieH
      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  
    • Ascer
      By Ascer
      1. Description.
      Udf working with MSDN System.Collections.ArrayList. Allow you to make fast operations on huge arrays, speed is even x10 better than basic _ArrayAdd.  Not prefered for small arrays < 600 items. 2. Requirements
      .NET Framework 1.1 - 4.5 (on this version Microsoft destroy old rules) System Windows 3. Possibilities.
      ;=============================================================================================================== ; UDF Name: List.au3 ; ; Date: 2018-02-17, 10:52 ; Description: Simple udf to create System Collections as ArrayList and make multiple actions on them. ; ; Function(s): _ListCreate -> Creates a new list ; _ListCapacity -> Gets a list size in bytes ; _ListCount -> Gets items count in list ; _ListIsFixedSize -> Get bool if list if fixed size ; _ListIsReadOnly -> Get bool if list is read only ; _ListIsSynchronized -> Get bool if list is synchronized ; _ListGetItem -> Get item on index ; _ListSetItem -> Set item on index ; ; _ListAdd -> Add item at end of list ; _ListClear -> Remove all list items ; _ListClone -> Duplicate list in new var ; _ListContains -> Get bool if item is in list ; _ListGetHashCode -> Get hash code for list ; _ListGetRange -> Get list with items between indexs ; _ListIndexOf -> Get index of item ; _ListInsert -> Insert a new item on index ; _ListInsertRange -> Insert list into list on index ; _ListLastIndexOf -> Get index last of item ; _ListRemove -> Remove first found item ; _ListRemoveAt -> Remove item in index ; _ListRemoveRange -> Remove items between indexs ; _ListReverse -> Reverse all items in list ; _ListSetRange -> Set new value for items in range ; _ListSort -> Sort items in list (speed of reading) ; _ListToString -> Get list object name ; _ListTrimToSize -> Remove unused space in list ; ; Author(s): Ascer ;=============================================================================================================== 4. Downloads
      List.au3 5. Examples
      SpeedTest _ArrayAdd vs ListAdd SpeedTest ArraySearch vs ListIndexOf Basic usage - crating guild with members  
    • nacerbaaziz
      By nacerbaaziz
      can we  create a list box with  Columns?
      welcome everybody
      Dears I have a question if you let me
      can we  create a list box with  Columns?
      i know we can create a list view with Columns
      but my question is about the list box
      I'm waiting your responses
      Thank you in advance
    • Sigmac
      By Sigmac
      I have used this code for a long time, but for a week or 2 it stopped working?
      Connection is ok, the put create the file on server if I delete it on the FTP server, but the file is empty and after 30 secondees the function return 0
      I have tested on 2 machines, Filezilla FTP client have no issue to the server.
      Firewall is disabled under test.
        If $writeFTP = True Then
            $Open=_FTPOpen('MyFTP Control')
            $Conn=_FTPConnect($Open, $destinationServer, $destinationUsername, $destinationPass)
       MsgBox(0,"start","start")
            $Ftpp=_FtpPutFile($Conn, $filePath&$name&".json", "httpdocs/p_calender/" & $name & ".json")
      MsgBox(0,"return","return"&$Ftpp)
      Exit
            $Ftpc=_FTPClose($Open)
           EndIf
       
      From the Lib:
      Func _FTPPutFile($l_FTPSession, $s_LocalFile, $s_RemoteFile, $l_Flags = 0, $l_Context = 0)
          Local $ai_FTPPutFile = DllCall('wininet.dll', 'int', 'FtpPutFile', 'long', $l_FTPSession, 'str', $s_LocalFile, 'str', $s_RemoteFile, 'long', $l_Flags, 'long', $l_Context)
          If @error OR $ai_FTPPutFile[0] = 0 Then
              SetError(-1)
              Return 0
          EndIf
          Return $ai_FTPPutFile[0]
      EndFunc ;==> _FTPPutFile()
       
    • mLipok
      By mLipok
      As for now I was using such kind of snippet:
      Local $aFTP_FileList = _FTP_ListToArray($hFTPSession, 2) For $iFTPFileSize_idx = 1 To $aFTP_FileList[0] ConsoleWrite('Pobieram informacje o pliku:' & $aFTP_FileList[$iFTPFileSize_idx] ) $iWielkoscPliku = _FTP_FileGetSize($hFTPSession, $aFTP_FileList[$iFTPFileSize_idx]) ConsoleWrite($iFTPFileSize_idx & ' : ' & $aFTP_FileList[$iFTPFileSize_idx] & ' rozmiar=' & $iWielkoscPliku) Sleep(200) Next  
      But from some time my clients start buying cheap WD MyCloud NAS, and my problems starts.
      On all others NAS there is no problem (Seagate BlackArmour, QNAP, SYNOLOGY .... ).

      When on list there is for example 20 files then first two of them are very fast checked and reported they FileSize.
      After 2 files there is about 20 sec stop....... and next two files are checked with no errors but FileSize == 0 
      again there is about 20 sec stop....... and next two files are checked with no errors but FileSize == 0 
      again there is about 20 sec stop....... and next two files are checked with no errors but FileSize == 0 
      again there is about 20 sec stop....... and next two files are checked with no errors but FileSize == 0 
      again there is about 20 sec stop....... and next two files are checked with no errors but FileSize == 0 
      .......
       
      In time when I process investigation I checked how :  _FTP_ListToArray2D and _FTP_ListToArrayEx works and I was shocked that they works super fast.
       
      Here are the questions:
      Q1: Why does the problem occur only on WD MyCloud NAS?
      Q2: What is the significant difference that makes using _FTP_ListToArray2D and _FTP_ListToArrayEx to retrieve file sizes is still fast?
      Q3: Why _FTP_FileGetSize() not reporting @errors but returned FileSize = 0 
       
      Regards,
      mLipok
       
×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.