how to search files from local hard disks?

i am using Turbo C++.
i want to make a program which can find all the files with given name or extensions on a computer.Just like find on start menu of windows.can anybody tell me code for it.i had made a program which finds files from the folder only where it is.
help me...
thanks...
amit
b_amitAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
AlexVirochovskyConnect With a Mentor Commented:
J hope, my code helps you. Alex
0
 
djbusychildCommented:
so you're on windows?
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
wkmatt42Commented:
If you're not using SDK, try the _findfirst (or findfirst) function. (And the companion _findnext/findnext.) I'm pretty sure Turbo C++ supports them, but it's been a while.
0
 
makerpCommented:
VC++ examle, this is from my mp3 system. you will need to code a recursive function that calls itself when it finds a directory, enjoy

void Mp3FileStore::get_tracks(char *path, long album_id)
{
     assert(path != NULL);

     struct _finddata_t ff;
     long fhandle;
     char spath[256], fname[_MAX_FNAME], ext[_MAX_EXT];

     /* make a path with wild card for .mp3 on the end */
     sprintf(spath,"%s\\*.*",path);
     /* open the direcory */
     if((fhandle = _findfirst(spath,&ff)) != -1)
     {
          /* loop through each entry */
          while(!_findnext(fhandle,&ff))
          {    
               /* is it a file */
               if(ff.attrib ^ _A_SYSTEM)
               {
                    /* extract the track name */
                    _splitpath(ff.name,NULL,NULL,fname,ext);          
                    if(!stricmp(ext,".mp3"))
                    {      
                         add_track_to_array(fname,album_id);
                    }
               }
          }
          close(fhandle);
     }

}
0
 
b_amitAuthor Commented:
hello friends,
i am not using SDK,
but 'wkmatt42',findfirst & findnext find files in only one directory,so i had to copy it in each directory???
'makerp'
i will take a look at your code tonight...
thanks for everybody...
amit
0
 
wkmatt42Commented:
findfirst & findnext should find everything in a directory, including subdirectories. It's up to you (or one of us if you need help) to use that information to walk through the entire directory tree. It will also give you a chance to explore recursion. Yee haw.
0
 
AlexVirochovskyCommented:
Next is may code for delete all files from some directory
and in all subdirectories. Change it a bit and you can
don't delete files, but ...
/*============================================================*/
/* Name        : ClearDisket                          */
/* Description : Clear disket from all file                */
/* Arguments   : Root dir of disket("a:" or "b:")           */
/* Return      : None                               */
/*============================================================*/
void ClearDisket(char *szFind)
{
  char szLocalFind[255];

  strcpy(szLocalFind,szFind); strcat(szLocalFind,"\\*.*");
                               //delete all file
  struct ffblk ffblk;
  int done = findfirst(szLocalFind,&ffblk,
                   FA_ARCH | FA_SYSTEM | FA_HIDDEN);
  while (!done)               //files
    {
      char szName[255];
      strcpy(szName, szFind); strcat(szName,"\\");
      strcat(szName,ffblk.ff_name);
//in this place I delete file: make your own
      if (!DeleteFile(szName))          //read only!
        {
          chmod(szName,S_IREAD | S_IWRITE);
          DeleteFile(szName);
        }
      done = findnext(&ffblk);
    }
  done = findfirst(szLocalFind,&ffblk,FA_DIREC);//all subdirs
  while (!done)
    {               //now dir:
      char szName[255];
      strcpy(szName, szFind); strcat(szName,"\\");
      strcat(szName,ffblk.ff_name);
      if (*ffblk.ff_name != '.' && !RemoveDirectory(szName))
        {
          ClearDisket(szName);          //delete all sub dir
//in this place I delete subdirectory: make your code
          RemoveDirectory(szName);     //must be empty!
        }
      done = findnext(&ffblk);
    }
}
 
0
 
makerpCommented:
you probebly need to recurse, therefore make my function recursive

pseudo

function(pass path)
{
  opendir
  for each fileordir
    if file then
      do what ever with file
    elseif
      call this function(pass path)
    end if
  next
}
0
 
b_amitAuthor Commented:
hello alexvirochovsky
             can you send me your complete source-code of the program given above.i did not understood it.
 how DeleteFile(szName) works? and lot more.
 can you mail me it at b_amit4u@yahoo.co.in
thanking you...
 amit
0
 
AlexVirochovskyCommented:
>>complete source-code of the program given above
This program is the part of huge project, and ALL source
code don't helps you.
And code above is full code for delete all files and subdirectories from some place (say disket, if parameter =
"a:"
>>how DeleteFile(szName) works?
it is windows Api, that simple delete file, in BC for
such purpose exist function unlink(file_name);
>>how this code works?
it works in 2 statges in recursie :
1. Delete all file in this directory
2. Find all subdirectory (FA_DIREC attribute) and
call yourself!
0
All Courses

From novice to tech pro — start learning today.