用于从 FTP 下载名称以给定字符串开头的文件的批处理文件 [英] Batch file to download a file with name that starts with a given string from FTP

查看:14
本文介绍了用于从 FTP 下载名称以给定字符串开头的文件的批处理文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下 bat 文件:

I use the following bat file:

::@echo off
cd /d %0.. 
set cmd=%CD%

echo user %~2> %cmd%ftpcmd.dat
echo %~3>> %cmd%ftpcmd.dat
echo bin>> %cmd%ftpcmd.dat
IF NOT "%5" == "" echo cd %~5>> %cmd%ftpcmd.dat
echo get %~4 %cmd%\%~4>> %cmd%ftpcmd.dat
echo quit>> %cmd%ftpcmd.dat
ftp -n -s:%cmd%ftpcmd.dat %1
del %cmd%ftpcmd.dat

参数和执行如下:

c:download.bat  ftpHost login password file.xml FTP_FOLDER

如您所见,此脚本复制文件具有特定名称.

As you can see this script copy file with the specific name.

如何更改此脚本以查找以某个字符串开头的第一个 XML 文件.因此,我想要传递 beginning_of_the_string(没有 .xml)而不是 file.xml.那么脚本应该复制这些文件中的第一个?

How can I change this script to look for the first XML file that starts with a certain string. So instead of file.xml I want pass beginning_of_the_string (without .xml). Then script should copy the first of these files?

推荐答案

Windows ftp.exe 本身不支持通配符.虽然如果您在 mget 命令中使用通配符,它​​会未经修改地将其传递给服务器.如果服务器支持通配符(这是一种非标准但常见的行为),它将允许 ftp.exe 仅下载匹配的文件.另请参阅使用通配符的FTP目录部分列表.

The Windows ftp.exe does not support wildcards on it own. Though if you use a wildcard in mget command, it will pass it unmodified to the server. If the server supports wildcards (what is a non-standard, but common behaviour), it will allow ftp.exe to download only the matching files. See also FTP directory partial listing with wildcards.

mget beginning_of_the_string*.xml

<小时>

如果您的 FTP 服务器不支持通配符,您可以分两个阶段运行 ftp.exe.首先列出远程目录.然后您在本地处理该列表以查找具有给定前缀的文件.然后再次运行 ftp.exe 下载文件.查看@Hackoo 的答案以获取此类实现的示例.


If your FTP server does not support wilcards, you can run the ftp.exe in two phases. First to list the remote directory. Then you locally process the list to find a file with given prefix. And then run the ftp.exe again to download the file. Check the answer by @Hackoo for an example of such implementation.

或者使用其他本地支持通配符匹配的FTP客户端.

Or use another FTP client that supports wildcard matching locally.

例如使用 WinSCP 你可以这样做:

E.g. with WinSCP you can do:

@echo off
cd /d %0.. 
set cmd=%CD%

echo open ftp://%~2:%~3@%1 > %cmd%ftpcmd.dat
IF NOT "%5" == "" echo cd %~5 >> %cmd%ftpcmd.dat
echo get %~4 %cmd% >> %cmd%ftpcmd.dat
echo exit >> %cmd%ftpcmd.dat
%cmd%winscp.com /script=%cmd%ftpcmd.dat
del %cmd%ftpcmd.dat

你这样称呼它:

c:download.bat ftpHost login password beginning_of_the_string*.xml FTP_FOLDER

使用任何其他 文件掩码/代替 beginning_of_the_string*.xmlWinSCP 支持的通配符.

Instead of the beginning_of_the_string*.xml, use any other file mask/wildcard that WinSCP supports.

有关详细信息,请参阅WinSCP 脚本编写指南.

For details, see the guide to WinSCP scripting.

(我是 WinSCP 的作者)

这篇关于用于从 FTP 下载名称以给定字符串开头的文件的批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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