Solved

Search for a File

Posted on 1997-08-28
11
148 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
  • 7
  • 4
11 Comments
 
LVL 8

Accepted Solution

by:
ZifNab earned 20 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
 
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
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 video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

757 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

21 Experts available now in Live!

Get 1:1 Help Now