Solved

searching for a file on all hard disk drives of a PC

Posted on 1999-01-10
12
160 Views
Last Modified: 2010-04-06
Hi experts,

can you please show me how I can search for a specific file - for example: autoexec.bat - on all hard
disk drives of my PC?

With kind regards

Mathes
0
Comment
Question by:mathes
[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
  • 5
12 Comments
 
LVL 20

Accepted Solution

by:
Madshi earned 10 total points
ID: 1355671
Hi Mathes,

can't give you a ready to use source, but I guess, you can put the parts together by your own...

Use this to find all hard drives:

var c1,c2 : cardinal;
    ch1   : char;
begin
  c2:=GetLogicalDrives;
  for ch1:='A' to 'Z' do
    if odd(c2 shr (ord(ch1)-ord('A'))) then begin
      c1:=GetDriveType(PChar(ch1+':\'));
      if c1=DRIVE_FIXED then begin
        // here do the enumeration stuff...
      end;
    end;
end;

If you only need to look if there's an "autoexec.bat" on the root of each drive, simply use
  "fileExists(ch1+':\autoexec.bat')"

If you have to look through all the subDirs, use a recursive FindFirstFile, FindNextFile, FindClose loop.

Any questions? Please ask...

Regards, Madshi.
0
 

Author Comment

by:mathes
ID: 1355672
Dear Madshi,

thank you for your posting.

Can you please show me, how I can perform a recursive FindFirstFile, FindNextFile, FindClose loop?

With kind regards

mathes

(P.S.: i am sure the answer to my question can be found in a FAQ, however I don't have such a FAQ at hand right now.

0
 
LVL 20

Expert Comment

by:Madshi
ID: 1355673
Hi Mathes,

try this one:

procedure EnumFiles(path: string; findFile: string);
var fd1 : TWin32FindData;
    c1  : cardinal;
    s1  : string;
begin
  if fileExists(path+findFile) then
    MessageBox(0,pchar(path+findFile),'File found:',0);
  c1:=FindFirstFile(PChar(path+'*.*'),fd1);
  if c1<>INVALID_HANDLE_VALUE then
    try
      repeat
        s1:=string(fd1.cFileName);
        if (s1<>'.') and (s1<>'..') and (fd1.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY<>0) then
          EnumFiles(path+s1+'\',findFile);
      until not FindNextFile(c1,fd1);
    finally Windows.FindClose(c1) end;
end;

EnumFiles('C:\','mscreate.dir');

Regards, Madshi.
0
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!

 

Author Comment

by:mathes
ID: 1355674
Dear Madshi,

thank you for your posting.

Can you please show me, how I can perform a recursive FindFirstFile, FindNextFile, FindClose loop?

With kind regards

mathes

(P.S.: i am sure the answer to my question can be found in a FAQ, however I don't have such a FAQ at hand right now.

0
 

Author Comment

by:mathes
ID: 1355675
Dear Madshi,

thank you for your posting.

Can you please show me, how I can perform a recursive FindFirstFile, FindNextFile, FindClose loop?

With kind regards

mathes

(P.S.: i am sure the answer to my question can be found in a FAQ, however I don't have such a FAQ at hand right now.

0
 

Author Comment

by:mathes
ID: 1355676
Hi Madshi,

thank you once again for your posting.


Can you please tell me if your EnumFiles procedure has a return value?

Actually I would like to to something like this:


location_of_my_dummy_file:=EnumFiles('dummy.doc');

ShellExecute('winword.exe',location_of_my_dummy_file);


But this pseudocode example willonly work, if you can find
drive,directory,filename,extension in 'location_of_my_dummy_file'.
How can this be accomplished ?

With kind regards

Mathes


0
 
LVL 20

Expert Comment

by:Madshi
ID: 1355677
Hmm. EnumFiles has no return value. How about simply running it and look what will happen. It will show all files it found in a MessageBox. The problem is, the file you're searching for, could be found in more than one location. If you want to abort the search when the first match is found, you should do something like this:

function EnumFiles(path: string; findFile: string) : string;
var fd1 : TWin32FindData;
    c1  : cardinal;
    s1  : string;
begin
  if fileExists(path+findFile) then begin
    result:=path+findFile;
    exit;
  end;
  c1:=FindFirstFile(PChar(path+'*.*'),fd1);
  if c1<>INVALID_HANDLE_VALUE then
    try
      repeat
        s1:=string(fd1.cFileName);
        if (s1<>'.') and (s1<>'..') and (fd1.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY<>0) then begin
          result:=EnumFiles(path+s1+'\',findFile);
          if result<>'' then break;
        end;
      until not FindNextFile(c1,fd1);
    finally Windows.FindClose(c1) end;
end;

Now the complete path (with drive and ...) is returned, but you will only get the first found file...

Regards, Madshi.
0
 

Author Comment

by:mathes
ID: 1355678
Dear Madshi,

for my purpose it will be enough to abort the search when the first match is found.
In your function I have to use both the filename AND the path as parameter. What can I do,
if I only know that the file "dummy.doc" can be found on my PC, but I don't know in which directory.
In this case I don't know which directory i should use as parameter. How can I use your function
in this situation? Ithink the best would be to start searching oin the root directory and as soon as
the first match is found, the search should be aborted, and the first match should be returned as result
of this function call.

On which drives will your function search for the files? Perhaps the file can't be found on partition
C:, but can be found on partition D: of my harddisk..

With kind regards

Mathes

0
 
LVL 20

Expert Comment

by:Madshi
ID: 1355679
For this purpose I gave you the sources in my original answer:

function findFileOnAllHarddisks(fileName: string) : string;
var c1,c2 : cardinal;
    ch1   : char;
begin
  result:='';
  c2:=GetLogicalDrives;
  for ch1:='A' to 'Z' do
    if odd(c2 shr (ord(ch1)-ord('A'))) then begin
      c1:=GetDriveType(PChar(ch1+':\'));
      if c1=DRIVE_FIXED then begin
        result:=EnumFiles(ch1+':\',fileName);
        if result<>'' then break;
      end;
    end;
end;

This looks through all harddisks in your computer.

Regards, Madshi.
0
 

Author Comment

by:mathes
ID: 1355680

Dear Madshi,

thank you again for your posting.

I meanwhile tried your code.

In line

result:=EnumFiles(ch1+':\',fileName);

(within your function findFileOnAllHarddisks)

Delphi says:

incompatible types: string and procedure, untyped pointer or untyped
parameter.

Can you please tell me, what is wrong here?

With kind regards

Mathes


0
 
LVL 20

Expert Comment

by:Madshi
ID: 1355681
Not on my computer!!!

If I put together the last two functions I gave you (from 05:51AM and 6:27AM), everything is fine. It works - I've tested it.

Regards, Madshi.
0
 

Author Comment

by:mathes
ID: 1355682
Dear Madshi,

thank you for yourexcellent help. With sources o the correct time, everything works perfectly.

With kind regads

Mathes
0

Featured Post

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Firemonkey DbLookupComboBox equivalent ? 2 79
Delphi: making a BW image transparent 10 138
Delphi Seattle StructureView color 1 34
Firemonkey Tcameracomponent resultion 1 48
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

759 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