Elia

FTP Multiple Files with the same FTP Session

4 posts in this topic

Hi All,

I am trying to write a script that FTPs multiple files to the same destination using the same FTP session. The script takes the source files, places them on the FTP server, the files should be FTP'd and then moved into the SENT folder in the original location.  I seem to have got everything working except when i try to FTP multiple files, if i put a specific file name under $LocalFile & $RemoteFile under FTP_FilePut then the script works, can anyone tell me where I am going wrong please? I've visited other forums and input an array for the $LocalFile which seems to work, as if i put an ArrayDisplay in it does show me all the files in the folder. I have tried to enter a path, "". '' and "*.*" for $RemoteFile but nothing seems to work:

#include <FTPEx.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>
#include <Inet.au3>
#include <Debug.au3>
#include <File.au3>
#include <Array.au3>

Local $Server = 'ftp.com'
Local $Username = 'user'
Local $Password = 'password'
Local $LocalFile = 'C:\FTP\doc.txt'
Local $RemoteFile = '*.*'
Local $FilePath = 'C:\FTP'

FileMove ("\\COMP\Source\*.txt", "\\COMP\FTP")

If FileExists ( "\\COMP\FTP" ) Then
    Local $FileList = _FileListToArray($FilePath, "*", 1)
    Local $Open = _FTP_Open('FTP Test')
    Local $Connect = _FTP_Connect($Open, $Server, $Username, $Password)
    If $Connect <> 0 Then
        For $i = 1 to $FileList[0] - 1
            Local $Transfer = _FTP_FilePut($Connect, $FileList[$i], "" ,0 ,0)             <------------- I think the problem is with the $RemoteFile part
        Next
            _FTP_Close($Connect)
            _FTP_Close($Open)
        
            FileMove ( "\\COMP\FTP\*.txt", "\\COMP\Source\SENT")
     EndIf
EndIf

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

What was the idea behind opening 3 threads on the same topic in different forums?

mmm.. and one more just popped up. Next one will urn you a posting holiday from our forums!

Jos

Edited by Jos

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Yes the problem is the remotepath it mustn't be blank. Here a small script i wrote in 2011:

;https://autoit.de/index.php/Thread/14918-Deutsche-Hilfe-Funktionen-ohne-Beispiel/?postID=214046#post214046#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <FTPEx.au3>
#include <File.au3>
#include <Array.au3>

Opt('MustDeclareVars', 1)

_example2()

Func _example2()
    ;lädt alle Dateien eines Ordner mithilfe  _FTP_FilePut auf den FTP-Server (noch ohne Unterordner)
    Local $s_ServerName = 'guest.autobert.myplace.net'  ;dieser Server existiert nicht
    Local $s_Username = "guest"
    Local $s_Password = "TopSecret"

    Local $s_LocalFolder = @ScriptDir ;das angegebene Verzsichnis muss existieren
    Local $s_RemoteFolder = "/FTPTestfiles/Test/" ;dieser Ordner wird notfalls angelegt
    Local $i_Passive = 0 ;manche Server benötigen hier eine 1 für Passiven Modus
    Local $l_InternetSession, $l_FTPSession, $errOpen, $errFTP

    $l_InternetSession = _FTP_Open('AuoItZilla') ;Öffnet eine FTP Sitzung
    $errOpen = @error
    If Not @error Then
        $l_FTPSession = _FTP_Connect($l_InternetSession, $s_ServerName, $s_Username, $s_Password, $i_Passive) ;Verbindet zu einem FTP Server
        $errFTP = @error
        If Not @error Then
            _FolderPut($l_FTPSession, $s_LocalFolder, $s_RemoteFolder, "_FTP*3.au3") ;kopiert alle au3 Dateien
            If Not @error Then
                ConsoleWrite("_FolderPut: " & @extended & " Dateien erfolgreich übertragen" & @CRLF)
            Else
                ConsoleWrite("_FolderPut Fehler:" & @error & " " & @extended & @CRLF)
            EndIf
        Else
            MsgBox(0, "Connect", "fehlgeschlagen")
            ConsoleWrite("Connect: " & " " & $errFTP & @CRLF)
        EndIf
    Else
        MsgBox(0, "Open", "fehlgeschlagen")
        ConsoleWrite("Open " & " " & $errOpen & @CRLF)
    EndIf
    _FTP_Close($l_InternetSession) ;schliesst die Sitzung
