# Solved: DOS batch ftp - download today's files



## thmpr52

Thanks to TheOutcaste in a recent post I was able to create the first sequence of batch files for a work project. This second part I am wondering if I can perform in one script.

I am trying to access an FTP server and pull only today's files. If I try to separate the two processes and pull the files first, all the files will have today's date.

Below is the initial ftp script thus far:

_contents of ftp.bat_
::FTP Encrypted Files from Company X
ftp -s:ftp.txt

_contents of ftp.txt_
open <ftp address>
<username>
<password>
lcd <local folder>
cd <remote folder>
binary
prompt
mget R*.pgp
quit

So is there any way to incorporate the regular dos bat script below into the ftp above?

::Copy Files Made Or Modified Today
@echo off
setlocal
set source= 
set dest= <destination directory>
pushd "%source%"
set t=%date:~4%
::for /f %%a in ('dir /b /a-d /o-d') do call ROCESS "%%a"
goto :eof
popd
ROCESS
for /f %%j in ('echo %~t1') do set d=%%j
if "%d%"=="%t%" copy %1 "%dest%"
goto :eof

Thank you in advance for your assistance!


----------



## TheOutcaste

The FTP Server would have to provide someway of only getting files for a specific date. I'm not aware of a way to do that.

What you'll need to do is use two FTP sessions, one to get a list of files which will be used to create a script for the 2nd FTP session that will use the get command to retrieve the files that match the requirements.

The following should do that. It depends on the format that your FTP server uses to output the *Dir* command.
My ISPs server outputs like this:


Code:


[COLOR=Red]d[/COLOR]rwxr-xr-x    2 1578902                    1001                   4096 Oct 19 [COLOR=Red]20:11[/COLOR] SystemTools
-rw-r--r--    1 1578902                    1001                4096000 Nov  8  [COLOR=Red]2007[/COLOR] sp3.part01.exe
-rw-r--r--    1 1578902                    1001                3512560 Sep 11 22:17 sp3.vol11+9.PAR2

On some FTP servers the dir command may give a brief list, which only lists file names. In that case, you can use ls -lt. The output may be different (on mine the 1001 is not listed) so the For loop may need tweaking. Also, you can't specify both options and a file pattern, so if you must use the ls command, the batch file will have to sift out the *R*.pgp* files.
The dates are the File Modified times. Dates are shown as Month, Day and Time for files less than 1 year old. Files older than 1 year are Month, Day, and Year. The flags will have a *d* as the first character for directories, so the find and findstr statements in the For loop first list all files that have a time (the "*:*") and excludes directories (first character *d*).



Code:


@Echo Off
Setlocal EnableDelayedExpansion
Set lcd=<local folder>
pushd %lcd%
Set t=%date:~4%
Set _months=JanFebMarAprMayJunJulAugSepOctNovDec
For /F "TOKENS=1-3* DELIMS=/" %%B In ("%t%") Do (Set _tmonth=%%B)&Set _tdate=%%C
If %_tmonth:~0,1%==0 Set _tmonth=%_tmonth:~1,1%
If %_tdate:~0,1%==0 Set _tdate=%_tdate:~1,1%
Set /A _tmonth=(%_tmonth%-1)*3
Set _nmonth=!_months:~%_tmonth%,3!
:: FTP Encrypted Files from Company X
:: Get list of files
ftp -s:ftp1.txt
:: Create script to get files created today
>ftp2.txt Echo open <ftp address>
>>ftp2.txt Echo <username>
>>ftp2.txt Echo <password>
>>ftp2.txt Echo lcd <local folder>
>>ftp2.txt Echo cd <remote folder>
>>ftp2.txt Echo binary
For /F "tokens=6-8*" %%I In ('find ":" ftpdir.txt ^|findstr /B /V d') Do If %%J==%_tdate% If %%I==%_nmonth% Echo get %%L >>ftp2.txt
>>ftp2.txt Echo quit
ftp -s:ftp2.txt
popd

contents of ftp1.txt. This file logs in, sets the directories, then outputs a list into the ftpdir.txt file, which will be saved in the local directory.
open <ftp address>
<username>
<password>
lcd <local folder>
cd <remote folder>
ascii
dir R*.pgp ftpdir.txt
quit

contents of ftp2.txt. This file is created on the fly, creating individual get statements for each file the For loop finds.
open <ftp address>
<username>
<password>
lcd <local folder>
cd <remote folder>
binary
get <file1.pgp>
get <file2.pgp>
get <file3.pgp>
.
.
.
quit

HTH

Jerry


----------



## Squashman

I think you could do something like this with Fileaze real easy but it costs $30.
http://www.fileaze.com/features/


----------



## thmpr52

So for the ftp2.txt, I would need to populate the filenames that result from the FOR loop that ran in ftp1.txt?


----------



## TheOutcaste

The for loop will create the list in ftp2 automatically, and then execute the script, that's the *Echo get %%L >>ftp2.txt* part at then end of the For loop.

Jerry


----------



## thmpr52

Sorry for my delayed reply. I did a few tests and it worked perfectly. Thank you once again so much TheOutcaste!!!


----------

