• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2786
  • Last Modified:

how search a name of folder in c or c++

i want to know that how we can find out a name of an directory or folder name by c or c++.
we can search a file name by using "findfrist()" funtion.
it search current directory for files but i want to search
an directory.
for eg.
if a directory consist like this
c:\pearson\parivesh
now if we do searching by above funtion in pearson directory then it search files in pearson but in that list it not find parivesh.
if u have any solution or program then please mail me it at pariveshj_jain1@rediffmail.com
thanks
0
pariveshj_jain1
Asked:
pariveshj_jain1
  • 2
  • 2
  • 2
  • +4
2 Solutions
 
graham_kCommented:
you will find a good example of using findFirst/findNext at http://www.cpe.mut.ac.th/coures/EECP0442/Book/C++BuilderUnleashed/htm/ch24.htm

Note, a common mistake when specifying filenames is to forget to double the backslashes. I.e, you should search for "c:\\pearson\\parivesh"

If that doesn't help, please post your code here for us to look at.

best wishes,

Graham
0
 
marcjbCommented:
/*
You would be better off using the POSIX functions that open directories and allow for searching.

For example, here is a function that accepts the name of a directory to scan.  It assumes that the name has a '\' or '/' at the end of the name.  It gives you all entries in the directory, and then you can check to see what they are.

*/

int scanDir(char *dirName)
{
    DIR *directory;
    char path[256];
    struct dirent *entry;
    struct stat fileStats;
    int rc = 0;

    if ( (directory = opendir(dirName)) == NULL )
        return -1;

    while ( (entry = readdir(directory)) != NULL ) {
        sprintf(path, "%s%s", dirName, entry->d_name);
        if ( stat(path, &fileStats) != 0 ) {
            closedir(directory);
            return -2;
        } else if ( S_ISDIR(fileStats.st_mode) ) {
            printf("'%s' is a directory\n", path);
        } else if ( S_ISREG(fileStats.st_mode) ) {
            printf("'%s' is a file\n", path);
        } else {
            printf("'%s' is something else\n", path);
        }
    }
    closedir(directory);
    return 0;
}

Hope this helps,

Marc
0
 
pagladasuCommented:
void finddDir(const CString &source)
{
WIN32_FIND_DATA filedata;
HANDLE handle=FindFirstFile((const char *)(source+"\\*.*"),&filedata);
BOOL ret=FindNextFile(handle,&filedata);
ret=FindNextFile(handle,&filedata);
while(ret){
if((filedata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY){
cout<<filedata.cFileName ;
}
ret=FindNextFile(handle,&filedata);
}
ret=FindClose(handle);
return;
}
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
graham_kCommented:
the good thing about the example whose URL I posted is that it is recursrive and while go through all directories, to any depth.
0
 
A_studentCommented:
the good thing about the example whose URL I posted is that it is recursrive and while go through all directories, to any depth.
0
 
pagladasuCommented:
how is it that graham_k and A_student post the same comment reference in the first person.
0
 
jimbucciCommented:
Looks like someone has two id's - eh?
0
 
SpideyModCommented:
graham_k,  A_student

I need you both to respond within 72 hours.  Is this a duplicate ID?  If so tell me all the IDs you are using.  Failure to do so requires me to submit your accounts for administrative review.

SpideyMod
Community Support Moderator @Experts Exchange
0
 
SpideyModCommented:
A request for a deletion has been made and denied as no feedback to the experts was provided.

SpideyMod
Community Support Moderator @Experts Exchange
0
 
Kent OlsenData Warehouse Architect / DBACommented:
No comment has been added lately, so it's time to clean up this TA.

I will leave a recommendation in the Cleanup topic area that this question is:
Split between graham_k and marcjb

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Kent (Kdo)
EE Cleanup Volunteer
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
  • 2
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now