Solved

Search for a File

Posted on 1997-08-28
11
151 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

856 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