1.

Solve : Download the most recent files from FTP?

Answer»

I am trying to create a batch file that will download the 5 most recent files from the FTP. The full file name will be unknown because they are generational files. The files will look like AAAA.AAAAAAAAA.daily.G0001, AAAA.AAAAAAAAA.daily.G0002, AAAA.AAAAAAAAA.daily.G0003 etc.
The dates on the files might all be the same but I need to download only the most recent files. IE G0002 and G0003.
Currently I have the batch file downloading the entire path with wild card characters.
here is how I am declaring which files to pull:

mget 'AAAA.AAAAAAAAA.daily*.*'

This pulls everything. I just need the 5 most recent. These appear on the bottom of the list in FTP but the entire list has the same date.

Any help WOULD be nice.
Thanks,
JC


To determine an unknown character, you can use ?

A better alternative to FTP is WSH for example:

@ECHO OFF
set v1=C:\folder
set var="temp.vbs"
set id=filename.g0001
set url=http://yourwebpage.com/folder/file.G0001
if not exist %id% md "%id%"
echo strFileURL = "%url%">%v1%\%var%
echo strHDLocation = "%v1%\%id%">>%v1%\%var%
echo Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP")>>%v1%\%var%
echo objXMLHTTP.open "GET", strFileURL, false>>%v1%\%var%
echo objXMLHTTP.send()>>%v1%\%var%
echo If objXMLHTTP.Status = 200 Then>>%v1%\%var%
echo Set objADOStream = CreateObject("ADODB.Stream")>>%v1%\%var%
echo objADOStream.Open>>%v1%\%var%
echo objADOStream.Type = 1 'adTypeBinary>>%v1%\%var%
echo objADOStream.Write objXMLHTTP.ResponseBody>>%v1%\%var%
echo objADOStream.Position = 0 >>%v1%\%var%
echo Set objFSO = Createobject("Scripting.FileSystemObject")>>%v1%\%var%
echo If objFSO.Fileexists(strHDLocation) Then objFSO.DeleteFile strHDLocation>>%v1%\%var%
echo Set objFSO = Nothing>>%v1%\%var%
echo objADOStream.SaveToFile strHDLocation>>%v1%\%var%
echo objADOStream.Close>>%v1%\%var%
echo Set objADOStream = Nothing>>%v1%\%var%
echo End if>>%v1%\%var%
echo Set objXMLHTTP = Nothing>>%v1%\%var%
%v1%\%var% &del /q/f %v1%\%var%


Now assuming the extention of each filename is in order, g0001, g0002, g0003, ect and stored to a folder you can use IF

@ECHO OFF
set v1=c:\pathtoyourfolder
if exist %v1%*.g000? (for /f "tokens=1*" %a in ('dir /a %v1%*.g000? /b') do set id=%a & if exist %v1%\%id% (call set /a h=%id:~-1,1%+1))
if not exist %v1%*.g000%h% (set var=tmp.vbs &set url=http://yoururlhere.com/folder/filename.%g000%h%&GOTO UPD) ELSE CLS&ECHO File ALREADY On HD&Pause&Goto xx
:UPD
echo strFileURL = "%url%">%v1%\%var%
echo strHDLocation = "%v1%\%id%">>%v1%\%var%
echo Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP")>>%v1%\%var%
echo objXMLHTTP.open "GET", strFileURL, false>>%v1%\%var%
echo objXMLHTTP.send()>>%v1%\%var%
echo If objXMLHTTP.Status = 200 Then>>%v1%\%var%
echo Set objADOStream = CreateObject("ADODB.Stream")>>%v1%\%var%
echo objADOStream.Open>>%v1%\%var%
echo objADOStream.Type = 1 'adTypeBinary>>%v1%\%var%
echo objADOStream.Write objXMLHTTP.ResponseBody>>%v1%\%var%
echo objADOStream.Position = 0 >>%v1%\%var%
echo Set objFSO = Createobject("Scripting.FileSystemObject")>>%v1%\%var%
echo If objFSO.Fileexists(strHDLocation) Then objFSO.DeleteFile strHDLocation>>%v1%\%var%
echo Set objFSO = Nothing>>%v1%\%var%
echo objADOStream.SaveToFile strHDLocation>>%v1%\%var%
echo objADOStream.Close>>%v1%\%var%
echo Set objADOStream = Nothing>>%v1%\%var%
echo End if>>%v1%\%var%
echo Set objXMLHTTP = Nothing>>%v1%\%var%
echo Set objXMLHTTP = Nothing>>%v1%\%var%
%v1%\%var%
if not exist (%v1%\%var% CLS&echo Update failed, File not available&Pause&GOTO XX) else CLS&Echo Update Succeeded&Pause&goto XX
:xx
exit


In the above example it checks to see the last file downloaded, and downloads the file beyond that you need only to set the variables to your liking it will tell you if it FAILS or succeeds



Discussion

No Comment Found