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.
Post here if you experience file transfer problems or unexpected errors.
Comment from the forum admin
The ADDEXCLUSION command is often missunderstood. If you stumble upon this thread I suggest you
to visit first the ADDEXCLUSION examples at:
https://www.scriptftp.com/d/ftp-transfer-commands/addexclusion
I'm attempting to use the ADDEXCLUSION command and it does not appear to work. Would like to avoid downloading files that are already in the target local directory. Here is how I am using it in my script. Any assistance you could provide is greatly appreciated. Thank you. -Bruce

ADDEXCLUSION(DOWNLOAD,"*.*","C:\landingzone")

Script output: :
OPENHOST("123.123.30.12","userhere","passwordhere")
Connecting to 123.123.30.12
Connected.

LOCALCHDIR("C:\landingzone")
Changing current local directory to C:\landingzone

ADDEXCLUSION(DOWNLOAD,"*.*","C:\landingzone")
Adding "*.*" at "C:\landingzone" to the download exclusion list.
GETLIST(REMOTE_FILES)
Getting file listing of current remote directory
Found 4 files.

GETFILE("08-24-2011-APNJ-DailyReminderReport.xls")
Downloading................. 08-24-2011-APNJ-DailyReminderReport.xls
FTP Script
  1. #
  2. #
  3. #-Go out to SSL secure remXchg site and get client uploaded files
  4. #-which filenames begin with today's date every 30 minutes.
  5. #
  6. #
  7. #-set log file
  8. $logfile="C:\landingzone\logs\FTPGetFilesLOG-".GETDATE(FORMAT3).".txt"
  9. LOGTO($logfile,APPEND)
  10. PRINT("===============================================================================")
  11. PRINT(GETDATE(FORMAT2))
  12.  
  13. #-connect to FTP server
  14. OPENHOST("123.123.30.12","userhere","passwordhere")
  15.  
  16. #-set current local directory
  17. LOCALCHDIR("C:\landingzone")
  18.  
  19. #-set filter for files that have already been downloaded
  20. ADDEXCLUSION(DOWNLOAD,"*.*","C:\landingzone")
  21.  
  22. #-set current remote directory
  23. CHDIR("/uploads")
  24.  
  25. #-set variable for todays date to match against
  26. $todaysdate=(GETDATE(MONTH)."-".GETDATE(DAY)."-".GETDATE(YEAR))
  27.  
  28. #-get remote folder/directory list
  29. GETLIST($remotefolders,REMOTE_DIRECTORIES)
  30. FOREACH $item IN $remotefolders
  31.   #-set the current remote directory
  32.   CHDIR($item)
  33.   #-get only files with filename starting with today's date
  34.   GETLIST($remotefiles,REMOTE_FILES)
  35.     FOREACH $item IN $remotefiles
  36.     $fileprefix=TEXTCUT($item,1,10)
  37.     IF($todaysdate==$fileprefix)
  38.       GETFILE($item)
  39.     END IF
  40.   #-set the current remote directory back to /uploads/
  41.   CHDIR("/uploads")
  42.  
  43. #-clear exclusion list
  44.  
  45. #-get any/all files that may have been uploaded to the '/upload/' directory
  46. GETFILE("*.*")
  47.  
  48. #-file transfer complete - close connection
  49.  
  50. #-close the ScriptFTP window
  51. #EXIT
I think that the best way to do so is just use the SYNC command. It can be used to download only new or modified files. For example:
FTP Script
  1. # Download new and modified files from the remote
  2. # folder /uploads to C:\landingzone
  3. SYNC("C:\landingzone","/uploads",DOWNLOAD,SUBDIRS)
If you use ADDEXCLUSION this way:
FTP Script
  1. ADDEXCLUSION(DOWNLOAD,"*.*","C:\landingzone")
you are excluding from download any remote file (*.*) that is in the directory C:\landingzone. Obviously this will make ADDEXCLUSION useless as no remote file has the path C:\landingzone (remote paths usually start with '/' instead)
I ended up NOT using the ADDEXCLUSION command. I constructed multiple different loops for comparing the newly uploaded customer files to what was brought down locally. This runs every 30 minutes so I wanted to avoid downloading the same files over and over.

Attached is the final script so you can see and you are welcome to post it as I changed the names of the paths, passwords, etc.

Thanks again!

