Solved

Search for a File

Posted on 1997-08-28
11
149 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

920 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

11 Experts available now in Live!

Get 1:1 Help Now