如何使用Internet Transfer控件获取FTP服务器上某文件的修改时间?

scie 2004-08-28 10:28:21
我想使下载到本地的文件与FTP服务器上的文件的修改时间一致,但使用"GET"获取的文件的修改时间为下载时间,请问如何获取FTP服务器上某文件的修改时间?谢谢了!
...全文
364 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
scie 2004-09-12
  • 打赏
  • 举报
回复
:rainstormmaster(暴风雨 v2.0):你好,谢谢你的帮助。我是用Inet API函数来做的,使用FtpFindFirstFile来获取相关信息,其中包含文件的修改时间。
rainstormmaster 2004-08-29
  • 打赏
  • 举报
回复
//我用remotehelp看了一下,远程主机不支持modtime命令啊。我用程序时间操作的时候,操作结果一直不能返回。

即使你的远程主机支持modtime命令,得到文件最后修改时间也要费很多事,在写ftp程序的时候,用winsock是最好的,不过对你的要求也是最高的
rainstormmaster 2004-08-29
  • 打赏
  • 举报
回复
GetHeader 方法只能用于HTTP协议。
online 2004-08-29
  • 打赏
  • 举报
回复
用API函数或者winsock都可以
Internet Transfer Control再试试
scie 2004-08-29
  • 打赏
  • 举报
回复
用API函数倒是可以实现,我用VB的目的就是为了使用Internet Transfer Control使问题简单一些,要是使用API的话我干脆用VC做算了。
scie 2004-08-29
  • 打赏
  • 举报
回复
rainstormmaster(暴风雨 v2.0) : 我用remotehelp看了一下,远程主机不支持modtime命令啊。我用程序时间操作的时候,操作结果一直不能返回。
rainstormmaster 2004-08-29
  • 打赏
  • 举报
