Solved

how to search files from local hard disks?

Posted on 2001-06-28
11
183 Views
Last Modified: 2010-04-15
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
0
Comment
Question by:b_amit
  • 3
  • 2
  • 2
  • +2
11 Comments
 
LVL 5

Expert Comment

by:djbusychild
ID: 6236853
so you're on windows?
0
 
LVL 5

Expert Comment

by:djbusychild
ID: 6236869
0
 

Expert Comment

by:wkmatt42
ID: 6236995
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
 
LVL 10

Expert Comment

by:makerp
ID: 6237077
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
 

Author Comment

by:b_amit
ID: 6238683
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Expert Comment

by:wkmatt42
ID: 6238708
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
 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 6240493
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
 
LVL 10

Expert Comment

by:makerp
ID: 6242105
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
 
LVL 14

Accepted Solution

by:
AlexVirochovsky earned 75 total points
ID: 6245081
J hope, my code helps you. Alex
0
 

Author Comment

by:b_amit
ID: 6250008
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
 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 6251331
>>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

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

705 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

22 Experts available now in Live!

Get 1:1 Help Now