?
Solved

Search for a File

Posted on 1997-08-28
11
Medium Priority
?
155 Views
Last Modified: 2010-04-06
I want to search for a file to make sure it exists before I do additional processing.  I fould a method called FileExists in the Delphi help file but it doesn't seem to allow you to specify directory/path or clearly show how to use it.  Code samples?
0
Comment
Question by:d4jaj1
[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
  • 7
  • 4
11 Comments
 
LVL 8

Accepted Solution

by:
ZifNab earned 40 total points
ID: 1342788
Hi d4jaj1,

Oh boy, it's late (23.30h) and I'm still at my work. My problem is still not solved, I hate it! Hope I can help you. 'Let this day be usefull for somebody.'

function FileExists(const FileName: string): Boolean;

FileName can be :

 - just a filename (e.g. HELLO.TXT)
 - filename+path   (e.g. c:\MYFILES\HELLO.TXT)

So if you want to see if a file exists in a directory called MYFILES and the filename is MYFILE.DAT and it's on harddisk D than you have to use FileExists as follow :

    FileExists('D:\MYFILES\MYFILE.DAT');

Hope this helps you, if not just say what the problem is.

Have fun,
c.u. ZifNab;
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1342789
Hi d4jaj1,

if FileExists('D:\MYFILES\MYFILE.DAT') then
 begin
  {Code when file exists}
 end else
      begin
       {Code whan file doesn't exists}
      end;

if FileExists('D:\MYFILES\MYFILE.DAT') then
 ShowMessage('File exists');
  else ShowMessage('File doesn''t exists');

Sorry, can't help myself. Just want to be sure that you don't have the problems I had today with programming. (Do you know something about a bug in QuickReport 1.1?)

Have fun,
c.u. ZifNab;

0
 
LVL 3

Author Comment

by:d4jaj1
ID: 1342790
Nope, I don't know much about QuickReport - I'm having problems with grouping in it myself.

In regard to your answer.  I do have one problem/issue, although It's my fault  wasn't clear in my question.  I aplogize for that.  I want to search for the file on the users system,  thus I won't know what the path & file name is were teh file should exist since the user decides that at the time of installation.  Is there a specifier I can use to determine the current directory, like InstallShield has <INSTALLDIR>?  If not, how would I search the entire drive or for that matter, change the drive?  I was hoping this would be as simple as DIR *.*.  :-).
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:ZifNab
ID: 1342791
I already found the question to easy, but you can find the current directory with GetDir(), the following example is for the Delphi help file.

var
   s : string;
 begin
   GetDir(0,s); { 0 = Current drive }
   MessageDlg('Current drive and directory: ' + s, mtInformation, [mbOk] , 0);
 end;

OffCourse you can iterate through the whole disk. But there is already a free component which does that, so you it's easier to get it than writing it yourself. It's called TreeScan and you can find it on the Super Delphi Page site.

Offcourse you can write it yourself.

Need information? Just ask!

Have fun,
c.u. ZifNab;
0
 
LVL 3

Author Comment

by:d4jaj1
ID: 1342792
I got it to work, but I have a question.  In my example below, the GETDIR returns d:\Delphi\Bin.  As you can see, I add a \ and the file name I'm searching for to the string.  My question is, what if GETDIR returns just 'D:\'.  By me adding the slash, I'll never find the file.  Any better way of writing my code?

var
s: string;
begin

getdir(0,s);

if fileexists(s + '\uk.dem') then
   showmessage('Found it')
   else showmessage('Didn''t find it');
end;
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1342793
Hi d4jaj1,

You can look at the last letter in the string. If it isn't a '\' then you add an '\'.

var
  s: string;

begin
 getdir(0,s);
 if Copy(s, length(s),1)<> '\' then s := s + '\';
 if fileexists(s + '\uk.dem') then
      showmessage('Found it')
   else showmessage('Didn''t find it');
 end;

That will work just fine.

Have fun,
c.u. ZifNab;

0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1342794
Hi d4jaj1,

You can look at the last letter in the string. If it isn't a '\' then you add an '\'.

var
  s: string;

begin
 getdir(0,s);
 if Copy(s, length(s),1)<> '\' then s := s + '\';
 if fileexists(s + 'uk.dem') then
      showmessage('Found it')
   else showmessage('Didn''t find it');
 end;

That will work just fine.

Have fun,
c.u. ZifNab;

0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1342795
Sorry about that,
There slipt an error in the first source.

Have fun,
c.u. ZifNab;
0
 
LVL 3

Author Comment

by:d4jaj1
ID: 1342796
Perfect!
0
 
LVL 3

Author Comment

by:d4jaj1
ID: 1342797
Could you please check out my "How to use TQuery" question from 7/27?  You seem to really give good answers an I really need the help on this one.  It's unlocked and worth 100 points.  
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1342798
Ok I'll look at the code and I'll try to help you. But I'm not perfect.
 
Thanks for the compliment!

Have fun,
c.u. ZifNab;
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
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…
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: …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

800 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