Solved

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

Posted on 2002-06-24
10
2,782 Views
Last Modified: 2011-09-20
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
Comment
Question by:pariveshj_jain1
[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
  • 2
  • 2
  • 2
  • +4
10 Comments
 
LVL 6

Accepted Solution

by:
graham_k earned 100 total points
ID: 7103308
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
 
LVL 3

Assisted Solution

by:marcjb
marcjb earned 100 total points
ID: 7103588
/*
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
 
LVL 4

Expert Comment

by:pagladasu
ID: 7108714
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
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!

 
LVL 6

Expert Comment

by:graham_k
ID: 7109854
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
 

Expert Comment

by:A_student
ID: 7110127
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
 
LVL 4

Expert Comment

by:pagladasu
ID: 7111106
how is it that graham_k and A_student post the same comment reference in the first person.
0
 
LVL 3

Expert Comment

by:jimbucci
ID: 7153852
Looks like someone has two id's - eh?
0
 

Expert Comment

by:SpideyMod
ID: 8329026
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
 

Expert Comment

by:SpideyMod
ID: 8329028
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
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 9480340
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

Independent Software Vendors: 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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

728 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