Reputation: 93
I am trying to create a simple batch file(s) that a user can click and it will prompt the user for a begin date and end date (format yyyymmdd
) and will then ftp the files with a matching file name.
The file names are in the format yyyymmdd.3P.site.name_address.csv
. So if the user entered 20160101
and 20160103
, then it would ftp 3 files (the 3 that start with 20160101
, 20160102
, and 20160103
).
Since the date format in the filename is INT friendly, my thinking was to parse out the first 8 characters from the file names, and download any that are between the user input.
My problem is that I don't know how to store the 2 user inputs and compare to the parsed out file names. Here is my code at present:
test.bat
ftp -i -s:test.ftp
test.ftp
open ftp.somesite.com
username
password
lcd %UserProfile%\Desktop\ftptest
cd folderwithfiles
get 20160518.3P.site.name_address.csv
bye
Upvotes: 1
Views: 1175
Reputation: 202167
You have to run ftp
twice. First to get the listing. Then you parse the listing, find the files your want, generate new FTP script with one get
command per each matched file and run the ftp
again.
Something like this:
@echo off
set HOSTNAME=example.com
set USERNAME=user
set PASSWORD=pass
set SOURCE=/path
set /P FROM=Date from:
set /P TO=Date to:
echo open %HOSTNAME%> ls.txt
echo %USERNAME%>> ls.txt
echo %PASSWORD%>> ls.txt
echo cd %SOURCE%>> ls.txt
echo ls . list.txt>> ls.txt
echo bye>> ls.txt
ftp -s:ls.txt
del ls.txt
echo open %HOSTNAME%> get.txt
echo %USERNAME%>> get.txt
echo %PASSWORD%>> get.txt
echo cd %SOURCE%>> get.txt
rem Some servers will prefix all entries for "ls ." with "./"
rem For such servers, use the syntax with delims=/ to separate the filename from the prefix.
rem FOR /F "tokens=1,2 delims=/" %%i in (list.txt) do call :process %%j
rem But some servers do not add the prefix, for those this will do:
FOR /F %%i in (list.txt) do call :process %%i
echo bye>> get.txt
del list.txt
ftp -s:get.txt
del ls.txt
:process
set NAME=%1
set TIME=%NAME:~0,8%
if "%TIME%" geq "%FROM%" if "%TIME%" leq "%TO%" echo get %NAME%>>get.txt
exit /b
Or, if you can rely on file timestamps, you can use FTP client capable of selecting files based on a time range.
For example with WinSCP FTP client, the script can be as simple as:
set /P FROM=Date from:
set /P TO=Date to:
winscp.com /ini=nul /command ^
"open ftp://user:[email protected]/" ^
"get ""/path/*>=%FROM%<=%TO% 23:59:59""" ^
"exit"
The dates have to be entered in a format yyyy-mm-dd
.
References:
(I'm the author of WinSCP)
Upvotes: 1