[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to update text file off an URL?

Posted on 2011-05-13
5
Medium Priority
?
640 Views
Last Modified: 2012-05-11
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.
0
Comment
Question by:John86a
  • 3
5 Comments
 
LVL 23

Assisted Solution

by:Ferruccio Accalai
Ferruccio Accalai earned 1000 total points
ID: 35755439
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
 

Author Comment

by:John86a
ID: 35756228
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
 

Author Comment

by:John86a
ID: 35756294
Actually, I found a way to download, just need to compare the local txt with the one on the website.
0
 
LVL 14

Accepted Solution

by:
systan earned 1000 total points
ID: 35756913
...
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
 

Author Closing Comment

by:John86a
ID: 35757129
thanks
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses
Course of the Month19 days, 16 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question