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?
Hi,
How can I delete remote files from the ftp server after downloading them? I have 50 directories and within each directory there are 2 subdirectories in which the files that I need (to download then delete) exist. Here is what I have thus far...
FTP Script
  1. # Connect to FTP server
  2. OPENHOST("myftpsite.com","xx","xxxxx")
  3.  
  4. # Go to D:\mylocaldirectory
  5. LOCALCHDIR("D:\mylocaldirectory")
  6.  
  7. # Get all the files in the FTP server
  8. GETFILE("*.*",SUBDIRS)
  9.  
  10. #Change Remote Directory
  11. CHDIR("/")
  12.  
  13. # Delete all files after download
  14. DELETEFILE("*.*")
  15.  
  16. # Transfer finished, close the connection
  17.  
So my question is; how do I add the "SUBDIRS" command to the "DELETEFILE" command? Is this possible? When I run the script above it downloads all files appropriately; however when it gets to the "DELETEFILE" command, it returns the following...
DELETEFILE("*.*")
No remote file matches the wildcard
If I add the "SUBDIRS" command I get the following error...
Running D:\Program Files\ScriptFTP\templates\download.ftp
***** Script error on line 20, column 17: ")" expected
DELETEFILE("*.*",SUBDIRS)
^
The DELETEFILE command does not accept SUBDIRS. If you want to remove a
complete directory tree try RMDIR instead.

I suggest you to check the result of GETFILE before deleting the remote files. Something like:
FTP Script
  1. $result=GETFILE("*.*",SUBDIRS)
  2.  
  3. IF($result=="OK")
  4.     RMDIR("/")
If you have always two levels of subdirectories I think you can even try something like the example download_and_delete.ftp (http://www.scriptftp.com/examples.php?go=download_and_delete.ftp) of the script samples. The script will be a bit more complex but is also more powerful because it download, checks and deletes each file separately, not the complete directory tree.
Thanks, but I don't need to delete the actual directory or folder only the files within the folders (doesn't RMDIR remove the entire directory or folder)?

If SUBDIRS works with DELETEFILE, how do I do it? The error below is always generated when using SUBDIRS with DELETEFILE. Am I using improper syntax? I tried the following code
FTP Script
  1. DELETEFILE("*.*",SUBDIRS)
But I always get the following error
Running D:\Program Files\ScriptFTP\templates\download.ftp
***** Script error on line 20, column 17: ")" expected

DELETEFILE("*.*",SUBDIRS)
^
THANKS! :D
<me> The DELETEFILE command does accept SUBDIRS.
Oh, sorry. I typed it wrong. The DELETEFILE does not accept SUBDIRS.

<you> I don't need to delete the actual directory or folder only the files within the folders
Ok, I think I got it. I've written a script and pasted below. Please let me know if it is what you want:
FTP Script
  1. # Remote directory where the 50 subdirectories are located
  2. $remote_directory="/"
  3.  
  4. # Local directory where the files are downloaded
  5. $local_directory="D:\test"
  6.  
  7.  
  8. # Connect to server, put your own settings here
  9. $result=OPENHOST("127.0.0.1","carl","123456")
  10.  
  11. # If connection failed stop
  12. IF($result!="OK")
  13.     STOP
  14.  
  15.  
  16. # Change the current local directory.
  17. $result=LOCALCHDIR($local_directory)
  18.  
  19. # If LOCALCHDIR failed stop the script
  20. IF($result!="OK")
  21.     STOP
  22.  
  23. # Change the current remote directory.
  24. $result=CHDIR($remote_directory)
  25.  
  26. # If CHDIR failed stop the script
  27. IF($result!="OK")
  28.     STOP
  29.  
  30. # Once reached this point we are connected and ready
  31. # to browse the remote 50 directories with 2 subdirectories
  32. # in each one and a bunch of files in them ready for download.
  33.  
  34.  
  35. # Get the remote directory listing
  36. # it should retrieve 50 items.
  37. $result=GETLIST($list,REMOTE_DIRECTORIES)
  38.  
  39. # If GETLIST failed stop the script
  40. IF($result!="OK")
  41.     STOP
  42.  
  43. # For each directory in $list...
  44. FOREACH $directory IN $list
  45.     # Change current remote directory to that
  46.     # subdirectory
  47.     CHDIR($directory)
  48.  
  49.     # Create a local directory with the same name
  50.     # and change current local directory to it.
  51.     # LOCALMKDIR may fail because it already exists
  52.     LOCALMKDIR($directory)
  53.     LOCALCHDIR($directory)
  54.  
  55.     # Get the subdirectory listing. It should get
  56.     # two items
  57.     GETLIST($list2,REMOTE_DIRECTORIES)
  58.  
  59.     FOREACH $subdirectory IN $list2
  60.        # Change current remote directory to that
  61.        # subdirectory
  62.        CHDIR($subdirectory)
  63.  
  64.        # Create the local subdirectory with the same name
  65.        # and change current local directory to it.
  66.        # LOCALMKDIR may fail because it already exists
  67.        LOCALMKDIR($subdirectory)
  68.        LOCALCHDIR($subdirectory)
  69.  
  70.        # Get the file listing
  71.        GETLIST($list3,REMOTE_FILES)
  72.    
  73.        FOREACH $file IN $list3
  74.            # Download the file
  75.            $result=GETFILE($file)  
  76.  
  77.            # If the file has been succesfully downloaded
  78.            # delete the remote copy. If not stop the script.
  79.            IF($result=="OK")
  80.                DELETEFILE($file)
  81.            ELSE
  82.                STOP
  83.            END IF
  84.        END FOREACH
  85.  
  86.        # Return to parent directory
  87.        CHDIR("..")
  88.        LOCALCHDIR("..")
  89.     END FOREACH
  90.  
  91.  
  92.  
  93.     # Return to parent directory
  94.     CHDIR("..")
  95.     LOCALCHDIR("..")
  96.  
PERFECT!
I remarked out the create directory bit (the local directories always remain in-tact) - and it has been running for days without fail.
THANK YOU!!!
:D