Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Checking if File on Internet Exists

Posted on 2003-11-09
4
Medium Priority
?
345 Views
Last Modified: 2010-04-05
Hey,

How do I find out if a file exists on the internet and then tell the user in a message box that it exists ?

I was thinking something like ...

procedure TForm1.Button1Click
begin
If File ('www.website.com/test.txt') exists then
showmessage (' file exists');
end;
end.

Cheers,

Computer Programer
0
Comment
Question by:computerprogramer
4 Comments
 
LVL 5

Expert Comment

by:DeerBear
ID: 9715430
Hi,

Imho the only way to determine its existance is trying to download it.

HTH,

Andrew
0
 
LVL 6

Expert Comment

by:DaFox
ID: 9715463
Hi,

Try to retrieve the headers, e.g. by using HttpQueryInfo() API if you're not using any other internet components.

Regards,
Markus
0
 
LVL 7

Expert Comment

by:jconde
ID: 9715653
Hi!

It's really hard to know if a file exists or not on a specified server unless you intelligently parse the contents of it.

The reason is, first of all  "If File ('www.website.com/test.txt') exists then ..." will implicitly do the following

open a socket connection to www.website.com
use the GET method to retrieve test.txt
-> now you need to parse the returned text and determine if it exists or not.

The problem is many websites have a custom 404 display error screen so it may not be trivial to know if the file exists or not.

You must know exactly what type of file you're looking for and something regarding its contents so that you can determine if it exists or not.

BTW, I assume you're talking over the HTTP protocol correct ?

You can test this out for yourself by doing the following:

prompt> telnet www.website.com 80
GET /your_file.txt

and in most cases you WILL get a result back ... it may either be the 404 error a standard way, it may be a customized 404 page or can be the file itself.  Since that's a 3:1 posibility of the file existing, its easier to test for the file rather than try to parse the headers for the 404 error message.

A good approach for this is to develope either a service (win32) or a daemon listening on port X that returns true or false if the file exists, but I get the feeling you need this over HTTP.
0
 
LVL 2

Accepted Solution

by:
SaLz earned 80 total points
ID: 9718691
This will help you to find if a file is located on the internet or any local files.

it will try and execute if it can't then file it will tell you, file not found, if it is found it, will say, file found.

very simple function, hope it helps you out.


{$R *.dfm}

uses ExtActns;
function CheckFile:boolean;
var
CheckURL:string;
begin
Result:=True;
CheckURL:= 'http://127.0.0.1/index.xml';
with TDownLoadURL.Create(nil) do
  try
    URL:=CheckURL;
    try
      ExecuteTarget(nil);
    except
      Result:=False;
    end;
  finally
    Free;
end;
end;


procedure TForm1.Button1Click(Sender: TObject);

begin

    if CheckFile then
    begin
    showmessage ('File Exists!');
    end
    else
    begin
    showmessage ('File Not Found!');
    Exit;
    end;
end;
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

885 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