Get Timestamp of file using WinInet

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 14099
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Get Timestamp of file using WinInet

Post by ChrisGreaves »

Forum?
I am writing in VBA using the WinInet functions.
My business is uploading HTML and JPG files FROM the laptop TO the FTP server.

I cannot find a way to determine the datetime-last-modified of a file on the server, but suppose that if I had such a value I could save the application some time by NOT uploading JPG files where an up-to-date copy exists on the server.

I translate MSWord documents into HTML, so a compiled page will always be more recent than the previous version; the application is in control of the DOCument being compiled.
But the images within that page may have been edited independently since the last time the document was compiled, in which case I should upload the edited image.
If the LastModified stamp on the server matches my FileDateTime on the laptop, there is no need to upload the file.

I would consider a solution that produces a list of the FTP directory at the start of my job; I could locate a JPG file name in that list and compare the timestamp that way.

I am asking here before heading over to StackOverflow.

Thanks, Chris
A watched pot never boils over

User avatar
SpeakEasy
3StarLounger
Posts: 319
Joined: 27 Jun 2021, 10:46

Re: Get Timestamp of file using WinInet

Post by SpeakEasy »

If you are using WinInet, then consider looking at the FtpCommand function. And the command 'LIST' or, better (if supported on your FTP Server) "MLST"

Possibly better yet, have a look at FTPFindFirstFile and InternetFindNextFile to loop through the directory, populating the LPWIN32_FIND_DATA structure which includes file date/timestamps (including creation and last access dates - but these are not actually really supported by standard FTP, so wininet makes best guess 'based on available information" - typically the modified date)

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 14099
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Get Timestamp of file using WinInet

Post by ChrisGreaves »

SpeakEasy wrote:
18 Nov 2022, 20:06
If you are using WinInet, then consider looking at the FtpCommand function. And the command 'LIST' or, better (if supported on your FTP Server) "MLST"
Possibly better yet, have a look at FTPFindFirstFile and InternetFindNextFile to loop through the directory, populating the LPWIN32_FIND_DATA structure
SpeakEasy, thanks for this. I gave it a shot last night (failed!) and then tried again this morning (with a slightly older but supposedly fresher brain) (failed).
Untitled4.png
I have attached my code module; the procedure in question is the last one in the module.

I think that "Bad Call ing Convention" is almost certainly a reflection on my misuse of parameters.

I tried two distinct functions - FTPFindFirstFile and the misleading FindFirstFile. I assume that the second is a cover function that saves the user having to code various CONNECT code, but for my part, I connect to the site at the start of the macro, and Close the site connection at the end, anything from ten seconds to twenty minutes later, so the lower-level "FtpFindFirstFileA" should satisfy me.

I think my problem is either the way I am calling FtpFindFirstFileA, or perhaps an error in the TYPE structure (top of my attached module).

I did try the command function (LIST and MLST), but couldn't get them to work. In general I try to stick to generic code; I figure that I might get the application to work with command function on MY ftp server, but then it might not work when someone else tries to use it with a different server.

Thanks for any tips or advice
Chris
You do not have the required permissions to view the files attached to this post.
A watched pot never boils over

User avatar
SpeakEasy
3StarLounger
Posts: 319
Joined: 27 Jun 2021, 10:46

Re: Get Timestamp of file using WinInet

Post by SpeakEasy »

Have a look at the following (an extension of your code with some corrections to declarations and some additional stuff). Rapidly put together but should be enough to enable you to progress.
working.txt
You do not have the required permissions to view the files attached to this post.

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 14099
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Get Timestamp of file using WinInet

Post by ChrisGreaves »

SpeakEasy wrote:
19 Nov 2022, 15:24
working.txt
Thank you SpeakEasy
Untitled5.png
I ran your code as-is and saw that it worked. :clapping: :clapping: :clapping:
I re-instituted my site-connection procedure and saw that that worked (image above). :cheers:
I shall now spend a pleasant Saturday night poring (and pawing) over the entrails to see where I went wrong. Probably another couple of questions coming back at you.

It was not until I chose "Preview" on this post that I noticed the time stamp on the file I decided to PrtScr!

Meanwhile I thought of a third method - given a local file, download a copy from the server and compare the contents. If unequal, then my local copy is almost certainly more recent, so it won't hurt to re-upload it. I noted in the MSoft documentation that server times are converted to Local times, so that wouldn't hurt me at all. (It is not unusual for me to recompile a web page several times within the hour)

This would work for me because in my application I am uploading {IncludePicture} files, none, one, up to forty in any document. My image files are usually less than 500Kb, so the cost of the download is not significant.

A further characteristic of my application is that I am compiling web pages, with up to forty images, so to my mind it makes more sense to download a folder listing at the start of the macro, and use it for in-house lookup for the duration of that page.

Thanks Again
Chris
You do not have the required permissions to view the files attached to this post.
A watched pot never boils over

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 14099
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Get Timestamp of file using WinInet

Post by ChrisGreaves »

SpeakEasy wrote:
19 Nov 2022, 15:24
Have a look at the following (an extension of your code with some corrections to declarations and some additional stuff)....
Hi SpeakEasy. So far my main problem seems to have been in the structure WIN32_FIND_DATA. I have adopted your structure as is.

Code: Select all

'Type WIN32_FIND_DATA
'    dwFileAttributes As Long
'    ftCreationTime As Date - needs a two-element structure (FILETIME)
'    ftLastAccessTime As Date - needs a two-element structure (FILETIME)
'    ftLastWriteTime As Date - needs a two-element structure (FILETIME)
'    nFileSizeHigh As Long
'    nFileSizeLow As Long
'    dwReserved0 As Long
'    dwReserved1 As Long
'    cFileName As String - must be declared to be (* MAX_PATH)
'    cAlternateFileName As String
'    dwFileType As Long - Not Wanted On Voyage
'    dwCreatorType As Long - Not Wanted On Voyage
'    wFinderFlags As Integer - Not Wanted On Voyage
'End Type
I was not too concerned about labeling schemes such as "hFind" for handles. I understand the standard, but I adopted a different standard many years ago, and to divert from that would make my other applications out-of-step.
(FWIW I was taught always to adopt an existing standard rather than create my own, and that I did back in 1997 or thereabouts)

I am forging ahead, thanks to your help, have learned to use "FTPInternetCloseHandle" and others, but at times have to hurdle over the different naming schemes that seem to pop up in WinInet.
Last night I got a crude "Create Folder" working - it is time-expensive, starting always at the root / and making sure each directory exists; I can optimize that once I have my basic compiler running.

Down the road I will have questions about timing:- It seems logical to sign on to the user site at the start of the macro and stay signed on until the macro ends. But when it comes to folders, I recognize that when I am compiling a set of hyperlinked web pages with images, most of the time all files to be uploaded will be in the one folder, so it makes sense to do homework on the laptop, and recognize that my FTP directory has not changed since the previous JPEG or HTML file was uploaded to that directory a few milliseconds ago. More on that later.

Thanks again
Chris
A watched pot never boils over

User avatar
SpeakEasy
3StarLounger
Posts: 319
Joined: 27 Jun 2021, 10:46

Re: Get Timestamp of file using WinInet

Post by SpeakEasy »

> labeling schemes such as "hFind" for handles. I understand the standard, but I adopted a different standard

My comment in my code related to this was referencing the fact that lngHandle is not very useful - handle to what? If your standard is a (somewhat wordy) Systems Hungarian (which it appears to be), then perhaps lngFind ... ;-)