mevasquez
August 25th, 2001, 06:57 PM
Trying to get the creation time of a file on a remove machine. I can ftp to the machine using wininet and get a listing of the files but what I also want to do is to get the creation date. I've tried the GetFileTime function,which works on a local file but not a remote file, FileTimeToSystemTime Here's a sample of the code
First the declare functions
private Declare Function InternetCloseHandle Lib "wininet.dll" (byval hInet as Long) as Integer
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
private Declare Function FileTimeToSystemTime Lib "kernel32" (lpFileTime as FILETIME, lpSystemTime as SystemTime) as Long
private Declare Function GetFileTime Lib "kernel32" (byval hfile as Long, lpCreationTime as FILETIME, lpLastAccessTime as FILETIME, lpLastWriteTime as FILETIME) as Long
private Declare Function OpenFile Lib "kernel32" (byval lpFileName as string, lpReOpenBuff as OFSTRUCT, byval wStyle as Long) as Long
private Declare Function hread Lib "kernel32" Alias "_hread" (byval hfile as Long, lpBuffer as Any, byval lBytes as Long) as Long
private Declare Function lclose Lib "kernel32" Alias "_lclose" (byval hfile as Long) as Long
Here's the sub
public Sub EnumFiles(hConnection as Long)
Dim pData as WIN32_FIND_DATA
Dim hFind as Long
Dim lRet as Long
Dim gfile as string
Dim hfile as Integer
Dim CreationTime as FILETIME
Dim LastAccessTime as FILETIME
Dim LastWriteTime as FILETIME
Dim SystemTime as SystemTime
Dim FileStruct as OFSTRUCT
Dim iRC as Integer
'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
gfile = Left(pData.cFileName, InStr(1, pData.cFileName, string(1, 0), vbBinaryCompare) - 1)
'me.lstSDirList.AddItem gfile
'Debug.print gfile
Do
If gfile = "." Or gfile = ".." then
lRet = InternetFindNextFile(hFind, pData)
gfile = Left(pData.cFileName, InStr(1, pData.cFileName, string(1, 0), vbBinaryCompare) - 1)
else
me.lstSDirList.AddItem gfile
'create a buffer
pData.cFileName = string(MAX_PATH, 0)
'find the next file
hfile = OpenFile(gfile, FileStruct, OF_READ Or OF_SHARE_DENY_NONE) 'THIS SHOULD EQUAL > 0 BUT I get -1
If hfile = 0 then
MsgBox "Can't open the file", vbExclamation
Exit Sub
End If
If GetFileTime(hfile, CreationTime, LastAccessTime, LastWriteTime) then
' massage time into format that we can use
If Not FileTimeToSystemTime(LastAccessTime, SystemTime) then
print "Year of file :" & SystemTime.wYear
print "Month of File :" & SystemTime.wMonth
print "Day of File :" & SystemTime.wDay
else
MsgBox "FileTimeToSystemTime Failed"
End If
else
'MsgBox "GetFileTime Failed"
End If
iRC = lclose(hfile)
lRet = InternetFindNextFile(hFind, pData)
gfile = Left(pData.cFileName, InStr(1, pData.cFileName, string(1, 0), vbBinaryCompare) - 1)
'if there's no next file, exit do
If lRet = 0 then
Exit Do
End If
Debug.print gfile
End If
Loop
'close the search handle
InternetCloseHandle hFind
End Sub
Am I missing something.
First the declare functions
private Declare Function InternetCloseHandle Lib "wininet.dll" (byval hInet as Long) as Integer
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
private Declare Function FileTimeToSystemTime Lib "kernel32" (lpFileTime as FILETIME, lpSystemTime as SystemTime) as Long
private Declare Function GetFileTime Lib "kernel32" (byval hfile as Long, lpCreationTime as FILETIME, lpLastAccessTime as FILETIME, lpLastWriteTime as FILETIME) as Long
private Declare Function OpenFile Lib "kernel32" (byval lpFileName as string, lpReOpenBuff as OFSTRUCT, byval wStyle as Long) as Long
private Declare Function hread Lib "kernel32" Alias "_hread" (byval hfile as Long, lpBuffer as Any, byval lBytes as Long) as Long
private Declare Function lclose Lib "kernel32" Alias "_lclose" (byval hfile as Long) as Long
Here's the sub
public Sub EnumFiles(hConnection as Long)
Dim pData as WIN32_FIND_DATA
Dim hFind as Long
Dim lRet as Long
Dim gfile as string
Dim hfile as Integer
Dim CreationTime as FILETIME
Dim LastAccessTime as FILETIME
Dim LastWriteTime as FILETIME
Dim SystemTime as SystemTime
Dim FileStruct as OFSTRUCT
Dim iRC as Integer
'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
gfile = Left(pData.cFileName, InStr(1, pData.cFileName, string(1, 0), vbBinaryCompare) - 1)
'me.lstSDirList.AddItem gfile
'Debug.print gfile
Do
If gfile = "." Or gfile = ".." then
lRet = InternetFindNextFile(hFind, pData)
gfile = Left(pData.cFileName, InStr(1, pData.cFileName, string(1, 0), vbBinaryCompare) - 1)
else
me.lstSDirList.AddItem gfile
'create a buffer
pData.cFileName = string(MAX_PATH, 0)
'find the next file
hfile = OpenFile(gfile, FileStruct, OF_READ Or OF_SHARE_DENY_NONE) 'THIS SHOULD EQUAL > 0 BUT I get -1
If hfile = 0 then
MsgBox "Can't open the file", vbExclamation
Exit Sub
End If
If GetFileTime(hfile, CreationTime, LastAccessTime, LastWriteTime) then
' massage time into format that we can use
If Not FileTimeToSystemTime(LastAccessTime, SystemTime) then
print "Year of file :" & SystemTime.wYear
print "Month of File :" & SystemTime.wMonth
print "Day of File :" & SystemTime.wDay
else
MsgBox "FileTimeToSystemTime Failed"
End If
else
'MsgBox "GetFileTime Failed"
End If
iRC = lclose(hfile)
lRet = InternetFindNextFile(hFind, pData)
gfile = Left(pData.cFileName, InStr(1, pData.cFileName, string(1, 0), vbBinaryCompare) - 1)
'if there's no next file, exit do
If lRet = 0 then
Exit Do
End If
Debug.print gfile
End If
Loop
'close the search handle
InternetCloseHandle hFind
End Sub
Am I missing something.