Get the date modified of a file on a FTP Site

AGenMIS
AGenMIS used Ask the Experts™
on
I am using the below VBScript code to get a file from an FTP site.  However, before I get the file, I'd like to check the file's date modified.  Is there a way to do this?  
Dim objFSO1
Dim objwrc
Dim FileName
Dim objShell, objExecObject, FTPStatus
Set objFSO1 = CreateObject("Scripting.FileSystemObject")
' The Jobs.txt file holds the commands to connect to the ftp server
Set objwrc = objFSO1.CreateTextFile("c:\Jobs.txt", True)
 
' Below is the name of the file we want to copy from the ftp site.
FileName = "MyFile.txt"
' Build the contents of the Jobs.txt file
objwrc.WriteLine "open theftpsite.com"
objwrc.WriteLine "MyUserName"
objwrc.WriteLine "MyPassword"
objwrc.WriteLine "get " & FileName & " C:\MyLocation\" & FileName
objwrc.WriteLine "quit"
Set objShell = CreateObject("WScript.Shell")
objShell.Run "ftp -s:" & "c:\Jobs.txt" & " c:\FTPLog.txt", 0, True
Set objFSO = Nothing
Set objMyFile = Nothing
Set objShell = Nothing
 
MsgBox("Program Complete")

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Hello, you can check the filemodified property with FSO like this example from here :

http://www.vbscriptonline.com/FileSystemObjects/Programming-the-FileSystemObject.asp


Dim fso, f1
Set fso = CreateObject("Scripting.FileSystemObject")
' Get a File object to query.
Set f1 = fso.GetFile("c:\detlog.txt")   
' Print information.
Response.Write "File last modified: " & f1.DateLastModified 

Open in new window

Author

Commented:
Thank you for your comment.  It is greatly appreciated.  However, this did not provide me with the date I needed.  Using your code snippet provided me with the date the file was copied to my c:\ drive.  It did not provide me with the date the file had on the FTP server before it was copied.  For example, the file I am copying from the FTP server had the date of 07/15/2009 8:15 AM on the FTP server.  However when I ran your snippet, it provided me with the date of 07/17/2009 10:21 AM.  So you see its giving me the date the file was copied not the actual date on the FTP server.  
Top Expert 2010
Commented:
You will probrably need to connect to the ftp server and get this information, one way would be to use FtpFindFirstFile() API.

Create new standard project.
Add one command button

Add following code to form:
Option Explicit
 
Private Const INVALID_HANDLE_VALUE As Long = (-1)
Private Const INTERNET_OPEN_TYPE_DIRECT As Long = 1
Private Const INTERNET_SERVICE_FTP As Long = 1
Private Const INTERNET_FLAG_PASSIVE As Long = &H8000000
 
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
 
Private Type WIN32_FIND_DATA_W
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName(520) As Byte
cAlternateFileName(28) As Byte
End Type
 
Private Type SYSTEMTIME
wYear As Integer
wMonth As Integer
wDayOfWeek As Integer
wDay As Integer
wHour As Integer
wMinute As Integer
wSecond As Integer
wMilliseconds As Integer
End Type
 
Private Declare Function InternetOpenW Lib "wininet" (ByVal lpszAgent As Long, ByVal dwAccessType As Long, ByVal lpszProxyName As Long, ByVal lpszProxyByPass As Long, ByVal dwFlags As Long) As Long
Private Declare Function InternetConnectW Lib "wininet" (ByVal hInternet As Long, ByVal lpszServerName As Long, ByVal nServerPort As Long, ByVal lpszUsername As Long, ByVal lpszPassword As Long, ByVal dwService As Long, ByVal dwFlags As Long, ByVal dwContext As Long) As Long
Private Declare Function FtpFindFirstFileW Lib "wininet" (ByVal hConnect As Long, ByVal lpszSearchFile As Long, lpFindFileData As WIN32_FIND_DATA_W, ByVal dwFlags As Long, ByVal dwContext As Long) As Long
Private Declare Function InternetCloseHandle Lib "wininet" (ByVal hInternet As Long) As Long
Private Declare Function FileTimeToSystemTime Lib "kernel32" (lpFileTime As FILETIME, lpSystemTime As SYSTEMTIME) As Long
 
