1

I have made a script that does a really basic task, it connects to a remote FTP site, retrieves XML files and deletes them afterward.

The only problem is that in the past we lost files because they were added when the delete statement was run.

open ftp.site.com
username
password
cd Out
lcd "E:\FTP\Site"
mget *.XML
mdel *.XML

bye

To prevent this from happening, we want to put a script on the FTP server (rename-files.ps1). The script will rename the *.xml files to *.xml.copy.

The only thing is I have no clue how to run the script through my FTP connection.

3
  • Your cannot run a script using FTP. See How do I execute a script on another server using FTP in UNIX? - Unless you have another way to access the server than FTP. - Why don't you delete/rename just the files that were downloaded? That would be the correct solution. Commented May 23, 2018 at 14:00
  • I need to rename them before I download them the publishing of the xml's on the ftp site is an ongoing process. if I delete the XML's add the end of my script there is a chance new files will be deleted Commented May 23, 2018 at 14:00
  • Why? I understood that you want to prevent deleting file that where added during download. So again, delete only the files that were downloaded. And you do not need any renaming, let only executing any script. Commented May 23, 2018 at 14:02

1 Answer 1

0

Some, but very few, FTP servers support SITE EXEC command. In the very rare case that your FTP server does support it, you can use:

quote SITE EXEC powershell rename-files.ps1

Though, in most cases, you cannot execute anything on the FTP server, if FTP is the only way you can access the server. You would have to use another method to execute the script, like SSH, PowerShell Remoting, etc.


But your problem has other solutions:

  • rename files using FTP; or
  • delete only the files that were downloaded.

Both are doable, but you will need better FTP client than Windows ftp.exe.
See for example A WinSCP script to download, rename, and move files.


Or you can do it like:

  • Run ftp.exe once to retrieve list of files;
  • Process the list in PowerShell to generate an ftp script with get and del commands for specific files (without wildcard);
  • Run ftp.exe again with generated script.

Actually you do not need to use ftp.exe in PowerShell. .NET assembly has its own FTP implementation: FtpWebRequest.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.