Solved

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

Posted on 2002-06-24
10
2,780 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
  • 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
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

Portable, direct connect server access

The ATEN CV211 connects a laptop directly to any server allowing you instant access to perform data maintenance and local operations, for quick troubleshooting, updating, service and repair.

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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 how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

839 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