Solved

Checking if File on Internet Exists

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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…
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…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

734 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