EndFunc   ;==>_example2

Func _FolderPut($l_FTPSession, $s_LocalFolder, $s_RemoteFolder, $sFilter = "*")
    Local $bDebugFP = True ;um Debuginfos an- auszuschalten
    If $bDebugFP Then ConsoleWrite("Lokal " & $s_LocalFolder & " Remote " & $s_RemoteFolder & @CRLF)
    if StringRight($s_RemoteFolder,1) <> "/" Then $s_RemoteFolder &= "/"
    if StringLeft($s_RemoteFolder,1) <> "/" Then $s_RemoteFolder = "/" & $s_RemoteFolder
    Local $aPathSplit = StringSplit($s_RemoteFolder, "/")
    _ArrayDisplay($aPathSplit)
    For $i = 2 To $aPathSplit[0] -1     ;Element 1 ist leer ebenso letztes durch StringTrim.....
        If $bDebugFP Then ConsoleWrite($aPathSplit[$i])
        _FTP_DirSetCurrent($l_FTPSession, $aPathSplit[$i])
        If @error Then
            If $bDebugFP Then ConsoleWrite(@TAB & "wird versucht anzulegen" & @TAB)
            _FTP_DirCreate($l_FTPSession, $aPathSplit[$i])
            _FTP_DirSetCurrent($l_FTPSession, $aPathSplit[$i])
            If @error Then
                If $bDebugFP Then ConsoleWrite("Fehler " & @error & @CRLF)
                SetError(1, $i)
                Return 0
            Else
                If $bDebugFP Then ConsoleWrite("OK" & @crlf)
            EndIf
        Else
            If $bDebugFP Then ConsoleWrite("/")
        EndIf
    Next
    If $bDebugFP Then ConsoleWrite(@CRLF & "Aktuelles Verzeichnis" & _FTP_DirGetCurrent($l_FTPSession) & @CRLF)
    Local $aUpload_Files = _FileListToArray($s_LocalFolder, $sFilter, 1)
    Local $iErrors = 0
    Local $iSuccess = 0
    For $i = 1 To $aUpload_Files[0]
        If $bDebugFP Then ConsoleWrite($aUpload_Files[$i] & @TAB)
        If _FTP_FilePut($l_FTPSession, $aUpload_Files[$i], $aUpload_Files[$i]) Then
            $iSuccess +=1
            If $bDebugFP Then ConsoleWrite("OK" & @CRLF)
        Else
            $iErrors += 1
            If $bDebugFP Then ConsoleWrite("Fehler: " & @error & @CRLF)
        EndIf
    Next
    if $iErrors > 0 Then
        SetError(2, $iErrors, 0)
    Else
        SetExtended($iSuccess)
    EndIf
EndFunc   ;==>_FolderPut

Note: the func _FolderPut is non recursiv.

1 person likes this

Share this post


Link to post
Share on other sites

Hi AutoBert,

Thank you so much! I have changed from using an _FTP_FilePut command to _FTP_DirPutContents, this means I was also able to remove the array as it was no longer needed. For anyone else this is now the working code:

#include <FTPEx.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>
#include <Inet.au3>
#include <Debug.au3>
#include <File.au3>
#include <Array.au3>

Local $Server = 'ftp.com'
Local $Username = 'user'
Local $Password = 'password'
Local $LocalFolder = 'C:\FTP'
Local $RemoteFolder = '/users/FTP'

FileMove ("\\COMP\Source\*.txt", "\\COMP\FTP")

If FileExists ( "\\COMP\FTP" ) Then
    Local $Open = _FTP_Open('FTP Test')
    Local $Connect = _FTP_Connect($Open, $Server, $Username, $Password)
    If $Connect <> 0 Then
            Local $Transfer = _FTP_DirPutContents($Connect, $LocalFolder, $RemoteFolder, 0)    <-------------- New line
            _FTP_Close($Connect)
            _FTP_Close($Open)
            FileMove ( "\\COMP\FTP\*.txt", "\\COMP\Source\SENT")
     EndIf
