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

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

问题描述

我正在尝试制作一个批处理文件以将文件上传到 FTP 服务器.如果我手动输入它可以正常工作,但是当我运行批处理文件时,它会在连接后停止......它说:

I'm attempting to make a batch file to upload a file to an 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)):

然后没有别的了.这是怎么回事?

And then nothing else. What is going on here?

下面是我的批处理文件:

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 相比,您可能会发现编写 FTP 与 PowerShell 交互的脚本更容易.这是一个不同的模型,因为您不是直接编写要发送到 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:Devsomefilename.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:storesomefilename.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 是一个类似于 cmd.exe 的 shell,它运行您的 .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天全站免登陆