Solved

Checking if File on Internet Exists

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

813 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

10 Experts available now in Live!

Get 1:1 Help Now