Solved

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

Posted on 2002-06-24
10
2,779 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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
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.

815 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

7 Experts available now in Live!

Get 1:1 Help Now