FTP Script
  1. #
  2. #
  3. #-Go out to SSL secure site and get client uploaded files
  4. #-which filenames begin with today's date every 30 minutes. If file
  5. #-already exists in the local LandingZone directory then skip it.
  6. #
  7. #
  8. #-set log file
  9. $logfile="C:\landingzone\logs\FTPGetFilesLOG-".GETDATE(FORMAT3).".txt"
  10. LOGTO($logfile,APPEND)
  11. PRINT("===============================================================================")
  12. PRINT(GETDATE(FORMAT2))
  13.  
  14. #-connect to FTP server
  15. OPENHOST("ftp.secure.site.com","user","pwd")
  16.  
  17. #-set current local directory and build list of files in the LandingZone
  18. LOCALCHDIR("C:\landingzone")
  19. GETLIST($localfiles,LOCAL_FILES)
  20. PRINT($localfiles)
  21.  
  22. #-set current remote directory
  23. CHDIR("/uploads")
  24.  
  25. #-set all variables for todays date and file(s) to match against when looping files
  26. $todaysdate=(GETDATE(MONTH)."-".GETDATE(DAY)."-".GETDATE(YEAR))
  27. $ignorefile="FALSE"
  28. $uploadedfiles="files:"
  29.  
  30. #-get remote folder/directory list
  31. GETLIST($remotefolders,REMOTE_DIRECTORIES)
  32. FOREACH $item IN $remotefolders
  33.    #-set the current remote directory
  34.    CHDIR($item)
  35.    $clientdir=$item
  36.    #-get list of files in remote directory
  37.    GETLIST($remotefiles,REMOTE_FILES)
  38.    FOREACH $item IN $remotefiles
  39.       $remotefilename=$item
  40.       $remotefileprefix=TEXTCUT($item,1,10)
  41.       #-get only files with filename starting with today's date
  42.       IF($todaysdate==$remotefileprefix)
  43.          #-loop through all files already in the LandingZone directory
  44.          FOREACH $item IN $localfiles
  45.          $localfilename=$item
  46.             #-same file already exists, set the ignorefile flag to true
  47.             IF($remotefilename==$localfilename)
  48.                $ignorefile="TRUE"
  49.             END IF
  50.          END FOREACH
  51.          IF($ignorefile=="TRUE")
  52.             #-skip it
  53.          ELSE
  54.             #-get the file into the local LandingZone directory
  55.             GETFILE($remotefilename)
  56.             #-build string variable for email message
  57.             $uploadedfiles=$uploadedfiles.$todaysdate."-".$clientdir."-----"
  58.          END IF
  59.          #-set ignorefile flag back to the default of false
  60.          $ignorefile="FALSE"
  61.       END IF
  62.    #-set the current remote directory back to /uploads/
  63.    CHDIR("/uploads")
  64.    $clientdir="UPLOAD"
  65.  
  66. #-now get files that may have been uploaded to the /upload/ parent directory and add to email list
  67. GETLIST($remoteparentfiles,REMOTE_FILES)
  68. FOREACH $item IN $remoteparentfiles
  69.    $remoteparentfilename=$item
  70.    #-loop through all files already in the LandingZone directory
  71.    FOREACH $item IN $localfiles
  72.    $localfilename=$item
  73.       #-same file already exists, set the ignorefile flag to true
  74.       IF($remoteparentfilename==$localfilename)
  75.          $ignorefile="TRUE"
  76.       END IF
  77.    IF($ignorefile=="TRUE")
  78.       #-skip it
  79.    ELSE
  80.       #-get the file into the local LandingZone directory
  81.       GETFILE($remoteparentfilename)
  82.       $uploadedfiles=$uploadedfiles.$todaysdate."-".$clientdir."-----"
  83.    END IF
  84.  
  85. #-send email notification if at least 1 file was downloaded
  86. If($uploadedfiles!="files:")
  87.    $blat_path="C:\windows\system32\blat.exe"
  88.    $smtp_svr="smtp.mysvr.com"
  89.    $smtp_usr="support@mycompany.com"
  90.    $smtp_pwd="mypwd"
  91.    $email_from="support@remxchg.com"
  92.    $email_to="me@mycompany.com"
  93.    $email_subject="files_uploaded_from_clients"
  94.    $email_body=$uploadedfiles
  95.    $emailiT=$blat_path." -server ".$smtp_svr." -u ".$smtp_usr." -pw ".$smtp_pwd." -f ".$email_from." -to ".$email_to." -subject ".$email_subject." -body ".$email_body
  96.    EXEC($emailit)
  97.  
  98. #-file transfer complete - close connection
  99.  
  100. #-close the ScriptFTP window