回复
Option Explicit
Const FTP_TRANSFER_TYPE_UNKNOWN = &H0
Const FTP_TRANSFER_TYPE_ASCII = &H1
Const FTP_TRANSFER_TYPE_BINARY = &H2
Const INTERNET_DEFAULT_FTP_PORT = 21 ' default for FTP servers
Const INTERNET_SERVICE_FTP = 1
Const INTERNET_FLAG_PASSIVE = &H8000000 ' used for FTP connections
Const INTERNET_OPEN_TYPE_PRECONFIG = 0 ' use registry configuration
Const INTERNET_OPEN_TYPE_DIRECT = 1 ' direct to net
Const INTERNET_OPEN_TYPE_PROXY = 3 ' via named proxy
Const INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY = 4 ' prevent using java/script/INS
Const MAX_PATH = 260
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Private Type WIN32_FIND_DATA
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 As String * MAX_PATH
cAlternate As String * 14
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 FileTimeToSystemTime Lib "kernel32" (lpFileTime As FILETIME, lpSystemTime As SYSTEMTIME) As Long
Private Declare Function InternetCloseHandle Lib "wininet" (ByRef hInet As Long) As Long
Private Declare Function InternetConnect Lib "wininet.dll" Alias "InternetConnectA" (ByVal hInternetSession As Long, ByVal sServerName As String, ByVal nServerPort As Integer, ByVal sUserName As String, ByVal sPassword As String, ByVal lService As Long, ByVal lFlags As Long, ByVal lContext As Long) As Long
Private Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" (ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As String, ByVal sProxyBypass As String, ByVal lFlags As Long) As Long
Private Declare Function FtpSetCurrentDirectory Lib "wininet.dll" Alias "FtpSetCurrentDirectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
Private Declare Function FtpGetCurrentDirectory Lib "wininet.dll" Alias "FtpGetCurrentDirectoryA" (ByVal hFtpSession As Long, ByVal lpszCurrentDirectory As String, lpdwCurrentDirectory As Long) As Long
Private Declare Function FtpCreateDirectory Lib "wininet.dll" Alias "FtpCreateDirectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
Private Declare Function FtpRemoveDirectory Lib "wininet.dll" Alias "FtpRemoveDirectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
Private Declare Function FtpDeleteFile Lib "wininet.dll" Alias "FtpDeleteFileA" (ByVal hFtpSession As Long, ByVal lpszFileName As String) As Boolean
Private Declare Function FtpRenameFile Lib "wininet.dll" Alias "FtpRenameFileA" (ByVal hFtpSession As Long, ByVal lpszExisting As String, ByVal lpszNew As String) As Boolean
Private Declare Function FtpGetFile Lib "wininet.dll" Alias "FtpGetFileA" (ByVal hConnect As Long, ByVal lpszRemoteFile As String, ByVal lpszNewFile As String, ByVal fFailIfExists As Long, ByVal dwFlagsAndAttributes As Long, ByVal dwFlags As Long, ByRef dwContext As Long) As Boolean
Private Declare Function FtpPutFile Lib "wininet.dll" Alias "FtpPutFileA" (ByVal hConnect As Long, ByVal lpszLocalFile As String, ByVal lpszNewRemoteFile As String, ByVal dwFlags As Long, ByVal dwContext As Long) As Boolean
Private Declare Function InternetGetLastResponseInfo Lib "wininet.dll" Alias "InternetGetLastResponseInfoA" (lpdwError As Long, ByVal lpszBuffer As String, lpdwBufferLength As Long) As Boolean
Private Declare Function FtpFindFirstFile Lib "wininet.dll" Alias "FtpFindFirstFileA" (ByVal hFtpSession As Long, ByVal lpszSearchFile As String, lpFindFileData As WIN32_FIND_DATA, ByVal dwFlags As Long, ByVal dwContent As Long) As Long
Private Declare Function InternetFindNextFile Lib "wininet.dll" Alias "InternetFindNextFileA" (ByVal hFind As Long, lpvFindData As WIN32_FIND_DATA) As Long
Const PassiveConnection As Boolean = True
Private Sub Form_Load()
Dim hConnection As Long, hOpen As Long, sOrgPath As String
'open an internet connection
hOpen = InternetOpen("API-Guide sample program", INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0)
'connect to the FTP server
hConnection = InternetConnect(hOpen, "your ftp server", INTERNET_DEFAULT_FTP_PORT, "your login", "your password", INTERNET_SERVICE_FTP, IIf(PassiveConnection, INTERNET_FLAG_PASSIVE, 0), 0)
'create a buffer to store the original directory
sOrgPath = String(MAX_PATH, 0)
'get the directory
FtpGetCurrentDirectory hConnection, sOrgPath, Len(sOrgPath)
'create a new directory 'testing'
FtpCreateDirectory hConnection, "testing"
'set the current directory to 'root/testing'
FtpSetCurrentDirectory hConnection, "testing"
'upload the file 'test.htm'
FtpPutFile hConnection, "C:\test.htm", "test.htm", FTP_TRANSFER_TYPE_UNKNOWN, 0
'rename 'test.htm' to 'apiguide.htm'
FtpRenameFile hConnection, "test.htm", "apiguide.htm"
'enumerate the file list from the current directory ('root/testing')
EnumFiles hConnection
'retrieve the file from the FTP server
FtpGetFile hConnection, "apiguide.htm", "c:\apiguide.htm", False, 0, FTP_TRANSFER_TYPE_UNKNOWN, 0
'delete the file from the FTP server
FtpDeleteFile hConnection, "apiguide.htm"
'set the current directory back to the root
FtpSetCurrentDirectory hConnection, sOrgPath
'remove the direcrtory 'testing'
FtpRemoveDirectory hConnection, "testing"
'close the FTP connection
InternetCloseHandle hConnection
'close the internet connection
InternetCloseHandle hOpen
End Sub
Public Sub EnumFiles(hConnection As Long)
Dim mfile As String
Dim systime As SYSTEMTIME
Dim pData As WIN32_FIND_DATA, hFind As Long, lRet As Long
'set the graphics mode to persistent
Me.AutoRedraw = True
'create a buffer
pData.cFileName = String(MAX_PATH, 0)
'find the first file
hFind = FtpFindFirstFile(hConnection, "*.*", pData, 0, 0)
'if there's no file, then exit sub
If hFind = 0 Then Exit Sub
'show the filename
mfile = Left(pData.cFileName, InStr(1, pData.cFileName, String(1, 0), vbBinaryCompare) - 1)
FileTimeToSystemTime pData.ftLastWriteTime, systime
Debug.Print "文件" + mfile + "最后修改时间为:" + CStr(systime.wYear) + "年" + CStr(systime.wMonth) + "月" + CStr(systime.wDay) + "日 " + CStr(systime.wHour) + ":" + CStr(systime.wMinute)
Do
'create a buffer
pData.cFileName = String(MAX_PATH, 0)
'find the next file
lRet = InternetFindNextFile(hFind, pData)
'if there's no next file, exit do
If lRet = 0 Then Exit Do
'show the filename
mfile = Left(pData.cFileName, InStr(1, pData.cFileName, String(1, 0), vbBinaryCompare) - 1)
FileTimeToSystemTime pData.ftLastWriteTime, systime
Debug.Print "文件" + mfile + "最后修改时间为:" + CStr(systime.wYear) + "年" + CStr(systime.wMonth) + "月" + CStr(systime.wDay) + "日 " + CStr(systime.wHour) + ":" + CStr(systime.wMinute)
Loop
'close the search handle
InternetCloseHandle hFind
End Sub
Sub ShowError()
Dim lErr As Long, sErr As String, lenBuf As Long
'get the required buffer size
InternetGetLastResponseInfo lErr, sErr, lenBuf
'create a buffer
sErr = String(lenBuf, 0)
'retrieve the last respons info
InternetGetLastResponseInfo lErr, sErr, lenBuf
'show the last response info
MsgBox "Error " + CStr(lErr) + ": " + sErr, vbOKOnly + vbCritical
End Sub
online 2004-08-28
  • 打赏
  • 举报
