ftp automation

ScriptFTP

The professional tool to automate FTP, SFTP, FTPS and schedule FTP batch jobs

The forum is now read only. Please, go to the the main ScriptFTP website if you need help.
Need help writing a script? Have any question about ScriptFTP?
Hello there,

i have made a script according to the docu. I have a local ftps, there are different folders (with files in it) in the /root/ directory, it contains several database backups, that gets generated every 4 minutes and i want to download the newest files only and not the older stuff so i made this script but it doesnt work so far. It would be nice, if someone could check it:


FTP Script
  1. :connect
  2. #connect to the ftp
  3. SETPORT(313)
  4. SETPROTOCOL(FTPS_EXPLICIT_ENCRYPT_DATA)
  5. $result_open_host=OPENHOST("192.168.1.102","test","test")
  6.  
  7. #check if connection is possible, if not reconnect
  8. IF($result_open_host!="OK")
  9.     PRINT("Cannot connect!")
  10.     SLEEP(10)
  11.     PRINT("Trying again")
  12.     GOTO :connect
  13.  
  14. #Set local download dir
  15. LOCALCHDIR("C:\backups")
  16.  
  17. #Set remote dire
  18. CHDIR("/root/")
  19.  
  20. #Loop for fetching files
  21. WHILE("TRUE")
  22.     #Get remote file list
  23.     GETLIST($myremotefilelist,REMOTE_FILES,"*.*")
  24.     #Get the current date
  25.     $currenttime=GETDATE(FORMAT0)
  26.     PRINT("Current time is ".$currenttime)
  27.     #Check all files in remote dir and download them
  28.     FOREACH $remotefile IN $myremotefilelist
  29.         $filetime=(GETFILETIME(REMOTE,$remotefile)-(55*60))
  30.         PRINT("Remote file time is ".$filetime)
  31.             IF($filetime<$currentdate)
  32.             GETFILE($remotefile)
  33.             END IF
  34.     END FOREACH
  35.     Sleep(10)
  36.  
Hi Mike,

To download the most recent files the SYNC command is what you probably need.

https://www.scriptftp.com/d/ftp-transfer-commands/sync

Also, you can use date comparing to determine if a remote file should be downloaded or not. This is not related to the SYNC command, it is another way to accomplish what you want.

Here is an example that does some date operations. It is not exactly what you need but if you read the comments you will understand how to compare a date of a file against any date you define etc.

https://www.scriptftp.com/s/average-scripts/older_than_a_week-ftp

If you need further help feel free to post here again.
Ive watched the documentation again and made another script. This is not the "end version" of my script, the timedifferenz parameter is missing too, didnt added it.

FTP Script
  1. :connect
  2. #connect to the ftp
  3. SETPORT(523)
  4. SETPROTOCOL(FTPS_EXPLICIT_ENCRYPT_DATA)
  5. $result_open_host=OPENHOST("192.168.2.101","test","test")
  6.  
  7. #check if connection is possible, if not reconnect
  8. IF($result_open_host!="OK")
  9.     PRINT("Cannot connect!")
  10.     SLEEP(10)
  11.     PRINT("Trying again")
  12.     GOTO :connect
  13.  
  14. LOCALCHDIR("C:\Mucke")
  15. CHDIR("/test/")
  16.  
  17. $localtime=GETDATE(FORMAT1)
  18. GETLIST($list,REMOTE_DIRECTORIES)
  19.  
  20. FOREACH $dir IN $list
  21.     $wicked=CHDIR($dir)
  22.     GETLIST($new,REMOTE_FILES,"*.*")
  23.     FOREACH $file in $new
  24.     $mike=GETFILETIME(REMOTE, $file)
  25.     IF($mike<$localtime)
  26.     GETFILE("*.*",SUBDIRS)
  27.     END IF
  28.     END FOREACH
  29.     CHDIR("../")   

I know the SYNC "Tag" but the problem is, I don't want to remove the folders with the files inside from my ftp and the downloaded files from the ftp are move to another dir via another script. So in this scenario, SYNC would download the files again, so i need a bypass somehow
Here is a new update:
I've managed to get it to work, but it is pretty time consuming (I have around 4000 folders inside the ftp and the scanning process (GetList) is time consuming as f*ck). The FTP server has a time difference of 3hours so i added it with 3h and added another 5 minutes to the Systemtime to collect files that are up to 5 minutes available. I would like to use SYNC but the filter options are pretty "bad", there are a few but time filtering without scanning every folder for the date is a pretty bad bypass for me. Is there another way maybe ?

Creating a Directory isn't working with parameters, in the second FOREACH before the download, but idk why.. :(

I tried almost every FTPs client, but there isn't one, that fits to my needs but ScriptFTP is near.

FTP Script
  1. :connect
  2. #connect to the ftp
  3. SETPORT(123)
  4. SETPROTOCOL(FTPS_EXPLICIT_ENCRYPT_DATA)
  5. $result_open_host=OPENHOST("123.123.123.123","test","test")
  6.  
  7. #check if connection is possible, if not reconnect
  8. IF($result_open_host!="OK")
  9.     PRINT("Cannot connect!")
  10.     SLEEP(10)
  11.     PRINT("Trying again")
  12.     GOTO :connect
  13.  
  14. LOCALCHDIR("C:\Backup")
  15. CHDIR("/root/")
  16.  
  17. WHILE ("TRUE")
  18.  
  19.    $ZeitSystem=GETDATE(FORMAT0)
  20.    $ZeitSystem5=$ZeitSystem-(300)
  21.  
  22.    GETLIST($Ordner,REMOTE_DIRECTORIES)
  23.    FOREACH $Verzeichnis IN $Ordner
  24.       CHDIR($Verzeichnis)
  25.       $Dateien=GETLIST($Verzeichnis,REMOTE_FILES,"*.*")
  26.       FOREACH $Dateien IN $Verzeichnis
  27.          $ZeitDateien=GETFILETIME(REMOTE, $Dateien)
  28.          $ZeitDateien5=$ZeitDateien+(3*60*60)
  29.          IF ($ZeitDateien5>$ZeitSystem)
  30.             GETFILE ($Dateien, SUBDIRS)
  31.          END IF
  32.       END FOREACH
  33.       CHDIR("../")
  34.  
  35.  
  36.  
  37.    
Creating a Directory isn't working with parameters, in the second FOREACH before the download, but idk why

I don't see where you are trying to create a directory in the script. Just note, if it helps, that creating a directory in ScriptFTP does not accept any extra parameters, just the directory name.
Is there another way maybe ?

Unfortunately no, there is no other way, sorry. May be, if you have so many folders running separate FTP scripts in parallel may help. One script will process the folders starting with the A letter, then another one with the B and so on:


FTP Script
  1. GETLIST($Ordner,REMOTE_DIRECTORIES)
  2. FOREACH $Verzeichnis IN $Ordner
  3.     IF(TEXTCUT($Verzeichnis,1,1)=="A" || TEXTCUT($Verzeichnis,1,1)=="a")
  4.        CHDIR($Verzeichnis)
  5.        $Dateien=GETLIST($Verzeichnis,REMOTE_FILES,"*.*")
  6.        # .... etc
  7.     END IF

If you don't want to write a script for every letter you can pass the letter as a parameter in the command line and then retrieve it within the script with GETPARAM
Thank you, i will try it out :)