InternetFileGetTime()

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

InternetFileGetTime()

02 Jan 2014, 13:16

InternetFileGetTime() returns the Last-Modified UTC for a web resource in YYYYMMDDHH24MISS format or optionally as UnixTime.
A_LastError will be content-length when available.

Usage: Msgbox % InternetFileGetTime( "http://ahkscript.org/favicon.ico" ) "`nSize = " A_LastError

Code: Select all

InternetFileGetTime( URL, UnixTime=0 ) { ;              SKAN | 02-Jan-2014 | goo.gl/zC1Bww

  try WebRequest := ComObjCreate( "WinHttp.WinHttpRequest.5.1" )
  catch 
    Return 0, WebRequest := "", ErrorLevel := 1           

  try WebRequest.Open( "HEAD", URL )
  catch 
    Return 0, WebRequest := "", ErrorLevel := 2           
  
  try WebRequest.Send()
  catch
    Return 0, WebRequest := "", ErrorLevel := 3           

  try Length := WebRequest.GetResponseHeader( "Content-Length" )
  catch {
  }

  try LastMod := WebRequest.GetResponseHeader( "Last-Modified" )
  catch 
    Return 0, WebRequest := "", DllCall( "SetLastError", UInt,Length ), ErrorLevel := 4           

; Convert from RFC2616 string to SYSTEMTIME to FILETIME 
  VarSetCapacity( SYSTEMTIME, 16, 0 )
  If ! DllCall( "winhttp\WinHttpTimeToSystemTime", WStr,LastMod, UInt,&SYSTEMTIME ) 
    Return 0, WebRequest := "", DllCall( "SetLastError", UInt,Length ), ErrorLevel := 5           
  DllCall( "SystemTimeToFileTime", UInt,&SYSTEMTIME, Int64P,FILETIME )
  
; Convert from FILETIME to DATETIME
  DT := 16010101000000
  DT += % ( FILETIME / 10000000 ), S
  IfNotEqual, UnixTime, 0, EnvSub, DT, 19700101000000, S
  
Return DT, WebRequest := "", DllCall( "SetLastError", UInt,Length ), ErrorLevel := 0  
}
My Scripts and Functions: V1  V2
User avatar
Chef
Posts: 50
Joined: 14 Nov 2013, 13:01

Re: InternetFileGetTime()

03 Jan 2014, 03:07

Nice, and would be better if you can make it work with ftp links.
I have a script where I need to get size of files on the internet, url_getHeader seems to work on most links, but not ftp ones.

Code: Select all

url_getHeader(URL)
{
	ComObjError(0)
	WinHttpReq:= ComObjCreate("WinHttp.WinHttpRequest.5.1")
	WinHttpReq.Open("Head", URL, false)
	WinHttpReq.Send()
	return,WinHttpReq.GetAllResponseHeaders()
		, ComObjError(1)
}
But an old function of yours DownloadProgress() seems to work on all links, however I had to make a whole executable where I call it and as soon as it get the size it exits. Not elegant I know.
Guest10
Posts: 578
Joined: 01 Oct 2013, 02:50

Re: InternetFileGetTime()

03 Jan 2014, 13:32

i try to use this to get info (time modified or size modified) but i get 0:

Msgbox % InternetFileGetTime( "http://news.google.com/news?pz=1&cf=all ... output=rss" ) "`nSize = " A_LastError

0
Size = 0


how can i accomplish this so that through a Timer i can get info when this site is modified (a new headline is added, etc.)? :ugeek:

currently, i have to download the site as a file and keep tabs on (by comparing) the filesize locally. :geek:
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: InternetFileGetTime()

03 Jan 2014, 15:14

Chef wrote:Nice, and would be better if you can make it work with ftp links.
The function name is misleading.. I will have to change it.

It should be easy to get size of an FTP file, but fetching Last-modified time would not be that easy.
Let me know if you need and I can post a modified version, but I can write and test only x86 code.
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: InternetFileGetTime()

03 Jan 2014, 15:21

Guest10 wrote:i try to use this to get info (time modified or size modified) but i get 0:
The function will return values only if they are available in HTTP Response Header.
Here follows the response header for http://ahkscript.org/favicon.ico
Status: HTTP/1.1 200 OK
Date: Fri, 03 Jan 2014 20:16:12 GMT
Server: Apache
Last-Modified: Sun, 03 Nov 2013 17:46:55 GMT
ETag: "32aea1b-47e-4ea4962a12db1"
Accept-Ranges: bytes
Content-Length: 1150
Connection: close
Content-Type: image/x-icon
You may test any of your http urls online: http://web-sniffer.net/
User avatar
Chef
Posts: 50
Joined: 14 Nov 2013, 13:01

Re: InternetFileGetTime()

04 Jan 2014, 03:57

Hello SKAN,

