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.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • Your cannot run a script using FTP. See [How do I execute a script on another server using FTP in UNIX?](https://stackoverflow.com/q/37461686/850848) - 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. – Martin Prikryl May 23 '18 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 – Joran van der Griend May 23 '18 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. – Martin Prikryl May 23 '18 at 14:02

1 Answers1

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.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992