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,

I would like to share with you something that an user has just asked me by email. He wanted a script to constantly check for the file or files that may appear on a local folder and as soon as they are found. I wrote this small script for him:
FTP Script
  1. #
  2. # This script looks for files to appear in a
  3. # specific local directory and uploads them
  4. # to the FTP server. Once uploaded it moves
  5. # the files to another directory
  6. #
  7. # You can schedule this script to be run every
  8. # 5 minutes to automatically upload every file
  9. # that is copied to the folder
  10. #
  11.  
  12.  
  13. # The directory to watch for files
  14. $incoming_dir="C:\files_appear_here"
  15.  
  16. # Once the files are uploaded they are copied to here
  17. $processed_files_dir="C:\uploaded_files"
  18.  
  19. # Connect to the FTP server
  20. OPENHOST("192.168.23.32","myusername","mypassword")
  21.  
  22.  
  23. # Change current local directory
  24. LOCALCHDIR($incoming_dir)
  25.  
  26. # Retrieve the C:\pim\outbox file listing
  27. $result=GETLIST($list,LOCAL_FILES,"*.*")
  28.  
  29. # If GETLIST failed stop the script
  30. IF($result!="OK")
  31.     STOP
  32.  
  33. FOREACH $item IN $list
  34.     # Upload the file
  35.     $result=PUTFILE($item)
  36.     # If the file has been succesfully uploaded
  37.     # move it
  38.     IF($result=="OK")
  39.         EXEC("move /Y ".$item." ".$processed_files_dir)
  40.     END IF
  41.  
Be aware that you may upload a partial file if the file is in the process of "appearing" in "C:\files_appear_here".

And the timing might just be right to upload a partial file, have the "appearance" finish during the upload, then move the completed file to the "C:\uploaded_files" directory.

Which is why I did an OS file move first, and then uploaded the file. (See Store and Forward)

Edited by ScriptFTP support. You can see the script that Bucket posted here:
http://www.scriptftp.com/forum/viewtopic.php?p=1300
Be aware that you may upload a partial file if the file is in the process of "appearing" in "C:\files_appear_here".
You are absolutely right Bucket. I come across another solution a bit simpler than try moving the file to see if the file is being written by another process or being copied: check the file size, wait one second, and check the file size gain. If the file sizes are different it means that the file is being written (or in the process of "appearing" in that folder). The drawback of this solution is that it takes one second for every file to be uploaded. Of course, the other solution you posted (the link is in the previous post on this forum thread) is also completely correct.
FTP Script
  1. #
  2. # This script looks for files to appear in a
  3. # specific local directory and uploads them
  4. # to the FTP server. Once uploaded it moves
  5. # the files to another directory
  6. #
  7. # You can schedule this script to be run every
  8. # 5 minutes to automatically upload every file
  9. # that is copied to the folder
  10. #
  11.  
  12.  
  13. # The directory to watch for files
  14. $incoming_dir="C:\files_appear_here"
  15.  
  16. # Once the files are uploaded they are copied to here
  17. $processed_files_dir="C:\uploaded_files"
  18.  
  19. # Connect to the FTP server
  20. $result=OPENHOST("192.168.23.32","myusername","mypassword")
  21.  
  22. # If the connection has failed wait 5 seconds and close ScriptFTP
  23. IF($result!="OK")
  24.     SLEEP(5)
  25.     EXIT
  26.  
  27.  
  28. # Change current local directory
  29. LOCALCHDIR($incoming_dir)
  30.  
  31. # If cannot change current local directory wait 5 seconds and close ScriptFTP
  32. IF($result!="OK")
  33.     SLEEP(5)
  34.     EXIT
  35.  
  36.  
  37. # Retrieve the C:\pim\outbox file listing
  38. $result=GETLIST($list,LOCAL_FILES,"*.*")
  39.  
  40. # If cannot get the file listing wait 5 seconds and close ScriptFTP
  41. IF($result!="OK")
  42.     SLEEP(5)
  43.     EXIT
  44.  
  45. FOREACH $item IN $list
  46.     # Check the file size
  47.     $size1=GETFILESIZE(LOCAL,$item)
  48.    
  49.     # Wait for 1 second and check the file size again
  50.     SLEEP(1)
  51.  
  52.     # Check the file size
  53.     $size2=GETFILESIZE(LOCAL,$item)
  54.  
  55.     # Upload the file if the file is not being written (sizes are equal)
  56.     # and check also that GETFILESIZE did not returned an error (negative value)
  57.     IF($size1==$size2 AND $size1>=0 AND $size2>=0)
  58.         # Upload the file
  59.         $result=PUTFILE($item)
  60.         # If the file has been succesfully
  61.         #  uploaded move it to the other dir
  62.         IF($result=="OK")
  63.             EXEC("move /Y ".$item." ".$processed_files_dir)
  64.         END IF
  65.     END IF
  66.    
  67.  
  68. # Close the connection
