Jump to content
Elia

FTP Multiple Files with the same FTP Session

Recommended Posts

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

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

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.

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

    • 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
      Hello,
      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
      Hello,
      I am attempting to pull a list of the directory structure from a public FTP where no username or password is required i.e:
      ftp://ftp.adobe.com/pub/adobe/
      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
       
       
       
    • By Jemboy
      Hi,
      Ones(some times twice) a month I get an e-mail with zip file, which has price updates from a supplier.
      I have to upload the file to an FTP to get it processed.
      When uploading the file, it will get "timestamped" with the time and date at which time the file was uploaded.
      Normally this is fine, because I mostly upload the file the same day.
      Sometimes it may take 1 or 2 days before I can upload the file.
      For historical purpose, I would like to have the file timestamped with the original date.
      I have tried using:  _FTP_Command ( $hFTPSession, "MFCT YYYYMMDDHHMMSS path") 
      however this command does not work or change the timestamp as I expected.
      Does anyone now a way how I can change the timestamp of a FTP-file?
    • By Sally1801
      Hello,
      i need help in deleting files from a server. The function "_FTP_DirDelete" only deletes a directory when its empty. I need to delete a non empty directory. I also can not delete the files in this directory first, because the files are PAG and DIR files in a .DAV directory and FTPEx.au3 doesnt like a directory starting with a ".".
      Any ideas?
      Thx, Sally
×
×
  • Create New...