Wednesday 6 February 2019

windows - How do I check the outcome of a ftp script with a bat file?



I have a .bat file that runs a ftp script, grabbing files from the server and copying them to my machine. Works great.


I now want this .bat file to report if any of these errors occured.


If you could point me in the right direction to the general idea of how to do this, that would be great.


Thanks.



Answer



To be honest I've never had great success using fixed FTP scripts. Even if you deal with the return codes its hard to know exactly what went wrong.


I'd recommend using PowerShell or Python for the job instead. Both these have access to an FTP client that can be dynamically controlled. You'll know exactly whats worked or failed, and be able to deal with the issue then and there.


Simple Example in PS


$url = "ftp://ftp.foo.com/bar.txt"
$destination = "c:\foo\bar.txt"
$wc = New-Object System.Net.WebClient
$wc.DownloadFile($url, $destination)

If you need to do more complicated things I'd start by looking at http://msdn.microsoft.com/en-us/library/ms229718.aspx Those examples are in C#, but it should be pretty easy to convert them to PS.


A script built in this way can list a dir and loop over the files downloading the ones you want. If there are any errors you can handle them on a per file level how you like.


No comments:

Post a Comment

Where does Skype save my contact's avatars in Linux?

I'm using Skype on Linux. Where can I find images cached by skype of my contact's avatars? Answer I wanted to get those Skype avat...