Hello!
I'm looking for a FTP-program that can help me monitor a number of folders (right now about 20). When a .csv file appers a folder, I want it uploaded to an FTP-server (different servers and logins for each folder).

I think that the ScriptFTP and the script in this thread is close to what I'm looking for. But it worries me a bit that the script opens a connection even before it checks if there is anything to transmit. In my case there could be many unnessecary connections. So I would like a script like this, but that only opens a connection when something to transmit is found in the local folder.

Would it be OK to move the "OPENHOST" and CLOSEHOST to inside the IF($size1==$size2 AND $size1>=0 AND $size2>=0)... ...ENDIF? (Usually there will be only one .csv file to transmit).
Transfer to the script folder name, server address, user name and password as parameters on the command line. And create 20 jobs in the scheduler.
FTP Script
  1. #
  2. # This script looks for *.csv files to appear
  3. # in a specific local directory and uploads
  4. # them to the FTP server. Once uploaded it moves
  5. # the files to another directory
  6. #
  7. # You can schedule this script to be run every
  8. # 5 minutes to automatically upload every *.csv
  9. # file that is copied to the folder
  10. #
  11. # Use:
  12. # ScriptFTP.exe script_file.ftp incoming_dir processed_files_dir server user pass
  13.  
  14. # The directory to watch for files
  15. $incoming_dir=GETPARAM(3)
  16.  
  17. # Once the files are uploaded they are copied to here
  18. $processed_files_dir=GETPARAM(4)
  19.  
  20. # Change current local directory
  21. LOCALCHDIR($incoming_dir)
  22.  
  23. # If cannot change current local directory wait 5 seconds and close ScriptFTP
  24. IF($result!="OK")
  25.     SLEEP(5)
  26.     EXIT
  27.  
  28. # Retrieve the incoming_dir *.csv file listing
  29. $result=GETLIST($list,LOCAL_FILES,"*.csv")
  30.  
  31. # If cannot get the file listing wait 5 seconds and close ScriptFTP
  32. IF($result!="OK")
  33.     SLEEP(5)
  34.     EXIT
  35.  
  36. # If file list is empty wait 5 seconds and close ScriptFTP
  37. IF(COUNTELEMENTS($list)==0)
  38.     SLEEP(5)
  39.     EXIT
  40.  
  41. # Connect to the FTP server
  42.  
  43. # If the connection has failed wait 5 seconds and close ScriptFTP
  44. IF($result!="OK")
  45.     SLEEP(5)
  46.     EXIT
  47.  
  48. FOREACH $item IN $list
  49.     # Check the file size
  50.     $size1=GETFILESIZE(LOCAL,$item)
  51.  
  52.     # Wait for 1 second and check the file size again
  53.     SLEEP(1)
  54.  
  55.     # Check the file size
  56.     $size2=GETFILESIZE(LOCAL,$item)
  57.  
  58.     # Upload the file if the file is not being written (sizes are equal)
  59.     # and check also that GETFILESIZE did not returned an error (negative value)
  60.     IF($size1==$size2 AND $size1>=0 AND $size2>=0)
  61.         # Upload the file
  62.         $result=PUTFILE($item)
  63.         # If the file has been succesfully
  64.         #  uploaded move it to the other dir
  65.         IF($result=="OK")
  66.             EXEC ("move /Y ".$item." ".$processed_files_dir)
  67.         END IF
  68.     END IF
  69.  
  70.  
  71. # Close the connection
I am wanting to do a similar but opposite thing.
Is there a way to download a file once it has appeared in a remote folder?
I am sure it would require some sort of "listen" function.
Even checking every 10 mins would be sufficient.
Hi Tim. Thanks for pointing this out, it helped a lot.

I have replied your last post with this script modified:

http://www.scriptftp.com/forum/general-f6/upload-a-file-as-soon-as-it-appears-t520.html