回复
Inet1.Execute "http://yang/xml/1.bmp"
str = Inet1.GetHeader("Last-modified")
得到的时间是格林时间转换成北京时间
str = Replace(Right(str, Len(str) - InStr(1, str, ",") - 1), " GMT", "")
Text1.Text = CDate(Format(str, "yyyy/mm/dd hh:mm:ss")) + CDate(Format("08:00:00", "hh:mm:ss"))
rainstormmaster 2004-08-28
  • 打赏
  • 举报
回复
呵呵,对ftp命令不熟吧:

FTP命令是Internet用户使用最频繁的命令之一,不论是在DOS还是UNIX操作
系统下使用FTP,都会遇到大量的FTP内部命令,熟悉并灵活应用FTP的内部命令,可
以大大方便使用者,对于现在拨号上网的用户,如果ISP提供了shell可以使用noh
up,那么ftp将是你最省钱的上download方式,
ftp的命令行格式为:ftp -v -d -i -n -g[主机名]  
-v 显示远程服务器的所有响应信息。
-d 使用调试方式。   
-n 限制ftp的自动登录,即不使用.netrc文件。
-g 取消全局文件名。


ftp使用的内部命令如下(其中括号表示可选项):
  1.![cmd[args]在本地机中执行交互shell、exit回到ftp环境,如!ls*.zip 。
  2.¥ macro-ame[args]执行宏定义macro-name。
  3.account[password]提供登录远程系统成功后访问系统资源所需的补充口令 。
  4.appendlocal-file[remote-file]将本地文件追加到远程系统主机,若未指定远程系统文件名,则使用本地文件名。
  5.ascii 使用ascii类型传输方式。
  6.bell每个命令执行完毕后计算机响铃一次。
  7.bin使用二进制文件传输方式。
  8.bye退出ftp会话过程。
  9.case在使用mget时,将远程主机文件名中的大写转为小写字母。
  10.cd remote-dir 进入远程主机目录。
  11.cdup进入远程主机目录的父目录。
  12.chmod modefile-name将远程主机文件file-name的存取方式设置为mode,如chmod 777 a.out。
  13.close中断与远程服务器的ftp会话(与open对应)。
  14.cr使用asscii方式传输文件时,将回车换行转换为回行。
  15.delete remote-file删除远程主机文件。
  16.debug[debug-value]设置调试方式,显示发送至远程主机的每条命令,如debup3,若 设为0,表示取消debug。
  17.dir[remote-dir][local-file]显示远程主机目录,并将结果存入local-file。
  18.disconnection同close。
  19.form format将文件传输方式设置为format,缺省为file方式。
  20.getremote-file[local-file]将远程主机的文件remote-file传至本地硬盘的local-file。
  21.glob设置mdelete、mget、mput的文件名扩展,缺省时不扩展文件名,同命令行的-g参数。
  22.hash每传输1024字节,显示一个hash符号(#)。
  23.help[cmd]显示ftp内部命令cmd的帮助信息,如help get。
  24.idle[seconds]将远程服务器的休眠计时器设为[seconds]秒。
  25.image设置二进制传输方式(同binary)
  26.lcd[dir]将本地工作目录切换至dir。
  27.ls[remote-dir][local-file]显示远程目录remote-dir,并存入本地local-file。
  28.macdef macro-name定义一个宏,遇到macdef下的空行时,宏定义结束。
 29.mdelete[remote-file]删除远程主机文件。
  30.mdir remote-files local-file与dir类似,但可指定多个远程文件,如mdir*.o.*. zipoutfile。
  31.mget remote-files传输多个远程文件。
  32.mkdir dir-name 在远程主机中建一目录。
  33.mls remote-file local-file同nlist,但可指定多个文件名。
  34.mode[mode-name]将文件传输方式设置为mode-name,缺省为stream方式。
  35.modtime file-name显示远程主机文件的最后修改时间。
  36.mput local-file将多个文件传输至远程主机。
  37.newerfile-name如果远程机中file-name的修改时间比本地硬盘同名文件的时间更近,则重传该文件。
  38.nlist[remote-dir][local-file]显示远程主机目录的文件清单,并存入本地硬盘的local-file。
  39.nmap[inpatternoutpattern]设置文件名映射机制,使得文件传输时,文件中的某些字符相互转换,
如nmap¥1.¥2.¥3[¥1,¥2].[¥2,¥3],则传输文件a1 .a2.a3时,文件名变为a1、a2,
该命令特别适用于远程主机为非U-NIX机的情况。
40.ntrans[inchars[outchars]设置文件名字符的翻译机制,如ntrans1R,则文件名LL L将变为RRR。
  41.open host[port]建立指定ftp服务器连接,可指定连接端口。   
42.passive进入被动传输方式。
  43.prompt设置多个文件传输时的交互提示。
  44.proxyftp-cmd在次要控制连接中,执行一条ftp命令,该命令允许连接两个ftp服务器,以在两个服务器间传输文件。
第一条ftp命令必须为open,以首先建立两个服务器间的连接。
  45.put local-file[remote-file]将本地文件local-file传送至远程主机。
  46.pwd显示远程主机的当前工作目录。
  47.quit同bye,退出ftp会话。
  48.quote arg1,arg2……将参数逐字发至远程ftp服务器,如quote syst。
  49.recv remote-file[local-file]同get。
  50.regetremote-file[local-file]类似于get,但若local-file存在,则从上次传输中断处续传。
  51.rhelp[cmd-name]请求获得远程主机的帮助。
  52.rstatus[file-name]若未指定文件名,则显示远程主机的状态,否则显示文件状态。
  53.rename[from][to]更改远程主机文件名。   
54.reset清除回答队列。
  55.restart marker从指定的标志marker处,重新开始get或put,如restart 130。
  56.rmdir dir-name删除远程主机目录。
  57.runique设置文件名唯一性存储,若文件存在,则在原文件后加后缀。
  58.send local-file[remote-file]同put。
  59.sendport设置PORT命令的使用。
  60.site arg1,arg2……将参数作为SITE命令逐字发送至远程ftp主机。
  61.size file-name显示远程主机文件大小,如site idle 7200。
  62.status显示当前ftp状态。
  63.struct[struct-name]将文件传输结构设置为struct-name,缺省时使用stream结构。
64.sunique将远程主机文件名存储设置为唯一(与runique对应)。
  65.system显示远程主机的操作系统类型。
  66.tenex将文件传输类型设置为TENEX机所需的类型。
  67.tick设置传输时的字节计数器。
  68.trace设置包跟踪。
  69.type[type-name]设置文件传输类型为type-name,缺省为ascii,如typebinary,设置 二进制传输方式。
  70.umask[newmask]将远程服务器的缺省umask设置为newmask,如umask 3。
71.useruser-name[password][account]向远程主机表明自己的身份,需要口令时,必须输入口令,如user anonymous my@email。
  72.verbose同命令行的-v参数,即设置详尽报告方式,ftp服务器的所有响应都将显示给用户,缺省为on.
  73.?[cmd]同help。

1,451

社区成员

发帖
与我相关
我的任务
社区描述
VB 控件
社区管理员
  • 控件
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