如何脚本FTP上传和下载? [英] How to script FTP upload and download?

查看:210
本文介绍了如何脚本FTP上传和下载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建批处理文件以将文件上传到ftp服务器。
如果我手动输入它工作正常,但当我运行批处理文件它停止后,它连接...它说

I'm attempting to make a batch file to upload a file to ftp server. If I type it in manually it works fine but when i run the batch file it halts after it's connected... it says

connected to domain.com.

220 microsoft ftp server

User(domain.com:(none)):

那么没有别的。

以下是我的批处理文件:

Below is my batch file:

ftp www.domainhere.com 

user useridhere

passwordhere

put test.txt

bye

pause


推荐答案

想要以原始海报想象的方式来脚本化FTP会话,这是Expect会帮助的那种事情。 Windows上的批处理文件不能这样做。

It's a reasonable idea to want to script an FTP session the way the original poster imagined, and that is the kind of thing Expect would help with. Batch files on Windows cannot do this.

但是,与使用cURL或Expect不同,您可能会发现使用Powershell编写FTP脚本更容易。这是一个不同的模型,因为你不直接脚本化文本发送到FTP服务器。相反,您将使用Powershell来操作生成FTP对话的对象。

But rather than doing cURL or Expect, you may find it easier to script the FTP interaction with Powershell. It's a different model, in that you are not directly scripting the text to send to the FTP server. Instead you will use Powershell to manipulate objects that generate the FTP dialogue for you.

上传:

$File = "D:\Dev\somefilename.zip"
$ftp = "ftp://username:password@example.com/pub/incoming/somefilename.zip"

"ftp url: $ftp"

$webclient = New-Object System.Net.WebClient
$uri = New-Object System.Uri($ftp)

"Uploading $File..."

$webclient.UploadFile($uri, $File)

$File = "c:\store\somefilename.zip"
$ftp = "ftp://username:password@example.com/pub/outbound/somefilename.zip"

"ftp url: $ftp"

$webclient = New-Object System.Net.WebClient
$uri = New-Object System.Uri($ftp)

"Downloading $File..."

$webclient.DownloadFile($uri, $File)

您需要Powershell才能执行此操作。如果你不知道,Powershell是一个shell,如cmd.exe运行你的.bat文件。但Powershell运行.ps1文件,并且是相当有点更强大。 Powershell是Windows的免费插件,将内置到未来版本的Windows。 在此处

You need Powershell to do this. If you are not aware, Powershell is a shell like cmd.exe which runs your .bat files. But Powershell runs .ps1 files, and is quite a bit more powerful. Powershell is a free add-on to Windows and will be built-in to future versions of Windows. Get it here.

资料来源: http://poshcode.org/1134

这篇关于如何脚本FTP上传和下载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