Greetings:
That`s my question, until know i`ve been doing it by a batch file that i execute then read, is there a way to do it without the batch?, what i need is to list all of the Word (.doc) files of a given directory
Thanks in advance

Dani AI

Generated

For : you don’t need a batch file — use the OS directory APIs from C. Platform matters: on Unix-like systems use opendir/readdir (or glob), on Windows use the Win32 file-search functions. pointed you to a start and showed basic opendir usage; below are more robust, practical examples and a few gotchas.

POSIX-style (Linux, macOS) — open the directory, filter filenames for the .doc extension, and optionally stat the path to ensure it’s a regular file:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
#include <limits.h>

int is_doc(const char *name) {
    const char *ext = strrchr(name, '.');
    return ext && strcasecmp(ext, ".doc") == 0; /* case-insensitive */
}

int main(void) {
    const char *dir = "/path/to/dir";
    DIR *d = opendir(dir);
    struct dirent *e;
    char path[PATH_MAX];
    struct stat st;
    if (!d) { perror("opendir"); return 1; }
    while ((e = readdir(d)) != NULL) {
        if (!is_doc(e->d_name)) continue;
        snprintf(path, sizeof(path), "%s/%s", dir, e->d_name);
        if (stat(path, &st) == 0 && S_ISREG(st.st_mode))
            printf("%s\n", path);
    }
    closedir(d);
    return 0;
}

Windows (native) — use FindFirstFile / FindNextFile. This is simpler for a single directory and handles Windows semantics (and wildcard matching) directly:

#include <windows.h>
#include <stdio.h>

int main(void) {
    WIN32_FIND_DATAA fd;
    HANDLE h = FindFirstFileA("C:\\path\\to\\dir\\*.doc", &fd);
    if (h == INVALID_HANDLE_VALUE) return 1;
    do {
        if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
            printf("%s\\%s\n", "C:\\path\\to\\dir", fd.cFileName);
    } while (FindNextFileA(h, &fd));
    FindClose(h);
    return 0;
}

Notes and tips: use *.doc* if you want to include .docx; watch case-sensitivity differences (Linux vs Windows); for recursive searches implement a recursive directory walk and skip ./..; avoid system("dir") for portability and security. If you can use C++, std::filesystem (C++17) greatly simplifies this task.

Recommended Answers

All 2 Replies

/*
Name: Praveen Kr.
email id: [email]prvnkmr449@gmail.com[/email]
mobile no.:08808130075
for any programming help u can contact on this id.
*/

int main()
{
DIR *dir;
struct dirent *ent;
        dir = opendir ("c:\\tcpp\\");
if (dir != NULL) {

  /* print all the files and directories within directory */
  while ((ent = readdir (dir)) != NULL) {
    printf ("%s\n", ent->d_name);
  }
  closedir (dir);
} else {
  /* could not open directory */
  perror ("");
  return EXIT_FAILURE;
}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.