user5881924
user5881924

Reputation: 25

How to check if a sub-directory contains text files using c program

I have a directory say A, into which i have sub-directories aa,bb,cc,dd,ee,ff. Each sub directories have a number of .txt, .bin, .dat files. What I want to do is, check each of the sub-directory to see if it contains a text file, if yes return the sub directory name.

The below c script lists the sub directories, but please assist to check within the sub-directory for a txt file.

I'm trying to do this in windows 7-visual studio 2010

#include <dirent.h> 
#include <stdio.h> 
int main(void)
{
    DIR *d;
    DIR *f;
    struct dirent *dir;
    d = opendir("C:\\Users\\xp\\Desktop\\Star1");
    if (d) {
        while ((dir = readdir(d)) != NULL) {
            if (dir->d_name[0] != '.') {
                f=opendir(dir->d_name);
                if (strstr(dir->d_name , ".txt")) {
                    printf("%s\n", dir->d_name);
                }
            }
        }
        closedir(d);
    }

    return(0);
}

Upvotes: 0

Views: 678

Answers (3)

jdarthenay
jdarthenay

Reputation: 3147

As an alternative, lazy and Windows-specific solution, you can just let the job to the windows for command this way:

#include <stdio.h>
#include <string.h>

#define MAX_LENGTH 1024

int main()
{
    char buffer[MAX_LENGTH];

    FILE *f = _popen("cmd /c @for /R C:\\Users\\xp\\Desktop\\Star1\\ %i in (.) do @if exist \"%~i\"\\*.txt echo %~fi 2> NUL", "r");
    if (f != NULL)
    {
        while (fgets(buffer, MAX_LENGTH, f) != NULL)
        {
            int len = strlen(buffer);
            if (buffer[len - 1] == '\n')
            {
                buffer[--len] = '\0';
            }

            printf("Found: %s\n", buffer);
        }
        _pclose(f);
    }
}

Edit: fixed answer to give directory list instead of .txt files.

Upvotes: 0

Some programmer dude
Some programmer dude

Reputation: 409136

You could use a flag. If you find a file ending in ".txt" then you set the flag and exit the loop. After the loop you check the flag.


One way to check if a string ends with a specific sub-string:

static const char string_to_find[] = ".txt";

...

// First make sure the filename is long enough to fit the name-suffix
if (strlen(dir->d_name) > strlen(string_to_find))
{
    // +strlen(dir->d_name) to get a pointer to the end of dir->d_name
    // -strlen(string_to_find) to get a pointer to where the suffix should start
    if (strcmp(dir->d_name + strlen(dir->d_name) - strlen(string_to_find),
               string_to_find) == 0)
    {
        // File-name ends with ".txt"
    }
}

Upvotes: 1

svanderwoude
svanderwoude

Reputation: 119

Instead of printing the directories you could just put it in an if-statement to check if it's the desired file. If it is: return the directory name, else continue. You can put it all in a for-loop so you can check every directory.

For example:

If(!strcmp(filename, filetofind))
    Return dirname

Upvotes: 0

Related Questions