Dim hOpen As Long
Dim hConn As Long
 
 
Public Function GetDateInfo(ByVal szFile As String) As String
  
  Dim hInternet As Long
  Dim w32 As WIN32_FIND_DATA_W
  Dim St As SYSTEMTIME
  'file times returned in the WIN32_FIND_DATA structure are in the
  'local time zone, not in a coordinated universal time (UTC) format.
  hInternet = FtpFindFirstFileW(hConn, StrPtr(szFile), w32, 0, 0)
  If hInternet <> 0 Then
  ' Convert FILETIME to SYSTEMTIME
  Call FileTimeToSystemTime(w32.ftLastWriteTime, St)
  ' Convert to string
  GetDateInfo = DateSerial(St.wYear, St.wMonth, St.wDay) + TimeSerial(St.wHour, St.wMinute, St.wSecond)
  ' Must close this handle every time to successfuly query again
  InternetCloseHandle hInternet
  End If
  
End Function
 
Public Sub Connect(ByVal szServer As String, ByVal szUser As String, ByVal szPass As String, ByVal Port As Long)
  
  ' prepare
  hOpen = InternetOpenW(0, INTERNET_OPEN_TYPE_DIRECT, 0, 0, 0)
 
  If hOpen = 0 Then
    Exit Sub
  End If
  
  ' attemp to connect
  hConn = InternetConnectW(hOpen, _
    StrPtr(szServer), Port, _
    StrPtr(szUser), _
    StrPtr(szPass), INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0)
  
  If hConn <> 0 Then
    Debug.Print "connected!"
  Else
    Debug.Print "failed to connect"
    Disconnect
  End If
 
End Sub
 
Public Sub Disconnect()
 
  ' cleanup
  If hOpen <> 0 Then InternetCloseHandle hOpen
  If hConn <> 0 Then InternetCloseHandle hConn
 
End Sub
 
Private Sub Command1_Click()
  
  ' return the string format date/time
  Debug.Print GetDateInfo("/index.html")
  
End Sub
 
Private Sub Form_Load()
 
  ' connection info
  Connect "ftp.server.com", "user", "pass", 21
 
End Sub
 
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
  
  ' disconnect
  Disconnect
  
End Sub

Open in new window

Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
Can this be tweaked to work with VBScript?
Top Expert 2010

Commented:
<< Can this be tweaked to work with VBScript >>>

No. However if it was compiled as an activex.dll it could be used in VBScript using CreateObject(). I'm not much of a scripting guy so I don't know exactly how one does this using scripts.

Author

Commented:
I'd really like to be able to do this strictly in VBScript.  Any additional help would be appreciated.

Author

Commented:
Is there a way I could use the MDTM command within FTP and capture the output from this command within VBScript?

Author

Commented:
My final solution was to use the following code to capture and process each line of output from the dir command in ftp.

Set objFSO1 = CreateObject("Scripting.FileSystemObject")
Set objwrc = objFSO1.CreateTextFile("c:\Jobs.txt", True)
objwrc.WriteLine "open theftpsite.com"
objwrc.WriteLine "MyUserName"
objwrc.WriteLine "MyPassWord"
objwrc.WriteLine "dir"
objwrc.WriteLine "quit"
Set objShell = CreateObject("WScript.Shell")
Set objExecObject = objShell.Exec("%comspec% /c c:\windows\system32\ftp.exe -s:c:\ Jobs.txt ")
' Process each line of output from the dir commmand
Do While Not objExecObject.StdOut.AtEndOfStream
      strText = objExecObject.StdOut.ReadLine()
      'I have complex string manipulation to grab the date info from the dir output here.
Loop

Author

Commented:
pilozite and egl1044 thank you for your input.  My final solution can be seen in my comments.  Again, thanks for the input.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial