Tuesday, March 20, 2012

Check FTP file date

First I want to thank everyone that has given help to me and everyone else with the issues involving migrating to 2005... Thanks alot..

Now for the problem. I am looking for (an not finding anything of help) to check the date of a file on an ftp server. A file always exists but once a month the day changes. I would just download the file and check it locally but the files are several hundred megs in size so that would be inefficient.

So is there anyway to do that?

On another note, can anyone point me to a good resource for learning the scripting language that SSIS uses?

This trhread sounds similar to your problem. See if it helps you.

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=993578&SiteID=1

|||

This is shooting straight from the hip, so I don't know how close this is for you:

The easiest way to get a file's timestamp is through the System.IO.FileInfo class. I can easily read lots of valuable information from files on our LAN using code like this:

Dim fi As System.IO.FileInfo
Dim CreationTime As Date

fi = New System.IO.FileInfo(<Path_to_file>)
CreationTime = fi.LastWriteTime

The .NET base classes are quite insensitive to "paths", so I can use UNC paths (like \\MyServer\MyShare\MyFolder\MyFile.txt) just as easily as referencing paths on my local machine ("C:\MyFolder\MyFile.txt").

FTP sites are usually protected by passwords and things, and your credentials are passed to the FTP server for processing before you can get at the files. If your files happen to be on your company's LAN, you may be able to use the FIleInfo class to get the info you need. If it's on someone else's network, or only accessible across the Internet, you'll probably need something a bit more heavy-duty.

It looks as though the SSIS FTP task is solely intended to grab files and bring 'em down to a network. I don't see any ability to pass an FTP command (like "LS" or "CWD") to an FTP server and read back a response. Maybe in a future version of SSIS, eh?

|||

the ftp connection (which does all the work for the FtpTask) is written in c++ using winInet call.

the managed System.Net.FtpWebRequest class could be used in a script task to send make a call to get the file info from the server, you would need to grab the connection info from the ftp connection manager and make your own connection though.

I've never use the class, perhaps someone out there has?

|||

Here is some code I found at: http://www.devasp.net/net/articles/display/246.html

The Code works to access an FTP site and write the detail file information (date, size, file/directory name) into a StreamReader (unfortunately, I am not knowledgeable enough to figure out how to get the info out of the StreamReader and do something with it!) I created the message box so I could see what the StreamReader returned...

It gets the information the OP wanted (date of file) from an FTP server. Some of the other guru's can probably assist with how to use the resulting stream (or tell us a better way to store the output from the ListDirectoryDetails method, beyond my capabilities right now)...

' Had to set Option Strict Off to allow:
' fwr = FtpWebRequest.Create(ftp://xxx.xxx.x.x)
' I don' t know how to change this statement to allow it to work with Option Strict On

Option Strict Off

Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Runtime
' Must have System.Net to use FtpWebRequest
Imports System.Net
' Must have System.IO to use StreamReader
Imports System.IO

Public Class ScriptMain
Public Sub Main()

Dim fwr As FtpWebRequest
fwr = FtpWebRequest.Create(ftp://xxx.xxx.x.x) ' or ftp://ftp.somewhere.com
fwr.Credentials = New NetworkCredential("userid", "password")
fwr.Method = WebRequestMethods.Ftp.ListDirectoryDetails

Dim sr As New StreamReader(fwr.GetResponse().GetResponseStream())
Dim str As String = sr.ReadLine()
While Not str Is Nothing
'Console.WriteLine(str)
MsgBox(str)
str = sr.ReadLine()
End While

sr.Close()
sr = Nothing
fwr = Nothing

Dts.TaskResult = Dts.Results.Success

End Sub
End Class

|||

Change the type or cast it to allow Option Strict On, which I highly recommend.

Dim fwr As WebRequest

fwr = FtpWebRequest.Create("ftp://xxx.xxx.x.x") ' or

Dim ftp As FtpWebRequest

ftp = CType(FtpWebRequest.Create("ftp://xxx.xxx.x.x"), FtpWebRequest)

No comments:

Post a Comment