2

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

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. So instead of file.xml I want pass beginning_of_the_string (without .xml). Then script should copy the first of these files?

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
user3146344
  • 207
  • 1
  • 3
  • 16

2 Answers2

1

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

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.


Or use another FTP client that supports wildcard matching locally.

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

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.

For details, see the guide to WinSCP scripting.

(I'm the author of WinSCP)

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
1

As Martin Prikryl said :

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.

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 :

@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
Hackoo
  • 18,337
  • 3
  • 40
  • 70