Solved

Checking if File on Internet Exists

Posted on 2003-11-09
4
325 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 20 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now