Could you please edit your function or post a new one that will work with ftp? I don't mind if it doesn't work to get the time (would be even better though Image).

Here is an ftp page to test it on if you want...
ftp://ftp.winzip.com/testdown/testdown.htm
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

InternetFileGetSize()

05 Jan 2014, 00:12

Tested in AHK 1.1 x86 Ansi/Unicode:

Code: Select all

MsgBox % InternetFileGetSize( "ftp://ftp.winzip.com/testdown/testdown.htm" )
MsgBox % InternetFileGetSize( "http://ahkscript.org/favicon.ico" )

InternetFileGetSize( URL ) {

  hMod := DllCall( "LoadLibrary", WStr, "wininet.dll" )

  hIO  := DllCall( "wininet\InternetOpenW"
                 , WStr, "Microsoft Internet Explorer" 
                 , UInt, 4          ; INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY
                 , Int,  0, Int, 0, UInt, 0 )

  hIU  := DllCall( "wininet\InternetOpenUrlW"
                 , UInt, hIO, WStr, URL, Int, 0, Int, 0
                 , UInt, 0x84000000 ; INTERNET_FLAG_DONT_CACHE|INTERNET_FLAG_RELOAD
                 , UInt, 0 )
  
  If ( hIO & hIU ) 

    If ( SubStr( URL,1,4 ) = "ftp:" ) 

          FileSize := DllCall( "wininet\FtpGetFileSize", UInt,hIU, UIntP,0, UInt )  

    Else  DllCall( "wininet\HttpQueryInfoW"
                , UInt,  hIU
                , UInt,  0x20000005     ; HTTP_QUERY_CONTENT_LENGTH|HTTP_QUERY_FLAG_NUMBER
                , UIntP, FileSize, UIntP,4, Int,0 )
        
 
  DllCall( "wininet\InternetCloseHandle", UInt, hIU )
  DllCall( "wininet\InternetCloseHandle", UInt, hIO )
  DllCall( "FreeLibrary", UInt, hMod )  
 
Return FileSize
}
As for FTP Modified time, you will have to log-on to the FTP server to have access to folder contents.
You may also use ftp.exe if you want to avoid DllCalls. There is an example in given in Docs under FileAppend command.
http://www.nsftools.com/tips/RawFTP.htm#MDTM
User avatar
Chef
Posts: 50
Joined: 14 Nov 2013, 13:01

Re: InternetFileGetTime()

05 Jan 2014, 07:11

Thanks Image
malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: InternetFileGetTime()

04 Sep 2015, 12:38

The script of ftp FileSize does not provide true information about size of files that are bigger than 4 GB.
This variant works fine:

Code: Select all

size1:=InternetFileGetSize("ftp://speedtest:[email protected]/test5Gb-a.db")
size2:=InternetFileGetSize("ftp://speedtest:[email protected]/test10Gb.db")
size3:=InternetFileGetSize("ftp://speedtest:[email protected]/test100Mb.db")
sizeGB1:=round(size1/(1024**3),2) " GB"
sizeGB2:=round(size2/(1024**3),2) " GB"
sizeMB:=round(size3/(1024**2),2) " MB"
msgbox % sizeGB1 "`n" sizeGB2 "`n" sizeMB "`n`n" size " Bytes`n" round(size/(1024**3),2) " GB"

InternetFileGetSize( URL ) {

  hMod := DllCall( "LoadLibrary", WStr, "wininet.dll" )

  hIO  := DllCall( "wininet\InternetOpenW"
                 , WStr, "Microsoft Internet Explorer"
                 , UInt, 4          ; INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY
                 , Int,  0, Int, 0, UInt, 0 )

  hIU  := DllCall( "wininet\InternetOpenUrlW"
                 , UInt, hIO, WStr, URL, Int, 0, Int, 0
                 , UInt, 0x84000000 ; INTERNET_FLAG_DONT_CACHE|INTERNET_FLAG_RELOAD
                 , UInt, 0 )

  If ( hIO & hIU )

    If ( SubStr( URL,1,4 ) = "ftp:" )
   {
      varSetCapacity(huint,4)
      FileSize:=DllCall( "wininet\FtpGetFileSize", UInt,hIU,UInt,&huint,UInt)
      FileSize := FileSize + (NumGet(huint)*(2**32))
   }

    Else  DllCall( "wininet\HttpQueryInfoW"
                , UInt,  hIU
                , UInt,  0x20000005     ; HTTP_QUERY_CONTENT_LENGTH|HTTP_QUERY_FLAG_NUMBER
                , UIntP, FileSize, UIntP,4, Int,0 )


  DllCall( "wininet\InternetCloseHandle", UInt, hIU )
  DllCall( "wininet\InternetCloseHandle", UInt, hIO )
  DllCall( "FreeLibrary", UInt, hMod ) 
Return FileSize
}

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 74 guests