How to update text file off an URL?

Hi, every time an update button is pressed it must check the size of the current file and the file off an url (e.g.: http://www.test.com/filename.txt) - if they happen to be different then download the new one to replace the old one, and different as in, bigger or smaller. If the file is unavailable returns an error message explaining.

How can I achieve this?

Thanks.
John86aAsked:
Who is Participating?
 
systanCommented:
...
var
  hSession: HINTERNET;
  hService: HINTERNET;
  DiskSize,NetSize: DWORD;
  dwSize, Reserved: DWORD;
  BufSize, dwBytesRead: DWORD;
  Buffer: array[0..4095] of byte;
  buf: String;
...
...
Reserved := 0;
dwSize := sizeof(NetSize);
HttpQueryInfo(hService, HTTP_QUERY_CONTENT_LENGTH or HTTP_QUERY_FLAG_NUMBER,
@NetSize, dwSize, Reserved);
buf := Format('%d', [NetSize]);
NetSize:=strtoint(buf);

if (DiskSize = NetSize) then
begin
='You have already the latest update';
exit;
end;
...
...

Open in new window

0
 
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
To get the file size you can use something like this, then once you have the size you can compare it and eventually download the file

Uses Wininet

function FileSize(const url: string): String;
var
  hInet: HINTERNET;
  hConnect: HINTERNET;
  infoBuffer, Buffer: array [0..512] of char;
  dummy: DWORD;
  bufLen, lenbuf: DWORD;
  okay: LongBool;
  reply: String;
begin
  hInet := InternetOpen(PChar(application.title), INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY, nil, nil, 0);
  hConnect := InternetOpenUrl(hInet, PChar(url), nil, 0, INTERNET_FLAG_NO_UI, 0);
  if not Assigned(hConnect) then
    // ----------------------------------------------------------
    // If we couldn't open a connection then we know the url
    // is bad. The most likely reason is that the url is bad,
    // but it could be because of an unknown or badly specified
    // protocol.
    // ----------------------------------------------------------
    result := 'Bad url'
  else
  begin
    // ------------------------------
    // Create a request for the url.
    // ------------------------------
    dummy := 0;
    bufLen := Length(infoBuffer);
    lenbuf := Length(Buffer);
    okay := HttpQueryInfo(hConnect, HTTP_QUERY_STATUS_CODE, @infoBuffer[0], bufLen, dummy);
    if not okay then
      // Probably working offline, or no internet connection.
      result := 'No internet connection'
    else
    begin
      reply := infoBuffer;
      if reply = '200' then
      begin
        // File exists, let's get the size.
        HttpQueryInfo(hConnect, HTTP_QUERY_CONTENT_LENGTH, @Buffer[0], lenbuf, dummy);
        result := Buffer;
      end
      else if reply = '401' then
        // Not authorised. Assume page exists,
        // but we can't check it.
        result := 'Error 401. Not Authorised'
      else if reply = '404' then
        // No such file.
        result := 'Error 404. File not found'
      else if reply = '500' then
        // Internal server error.
        result := 'Error 500. Internal server error'
      else
        // Shouldn't get here! It means there is
        // a status code left unhandled.
        result := 'Generic Error';
    end;
    InternetCloseHandle(hConnect);
  end;
  InternetCloseHandle(hInet);
end;

use like

MySize: String;

MySize := FileSize('http://www.test.com/filename.txt');
If Pos('Error',MySize) > 0 then ShowMessage(MySize) //let's show the error
else //MySize is the internet filesize, use it as needed
0
 
John86aAuthor Commented:
That's very nice, thanks! However, how can I compare filename.txt with a local filename.txt and download it in case they're different?
0
 
John86aAuthor Commented:
Actually, I found a way to download, just need to compare the local txt with the one on the website.
0
 
John86aAuthor Commented:
thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.