Solved

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

Posted on 2002-06-24
10
2,777 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
 
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
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.

 
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:Kdo
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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 tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

759 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

19 Experts available now in Live!

Get 1:1 Help Now