Sign in to follow this  
Followers 0
ViciousXUSMC

Some Help with FTPEx.au3

6 posts in this topic

#1 ·  Posted (edited)

So I have a project that has been sitting for a long time, and I think I just finally found the answer to the problem.  FTPEx.au3 but I am having some minor issues and would want to make sure I get some expert advice since this is going to be important that it works right if I implement it.

Goal: To have several computers in the field update a map file automatically every once in a while.

The map file is between 500MB and 1GB

I have basic FTP working so far to transfer a single file:

#Include <FTPEx.au3>
#Include <Array.au3>

$vFTPSes = _FTP_Open("PSFTP")
;MsgBox(0, "", @Error)
$vFTPSes2 = _FTP_Connect($vFTPSes, "my server", "my user", "my pw")
;MsgBox(0, "", @Error)
_FTP_FileGet($vFTPSes2, "/Tiburon FTP Update/Maps/test.txt", "C:\Users\frpatrmo\desktop\Get Map Update\Maps\test.txt", "True")
;MsgBox(0, "", @Error)
_FTP_Close($vFTPSes2)

My first issue is this, the "file" is actually a directory of MANY files and I cant find anywhere in the help files a way to transfer an entire directory instead of a file.  If that can be done, thats pretty much my largest hurdle right now.  Instead I bet that I will need to use a loop and an array of files.  I looked at the filelist to array functions like _FTP_ListToArray but it lists every file at the root of the FTP server, I need it to drill down to the specific directory where the files I need are located.

 

Other than that I assume that in a script that the file transfer will finish before the next command is run, this way I can ensure I do not put an incomplete download file into the application directory.  My logic tells me to download the file to @TempDir and when/if it completes the entire download to then have AutoIT move/copy the directory to the program location to be used.  This way if the download never finishes it never gets moved/copied.

 

After I get this initial trouble figured out, I then need to find the best way to make this run about 1 time a week silently and then not run again for another week on a multi user computer environment. 

 

As always appreciate the help. 

 

Edit: Ok found _FTP_DirSetCurrent() lets me set my desired folder as the current Dir and then the _FTP_ListToArray() will show me the correct files.

Still a long way to go however, since its a mix of folders and files and I have no idea how to loop it to get everything transferred. 

 

Edited by ViciousXUSMC

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

The way to put a directory and sub directories is with:

_FTP_DirPutContents($vFTPSes2, $dirPath, $remotePath, 1) ; 1 = recursive

 

Edited by MikahS

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Share this post


Link to post
Share on other sites

Just wanted to add my crazy code, I have this working to move all the files and skip over the folders.

