Jump to content
Sign in to follow this  
ViciousXUSMC

Some Help with FTPEx.au3

Recommended Posts

ViciousXUSMC

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
MikahS

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
ViciousXUSMC

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
ViciousXUSMC

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
ViciousXUSMC

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
ViciousXUSMC

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  

  • Similar Content

    • Carm01
      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
       
       
       
    • Jemboy
      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?
    • Sally1801
      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
    • 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  
    • 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()
       
×