EndIf

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

    • Xandy
      By Xandy
      Screenshot program that can upload/FTP to website/storage with hotkey.
      Features GUI to display programmable keys.

      Set the hotkeys with this function to use, and display hotkeys.
      ; hotkey_set() Parameters: ; ----------------------------------------------------------- ; $aHotkey -                The array hotkeys are stored in. This function sets the values of this array[hotkey_id][$hotkey_data] ; $dHotkey_id -             The enum for this hotkey index ; $sHotkey_description -     The label displayed to the user to represent function of hotkey ; $sHotkey_name -            The hotkey it'self.  Whatever name string you want to give the key ; $dHotkey_key -             The _IsPressed keycode ;    Keycodes AT: https://www.autoitscript.com/autoit3/docs/libfunctions/_IsPressed.htm ; $dHotkey_shift -             Shift flag 0 or 1 Default off ; $dHotkey_ctrl -             Ctrl flag 0 or 1 Default off ; $dHotkey_alt -             Alt flag 0 or 1 Default off Func hotkey_set(ByRef $aHotkey, $dHotkey_id, $sHotkey_description, $sHotkey_name, $dHotkey_key, $dHotkey_shift = 0, $dHotkey_ctrl = 0, $dHotkey_alt = 0)     $aHotkey[$dHotkey_id][$eHotkey_data_key_description] = $sHotkey_description     $aHotkey[$dHotkey_id][$eHotkey_data_key_name] = $sHotkey_name     $aHotkey[$dHotkey_id][$eHotkey_data_key] = $dHotkey_key     $aHotkey[$dHotkey_id][$eHotkey_data_shift] = $dHotkey_shift     $aHotkey[$dHotkey_id][$eHotkey_data_ctrl] = $dHotkey_ctrl     $aHotkey[$dHotkey_id][$eHotkey_data_alt] = $dHotkey_alt EndFunc   ;==>hotkey_set ; You can set your hotkeys here ; Please visit the hotkey_set() function for parameter information hotkey_set($aHotkey, $eHotkey_screenshot_ftp, "Selected Window to FTP", "F12", "7B", 0, 1, 0); F12 hotkey_set($aHotkey, $eHotkey_screenshot_disk, "Selected Window to Disk", "S", "53", 1, 1, 1); S hotkey_set($aHotkey, $eHotkey_clipboard_send, "Send Clipboard keystrokes", "F10", "79", 1, 1, 1); F10  
      Configure settings dialog:

      Screenshot Filename and Screenshot Counter, are used to create simple unique filenames that can cycle. Copy URL to clipboard option. - For linking your screenshots. The screenshot file type is for local copy only. App always uses .JPG for FTP right now, but I could add FTP screenshot file type specification.  
      Any suggestions?  Did I break anything, what did I miss?
      Package uses TTS.au3 by Beege: 
       
      FTP_Screen.zip
      File includes:
         - FTP_Screen.au3
         - FTPScreen.ico
         - TTS.au3 - by Beege
    • drorshem
      By drorshem
      Hi,
      I'm looking to create a script which will download all *.txt files from a remote FTP server.
      Once downloaded, delete all *.txt files.
       
      Can this be achieved using AutoIt ?
    • Xenobiologist
      By Xenobiologist
      Hi folks,
      I'm using the udf #include <FTPEx.au3> to "download" some files from our zOS (MVS) system via autoit FTP with _FTP_FileGet.
      The FileGet works, but the codepage is wrong. Special German chars like öäüß are not transfered correctly.
      Any idea how to set the codepage? Swichting between binary or ascii doesn't solve the problem.
      Using the ftp command in a cmd.exe window it works like this : quote site sbd=(IBM-273,iso8859-1)
      Thanks
      Mega
    • DCCD
      By DCCD
      Hi, how to get full error reporting if FTP failed?
       
      tried  _WinAPI_GetLastErrorMessage() func but That didn't help at all.  the information was returned by _WinAPI_GetLastErrorMessage() can't help me!
       
       
      _FTP_Open, return Success: a handle, Failure: 0 and sets the @error flag to non-zero
      _FTP_Connect,  return Success: an handle to connected session. Failure: 0 and sets the @error flag to non-zero.
      _FTP_DirCreate, return Success: 1. Failure: 0.
      _FTP_FilePut, return Success: 1. Failure: 0 and sets the @error flag to non-zero.
       
      Any help would be much appreciated
       
       
       
       
    • Elia
      By Elia
      Hi All,
      I need some help, I am writing a script that FTPs some files and then outputs the results, but I am unable to figure out how to output the file name on line 58. Here is the code (modified to exclude personal information):
       
      #include <FTPEx.au3> #include <MsgBoxConstants.au3> #include <WinAPIFiles.au3> #include <Inet.au3> #include <Debug.au3> #include <File.au3> #include <Array.au3> Local $Server = 'ftp.com' Local $Username = 'user' Local $Password = 'password' Local $LocalFolder = 'C:\Folder' Local $RemoteFolder = '/users/user1' Local $SMTPServer = "Exchange.domain.com" Local $FromName = "FTP" Local $FromAddress = "email@address.com" Local $ToAddress = " email@address.com " Local $Subject = "FTP Transfer Failed, please check server" Local $Subject1 = "FTP Connection Failed, please check server" Local $Body[2] $Body[0] = "Line 1" $Body[1] = "Line 2" Local $Body1[2] $Body1[0] = "Line 1" $Body1[1] = "Line 2" $LogFolder = "\\COMP\LOGS"   _DebugSetup ("Debug", True)   FileMove ("\\COMP\Test\*.txt", "\\COMP\FTP")   Local $FolderContents = _FileListToArray("\\COMP\FTP") Local $LogFile = "\FilesToSend_" & @YEAR & @MON & @MDAY & @HOUR & @MIN & @SEC & ".log" _FileCreate($LogFolder & $LogFile) _FileWriteFromArray($LogFolder & $LogFile, $FolderContents)   If FileExists ( "\\COMP\FTP" ) Then     Local $Open = _FTP_Open('FTP')     Local $CallBack = _FTP_SetStatusCallback($Open, 'FTPStatusCallbackHandler')     Local $Connect = _FTP_Connect($Open, $Server, $Username, $Password, 0, $INTERNET_DEFAULT_FTP_PORT, $INTERNET_SERVICE_FTP, 0, $CallBack)         If $Connect <> 0 Then             Local $Transfer = _FTP_DirPutContents($Connect, $LocalFolder, $RemoteFolder, 0)             _FTP_Close($Connect)             _FTP_Close($Open)                 Else             _INetSmtpMail($SMTPServer, $FromName, $FromAddress, $ToAddress, $Subject1, $Body, "EHLO" & @ComputerName, -1)         EndIf             If $Transfer = 1 Then                 FileMove ( "\\COMP\FTP\*.txt", "\\COMP\Test\SENT")                 Else                 _INetSmtpMail($SMTPServer, $FromName, $FromAddress, $ToAddress, $Subject, $Body1, "EHLO" & @ComputerName, -1)             EndIf EndIf Func FTPStatusCallbackHandler($hInternet, $iContext, $iInternetStatus, $pStatusInformation, $iStatusInformationLength)                     #forceref $hInternet, $iContext                     If $iInternetStatus = $INTERNET_STATUS_REQUEST_SENT or $iInternetStatus = $INTERNET_STATUS_RESPONSE_RECEIVED Then                         Local $iBytesRead                         Local $tStatus = DllStructCreate('dword')                         _WinAPI_ReadProcessMemory(_WinAPI_GetCurrentProcess(), $pStatusInformation, $tStatus, $iStatusInformationLength, $iBytesRead)                         _DebugOut(_FTP_DecodeInternetStatus($iInternetStatus) & ' | Size = ' & DllStructGetData($tStatus, 1) & ' Bytes    Bytes read = ' & $iBytesRead)                     Else                         _DebugOut(_FTP_DecodeInternetStatus($iInternetStatus))                     EndIf                     EndFunc I would like the output on line 58 to include a file name, at the moment it only displays the size of the file and Bytes read. Here is what is currently outputted:
      Handle created
      Resolving name ...
      Name resolved
      Connecting to server ...
      Connected to server
      Receiving response ...
      Response received | Size = 58 Bytes    Bytes read = 4
      Sending request ...
      Request sent | Size = 13 Bytes    Bytes read = 4
      Receiving response ...
      Response received | Size = 20 Bytes    Bytes read = 4
      Sending request ...
      Request sent | Size = 19 Bytes    Bytes read = 4
      Receiving response ...
      Response received | Size = 20 Bytes    Bytes read = 4
      Handle created
      Handle created
      Handle created
      Handle created
      Handle created
      Closing connection ...
      Connection closed
      Handle closing ...
      Any help would be greatly appreciated!
      Thanks.