Solved

Search for a File

Posted on 1997-08-28
11
150 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi Dbf export problem to a Visual Foxpro application 6 188
tidtcpserver connection lost handle 2 83
Create a path if not exists 7 77
enhance the following code 3 31
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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

770 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