[英]Batch file to download a file with name that starts with a given string from FTP
I use the following bat file:我使用以下bat文件:
::@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
Parameters and executions are like below:参数和执行如下:
c:\download.bat ftpHost login password file.xml FTP_FOLDER
As you can see this script copy file with the specific name.如您所见,此脚本复制文件具有特定名称。
How can I change this script to look for the first XML file that starts with a certain string.如何更改此脚本以查找以某个字符串开头的第一个 XML 文件。 So instead of
file.xml
I want pass beginning_of_the_string
(without .xml
).因此,而不是
file.xml
我想通过beginning_of_the_string
(不.xml
)。 Then script should copy the first of these files?那么脚本应该复制这些文件中的第一个吗?
The Windows ftp.exe
does not support wildcards on it own. Windows
ftp.exe
本身不支持通配符。 Though if you use a wildcard in mget
command, it will pass it unmodified to the server.但是,如果您在
mget
命令中使用通配符,它将未经修改地将其传递给服务器。 If the server supports wildcards (what is a non-standard, but common behaviour), it will allow ftp.exe
to download only the matching files.如果服务器支持通配符(这是一种非标准但常见的行为),它将允许
ftp.exe
仅下载匹配的文件。 See also FTP directory partial listing with wildcards .另请参阅带有通配符的 FTP 目录部分列表。
mget beginning_of_the_string*.xml
If your FTP server does not support wilcards, you can run the ftp.exe
in two phases.如果您的 FTP 服务器不支持通配符,您可以分两个阶段运行
ftp.exe
。 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.然后再次运行
ftp.exe
下载文件。 Check the answer by @Hackoo for an example of such implementation.检查@Hackoo 的答案以获取此类实现的示例。
Or use another FTP client that supports wildcard matching locally.或者使用其他支持本地通配符匹配的 FTP 客户端。
Eg with WinSCP you can do:例如,使用WinSCP,您可以执行以下操作:
@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
And you call it like:你这样称呼它:
c:\download.bat ftpHost login password beginning_of_the_string*.xml FTP_FOLDER
Instead of the beginning_of_the_string*.xml
, use any other file mask/wildcard that WinSCP supports .而不是
beginning_of_the_string*.xml
,使用WinSCP 支持的任何其他文件掩码/通配符。
For details, see the guide to WinSCP scripting .有关详细信息,请参阅WinSCP 脚本编写指南。
(I'm the author of WinSCP) (我是 WinSCP 的作者)
As Martin Prikryl said :正如Martin Prikryl所说:
You can run the ftp.exe in two phases.
您可以分两个阶段运行 ftp.exe。 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.
然后再次运行 ftp.exe 下载文件。
So you can give a try for this batch that can download from a public FTP Server : ftp.microsoft.com all ws*.doc files that start with this string ws as example :所以你可以试试这个可以从公共FTP 服务器下载的批处理文件:ftp.microsoft.com以该字符串ws开头的所有ws*.doc文件作为示例:
@echo off
mode con cols=85 lines=22 & Color A
::***********************************
Set FTPSERVER=ftp.microsoft.com
Title List files and folders on an FTP server (%FTPSERVER%) by Hackoo
Set USER=anonymous
Set Password=anonymous@anonymous.com
Set DossierFTP=/bussys/winsock/winsock2/
Set DownloadFolder=winsock2
Set BeginString=ws
Set ExtensionType=doc
::*******************************************************
Goto List
:List
> ft.do echo Open %FTPSERVER%
>> ft.do echo %USER%
>> ft.do echo %Password%
>> ft.do echo prompt n
>> ft.do echo bin
>> ft.do echo cd %DossierFTP%
>> ft.do echo ls -R TLIST.txt
>> ft.do echo bye
ftp -s:ft.do
del ft.do
CLS
Color 9B
echo Click to Download the files list
pause
Goto Download
::*********************************************************
:Download
Cls
> ft.do echo Open %FTPSERVER%
>> ft.do echo %USER%
>> ft.do echo %Password%
>> ft.do echo prompt n
>> ft.do echo bin
>> ft.do echo cd %DossierFTP%
findstr /r /i "%ExtensionType%" TLIST.txt > %ExtensionType%Files.txt
findstr /r /i "^%BeginString%" %ExtensionType%Files.txt > %ExtensionType%.txt
for /F %%f in (%ExtensionType%.txt) do ( >> ft.do echo get %%f)
>> ft.do echo bye
ftp -s:ft.do
del ft.do
CLS
Color 9A
echo Moving downloaded files to %DownloadFolder% Folder
pause
Goto MoveFiles
::*********************************************************
:MoveFiles
cls
echo Moving downloaded files to %DownloadFolder% Folder
Set Source=%~dp0
Set Destination=%Source%%DownloadFolder%
if not exist %DownloadFolder% MD %DownloadFolder%
for /F %%f in (%ExtensionType%.txt) do (move "%Source%%%f" "%Destination%")
pause
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.