Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Checking if File on Internet Exists

Posted on 2003-11-09
4
Medium Priority
?
344 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

721 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