#Include <FTPEx.au3>
#Include <Array.au3>
$vFTPSes = _FTP_Open("PSFTP")
$vFTPSes2 = _FTP_Connect($vFTPSes, "server", "user", "pass")
_FTP_DirSetCurrent($vFTPSes2, "/Tiburon FTP Update/Maps/")
Local $hFTPFind
$aFList = _FTP_FindFileFirst($vFTPSes2, _FTP_DirGetCurrent($vFTPSes2), $hFTPFind)
While 1
    _FTP_FileGet($vFTPSes2, GetName(), "C:\Users\frpatrmo\desktop\Get Map Update\Maps\" & GetName(), "True")
    If @Error <> 0 Then ExitLoop
WEnd
_FTP_FindFileClose($hFTPFind)
_FTP_Close($vFTPSes2)
Func GetName()
    $aName = _FTP_FindFileNext($hFTPFind)
    If @Error <> 0 Then Return 1
    If $aName[1] = 16 Then
        Return "test.txt"
    Else
        Return $aName[10]
    EndIf
EndFunc

Share this post


Link to post
Share on other sites

The way to put a directory and sub directories is with:

_FTP_DirPutContents($vFTPSes2, $dirPath, $remotePath, 1) ; 1 = recursive

 

​Most unfortunatly what I really need is the opposite, a _FTP_DirGetContents() I found a user made UDF that states it does this but I am getting errors when trying to use it.

 

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Ok, as I was driving home from work the lightbulb came on and I got rid of that terrible code from yesterday, this is much cleaner.

#Include <FTPEx.au3>
#Include <Array.au3>

$vFTPSes = _FTP_Open("PSFTP")
$vFTPSes2 = _FTP_Connect($vFTPSes, "snip", "snip", "snip")
_FTP_DirSetCurrent($vFTPSes2, "/Tiburon FTP Update/Maps/")

$aFile = _FTP_ListToArray($vFTPSes2, 2)
For $i = 1 To UBound($aFile) - 1
    _FTP_ProgressDownload($vFTPSes2, "C:\Users\frpatrmo\desktop\Get Map Update\Maps\" & $aFile[$i], $aFile[$i])
Next

_FTP_Close($vFTPSes2)
MsgBox(0, "", "FTP Download Complete")

 

Now here is where I have a choice, I can manually create loops for the directories in the same fashion by just changing the _FTP_DirSetCurrent to one of those directories, but in the interest of getting better and learning I really want to find a way to make this recursive and do this on its own.

My best place to start is I think a _FTP_ListToArray($vFTPSes2, 1) so that it only captures the Directory names and then I need to pass that into a loop that will change the FTP Directory, the Save Local Save Location, and then loop through the files in that directory (also probably need to do a local DirCreate() but not sure on that)

As for my "how to check what version of the map, and if it downloaded fully" my idea was this one.  Place a text file in the FTP server with lines that correspond to the # of files, the size, and the version so that I can read those values and compare to the local download.  It is not fully completed by my testing snippet looks like this.

 

#Include <Array.au3>

$sDLSize = FileReadLine(@ScriptDir & "\Maps\Map Info.txt", 1)
$sDLCount = FileReadLine(@ScriptDir & "\Maps\Map Info.txt", 2)

$aLocalDL = DirGetSize(@ScriptDir & "\Maps\", 1)
;_ArrayDisplay($aLocalDL)

MsgBox(0, "", "Your Download Status Is" & @CRLF & "Proposed Download Size was " & $sDLSize & " Your Download was " & $aLocalDL[0])
MsgBox(0, "", "Your Download Status Is" & @CRLF & "Proposed Download File Count was " & $sDLCount & " Your Download was " & $aLocalDL[1])
If $sDLSize = $aLocalDL[0] Then
    MsgBox(0, "", "Size Matches")
Else
    MsgBox(0, "", "Size Does Not Match")
EndIf

If $sDLCount = $aLocalDL[1] Then
    MsgBox(0, "", "Count Matches")
Else
    MsgBox(0, "", "Count Does Not Match")
EndIf

 

Edited by ViciousXUSMC

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

So close!

I have tried this like 3 different ways, and trying to change local, global, dim statments and even now went and changed the Download() function so that the local save path is a passed varible and for some reason while this is recursivily downloading all my files, all of the files are being saved to the home folder "Maps" and the Directories being created are empty.  

It's driving me mad because I cant fathom whats going on.

 

#Include <FTPEx.au3>
#Include <Array.au3>

$vFTPSes = _FTP_Open("PSFTP")
$vFTPSes2 = _FTP_Connect($vFTPSes, "snip", "snip", "snip")
_FTP_DirSetCurrent($vFTPSes2, "/Tiburon FTP Update/Maps/")

Global $aFile2 = _FTP_ListToArray($vFTPSes2, 1)
$sLocalSave = "C:\Users\frpatrmo\desktop\Get Map Update\Maps\"
DirCreate($sLocalSave)

Download()

;_ArrayDisplay($aFile2)

For $i2 = 1 To UBound($aFile2) - 1
    _FTP_DirSetCurrent($vFTPSes2, "/Tiburon FTP Update/Maps/" & $aFile2[$i2])
    $sLocalSaveNew = "C:\Users\frpatrmo\desktop\Get Map Update\Maps\" & $aFile2[$i2]
    DirCreate($sLocalSaveNew)
    MsgBox(0, "", "Inside Loop $localsavenew is " & $sLocalSaveNew)
    Download($sLocalSaveNew)
Next




_FTP_Close($vFTPSes2)


;MsgBox(0, "", "FTP Download Complete")


Func Download($sLocalSave = "C:\Users\frpatrmo\desktop\Get Map Update\Maps\")
$aFile = _FTP_ListToArray($vFTPSes2, 2)

For $i = 1 To UBound($aFile) - 1
    _FTP_ProgressDownload($vFTPSes2, $sLocalSave & $aFile[$i], $aFile[$i])
Next
EndFunc

Edit: Well played FTP Download looks like I was missing the ending "\" so it just defaulted to the last directory instead of giving an error. 

Edited by ViciousXUSMC

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  
Followers 0

  • 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.